Java Drawing Board

Source: Internet
Author: User
Tags gety

Java Drawing Board
Import Java.awt.basicstroke;import java.awt.borderlayout;import Java.awt.button;import Java.awt.Color;import Java.awt.cursor;import Java.awt.dimension;import Java.awt.frame;import Java.awt.graphics;import Java.awt.graphics2d;import Java.awt.gridlayout;import Java.awt.label;import Java.awt.panel;import Java.awt.renderinghints;import Java.awt.toolkit;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;import Java.awt.event.mousemotionlistener;import Java.awt.event.WindowAdapter; Import Java.awt.event.windowevent;import java.awt.image.bufferedimage;/** * Simple drawing board Program * * @author He Qingsong * @version 1.0 */PUBL IC class UI extends Frame {//artboard private Palette Palette = null;//Display the current color panel private panel noncecolor = null;//Brush thickness Private  Label drawwidth = null;//painting nib point of decoration private Label drawcap = null;//Pick Color button listener event class private buttoncoloraction buttoncoloraction = null;//the cursor-style listener event class after the mouse enters the button privAte buttoncursor buttoncursor = null;//Brush-style listener Event private Buttonstrokeaction buttonstrokeaction = null;/** * Construction method * */PUBL IC UI () {//Set title bar text Super ("Drawing pad");//Construct a drawing board Palette = new Palette (); panel pane = new Panel (new GridLayout (2, 1));//Brush color selector Panel panecolor = new Panel (new GridLayout (1, 13));//12 color selection Buttons but ton [] Buttoncolor = new BUTTON[12]; color [] color = {Color.Black, Color.Blue, Color.cyan, Color.darkgray, Color.gray, Color.green, Color.magenta, Color.oran GE, Color.pink, color.red, Color.White, color.yellow};//displays the current color of the panel noncecolor = new Panel (); Noncecolor.setbackground ( Color.Black);p Anecolor.add (noncecolor); buttoncoloraction = new Buttoncoloraction (); buttoncursor = new ButtonCursor () ; for (int i = 0; i < buttoncolor.length; i++) {Buttoncolor[i] = new Button (); Buttoncolor[i].setbackground (Color[i]); Buttoncolor[i].addactionlistener (buttoncoloraction); Buttoncolor[i].addmouselistener (buttonCursor); Panecolor.add (Buttoncolor[i]);} Pane.add (Panecolor);//Brush Color selector panel Panestroke = new PaNel (new GridLayout (1, 13));//control brush Style buttonstrokeaction = new Buttonstrokeaction (); button [] Buttonstroke = new Button[11];buttonstroke[0] = New button ("1"); buttonstroke[1] = New Button ("3"); buttonstroke[ 2] = New button ("5"); buttonstroke[3] = New button ("7"); buttonstroke[4] = New button ("9"); buttonstroke[5] = New button ("11" BUTTONSTROKE[6] = New button (""), buttonstroke[7] = New button ("" "), buttonstroke[8] = new button (" n "); Buttonstroke  [9] = new button (""); buttonstroke[10] = New button ("");d Rawwidth = new Label ("3", label.center);d rawcap = new Label ("", Label.center);d rawwidth.setbackground (Color.lightgray);d rawcap.setbackground (Color.lightgray);p Anestroke.add ( DrawWidth); for (int i = 0; i < buttonstroke.length; i++) {Panestroke.add (buttonstroke[i]); Buttonstroke[i]. Addmouselistener (buttoncursor); Buttonstroke[i].addactionlistener (buttonstrokeaction); if (I <= 8) {buttonStroke[ I].setname ("width");} Else{buttonstroke[i].setname ("cap");} if (i = = 8) {panestroke.add (DRAWCAP);}} Pane.add (PAnestroke);//Add the brush color selector to the form This.add (pane, Borderlayout.north);//Add the drawing board to the form This.add (palette);//join? Form Listener, Exit program This.addwindowlistener When the Close button is clicked (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}); /Set the form icon This.seticonimage (Toolkit.getdefaulttoolkit (). CreateImage ("Images/palette.png"));// Sets the size of the form this.setsize (new Dimension (400, 430));//sets the form position in the center of the screen This.setlocationrelativeto (null);// Show Form this.setvisible (true);} /** * Program Entry * * @param args * string array parameters */public static void Main (string[] args) {new UI ()} /** * Select the Listener event class for the color button * @author He qingsong * */class Buttoncoloraction implements ActionListener {public void actionperformed (Action Event e) {Color color_temp = ((Button) E.getsource ()). Getbackground (); Noncecolor.setbackground (color_temp); Palette.setcolor (color_temp);}} /** * Mouse Enter button transform cursor style listener event class * @author He qingsong * */class Buttoncursor extends Mouseadapter {public void mouseentered (MouseEvent e) {(Button) E.getsource ()). SetCursor (New Cursor (cursor.hand_cursor)); PubLIC void mouseexited (MouseEvent e) {(Button) E.getsource ()). SetCursor (New Cursor (Cursor.default_cursor));}} /** * Set the Listener event class for the brush * @author He Qingsong * */class Buttonstrokeaction implements ActionListener {public void actionperformed (Actione Vent e) {button button_temp = (button) E.getsource (); String name = Button_temp.getname (), if (Name.equalsignorecase ("width")) {Drawwidth.settext (Button_temp.getlabel ()); Palette.setstroke (Float.parsefloat (Button_temp.getlabel ()));} else if (name.equalsignorecase ("cap")) {Drawcap.settext (Button_temp.getlabel ())), if (Button_temp.getlabel (). Equals ( "")) {Palette.setstroke (basicstroke.cap_square);} else if (Button_temp.getlabel (). Equals ("")) {Palette.setstroke (basicstroke.cap_round);}}}} /** * Artboard Class * * @author He qingsong * */class Palette extends Panel implements MouseListener, Mousemotionlistener {//mouse X coordinate position PR ivate int mousex = 0;//last X coordinate position private int oldmousex = 0;//mouse y coordinate position private int mousey = 0;//last Y coordinate position private int o Ldmousey = 0;//drawing color Private color color = null;//Brush style private Basicstroke stroke = null;//Cached graphics private BufferedImage image = null;/** * Constructs an artboard class * */public Palette () {this. Addmouselistener (this); This.addmousemotionlistener (this);//default black brush color = new Color (0, 0, 0);//set default brush style stroke = new Basicstroke (3.0f, Basicstroke.cap_round, basicstroke.join_round);//Create an RGB cache image of 1280 * 1024 image = new BufferedImage (128 0, 1024x768, BUFFEREDIMAGE.TYPE_INT_RGB);//Set the color Image.getgraphics (). SetColor (Color.White);//Draw Background image.getgraphics (). FillRect (0, 0, 1280, 1024);} /** * Override Paint Drawing method */public void Paint (Graphics g) {super.paint (g);//convert to graphics2dgraphics2d g2d = (graphics2d) g;//get Cached graphics Graphics2dgraphics2d bg = image.creategraphics ();//Graphics anti-aliasing bg.setrenderinghint (renderinghints.key_antialiasing, RENDERINGHINTS.VALUE_ANTIALIAS_ON);//Set drawing color bg.setcolor (color);//Set Brush style bg.setstroke (stroke);//Draw line, From the previous point to the new Point Bg.drawline (Oldmousex, Oldmousey, MouseX, mousey);//Draw the cached graphic onto the artboard g2d.drawimage (image, 0, 0, this);} /** * Overriding the Update method */public void Update (Graphics g) {thIs.paint (g);} /** * @return Stroke */public Basicstroke Getstroke () {return stroke;} /** * @param stroke to set the stroke */public void Setstroke (Basicstroke stroke) {this.stroke = stroke;}  /** * Set Brush weight * @param width */public void setstroke (float width) {this.stroke = new Basicstroke (width, stroke.getendcap (), Stroke.getlinejoin ());} /** * Set the decoration of the nib point of the painting * @param cap Reference Java.awt.BasicStroke class static field */public void Setstroke (int cap) {This.stroke = new Basicstroke (Stroke.getlinewidth (), Cap, Stroke.getlinejoin ());} /** * @return Color */public color GetColor () {return color;} /** * @param color to set the color */public void setcolor (color color) {This.color = color;} public void mouseclicked (MouseEvent MouseEvent) {}/** * mouse pressed */public void mousepressed (MouseEvent MouseEvent) {THIS.OLDM Ousex = This.mousex = Mouseevent.getx (); This.oldmousey = This.mousey = Mouseevent.gety (); repaint ();} public void mousereleased (MouseEvent MouseEvent) {}/** * mouse enters chessboard */public void mouseentered (MouseEvent MouseEvent) {THIS.S Etcursor(New Cursor (Cursor.crosshair_cursor));} /** * Mouse exit checkerboard */public void mouseexited (MouseEvent MouseEvent) {this.setcursor (new Cursor (Cursor.default_cursor));} /** * Mouse drag */public void mousedragged (MouseEvent MouseEvent) {this.oldmousex = This.mousex;this.oldmousey = This.mousey;t His.mousex = Mouseevent.getx (); This.mousey = Mouseevent.gety (); repaint ();} public void mousemoved (MouseEvent MouseEvent) {}}}

Java drawing Board

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.