Java: Making of drawing board

Source: Internet
Author: User

After you've learned to use button sniffing and mouse listeners, we can make a own drawing board, the following is a simple drawing board I made before, the function is still insufficient, but has been able to complete the pencil, eraser, draw a straight line, circle and other basic work, but also to complete the free choice of color, Don't let yourself do it.

The first is the interface class:

Java code

Import Java.awt.BorderLayout;  
Import Java.awt.Color;  
Import java.awt.Dimension;  
Import Java.awt.FlowLayout;  
Import Java.awt.Graphics;  
Import Java.awt.GridLayout;  
Import java.awt.event.ActionEvent;  
      
Import Java.awt.event.ActionListener;  
Import Javax.swing.ButtonGroup;  
Import Javax.swing.ImageIcon;  
Import Javax.swing.JButton;  
Import Javax.swing.JColorChooser;  
Import Javax.swing.JFrame;  
Import Javax.swing.JLabel;  
Import Javax.swing.JPanel;  
      
Import Javax.swing.JRadioButton; @SuppressWarnings ("Serial") public class Draw extends jframe{public color color=color.black;//Set the initial color privat  
    e Color colorselector;  
    Private JButton colorselectorbu[]=new jbutton[6];  
    A new object is required to create the object, otherwise his default value is null private JPanel colorjp=new JPanel ();  
        public static void Main (String args[]) {Draw dr=new Draw ();  
    Dr.showframe ();        public void Showframe () {JPanel north=new JPanel (); Upper area, storing color buttons toand color selector JPanel west=new JPanel ();       Left area, storing graphics button JPanel center=new JPanel ();        Intermediate area, storing artboards JPanel south=new JPanel (); The area below to display the current mouse position and the size of the drawing//-----------------Middle Area---------------center.setlayout (New FlowLayout (F  
        Lowlayout.left);//sets the layout of the middle area to left-aligned JPanel drawjp=new JPanel ();  
        Drawjp.setbackground (Color.White);  
        Drawjp.setpreferredsize (New Dimension (600,500));  
        Center.add (DRAWJP);  
        -----------------Upper Area----------------JButton colorchooserbu=new JButton ("more Colors");  
        Colorchooserbu.addactionlistener (Colorlistener);  
        Colorjp.setpreferredsize (New Dimension (120,60));  
        Colorjp.setlayout (New GridLayout (3,6,0,0));  
                Create a color group colour color[]=new color[]{color.black,color.blue,color.cyan,color.dark_gray,color.gray,color.green , Color.light_gray,color.green,color.magenta,color.orange,color.pink,color.yellow};       
        Dimension d=new Dimension (20,20);  
            for (int i=0;i<color.length;i++) {JButton colorbu=new JButton ();  
            Colorbu.setbackground (Color[i]);  
            Colorbu.setpreferredsize (d);  
            Colorjp.add (Colorbu);  
        Colorbu.addactionlistener (listener); }//button selector custom color group for (int j=0;j<colorselectorbu.length;j++) {colorselectorbu[j]=new JB  
            Utton ();  
            Colorselectorbu[j].setpreferredsize (d);  
            Colorjp.add (Colorselectorbu[j]);  
        Colorselectorbu[j].addactionlistener (listener);  
        } north.add (COLORJP);  
        North.add (Colorchooserbu);  -----------------the left area----------------String img[]=new string[]{"line", "oval", "rect", "RoundRect", "Eraser",   
        "Fill", "Pencil", "select"}; String img1[]=new string[]{"Line1", "Oval1", "Rect1", "Roundrect1", "Eraser1", "fill1", "pencil1 "," Select1 "};  
        Buttongroup bg=new Buttongroup (); for (int i=0;i=6) {//exceed six buttons, return 0 i=0;  
}  
        }  
    }; }

Here is the Listening class:

Java code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.