Java small section (application)

xiaoxiao2021-03-05  24

In 2004, I have been in Java for a period of time. During the period, the most complete write is the Java small discipline program, involving the mouse event, drawing function, file stream, file opening and saving.

// Powered by CompowerImport Java.applet. *; import java.awt. *; import java.awt.event. *; import java.util. *; import javax.swing. *; import java.awt.geom. *; Import java.io. *;

Class Point Implements Serializable {Int x, Y; Color Colories Int Tool; Int Boarder;

Point (int X, int y, color color {this.x = x; THIS.Y = Y; this.col = col; this.tool = Tool; this.boarder = boarder;}}

Class Paintboard Extends Frame Implements ActionListener, MouseMotionListener, MouseListener, ItemListener {INT X = -1, Y = -1; int CON = 1; // Brush size int ECON = 5; // Eraser Size

INT Toolflag = 0; // Toolflag: Tool Tag // Toolflag Tool Correspondeal: // (0 - Brush); (1 - Eraser); // (3 - Straight); (4 - circle); (5 - rectangular);

Color C = New Color (0, 0); // Brush Color Basicstroke Size = New Basicstroke (Con, Basicstroke.cap_butt, Basicstroke.Join_Bevel); // Brush thick Point Cutflag = New Point (-1, -1, C, 6, con); // truncated sign

Vector Painfo = NULL; // Point Information Vector Group INT N = 1;

FileInputStream Picin = NULL; FileoutputStream Picout = NULL; ObjectInputStream VIN = NULL; ObjectOutputStream Vout = NULL;

// * Tools panel - brush, straight line, circle, rectangular, polygon, rubber, Clear * / Panel Toolpanel, DRCIRCLE, DRECT; Button Clear, Pen; Choice Colchoice, Sizechoice, EraseRchoice; Button Colchooser; Label Color, size B, size E; // Save function Button OpenPIC, SavePic; FileDialog OpenPicture, SavePicture

Paintboard (String s) {super (s); addmousemotionListener (this); addMouseListener (this);

Paintinfo = new vector ();

/ * Each tool button and selection * / // Color selection colchoice = new choice (); colchoice.add ("black"); colchoice.add ("red"); colchoice.add ("blue"); colchoice. Add ("Green"); colchoice.additemlistener (this); // Brush size selection sizechoice = new choice (); sizechoice.add ("3"); sizechoice.add ("5 "); Sizechoice.add (" 7 "); Sizechoice.Add (" 9 "); Sizechoice.Additemlistener (this); // Eraser Selection Eraserchoice = New Choice (); EraSerchoice.Add (" 5 "); ERASERCHOICE .add ("9"); EraSerchoice.Add ("13"); ERASERCHOICE.ADD ("17"); ERASERCHOICE.Additemlistener (this); Toolpanel = New Panel ();

Clear = New Button; ERASER = New Button; Pen = New Button; DRLINE = New Button; DRCIRCLE = New Button ("Discircum "); Drrect = New Button (" Draw Rectangle ");

OpenPIC = New Button ("Open Drawing"); SavePic = New Button; Colchooser = New Button ("Show Toner Board");

// each component event listeners clear.addActionListener (this); eraser.addActionListener (this); pen.addActionListener (this); drLine.addActionListener (this); drCircle.addActionListener (this); drRect.addActionListener (this); openPic. AddActionListener (this); SavePic.AddActionListener (this); Colchooser.AddActionListener (this);

Color = New Label ("Brush Color", Label.center; Size B = New Label ("Brush Size", Label.center; Size E = New Label ("Rubber Size", Label.center; // Panel Add component Toolpanel.Add (OpenPIC); Toolpanel.Add (SavePic); Toolpanel.Add (Pen); Toolpanel.Add (DRCIRCLE); Toolpanel.Add (DrRECT);

Toolpanel.add (color); Toolpanel.add (colchoice); Toolpanel.Add (Size B); Toolpanel.Add (Sizechoice); Toolpanel.Add (Colchooser); Toolpanel.Add (ERASER); Toolpanel.Add (Size E) ; Toolpanel.Add (EraSerchoice);

Toolpanel.add (Clear); // Tools Panel to Applet Panel Add (Toolpanel, BorderLayout.North);

SetBounds (60, 60, 900, 600); setvisible (TRUE); validate (); // Dialog for Save and Load

OpenPicture = New FileDialog (this, "Open Drawing", FileDialog.Load; OpenPicture.setVisible (False); SavePicture = New FileDialog (this, "Save Drawings", FileDialog.Save; SavePicture.setVisible (false);

OpenPicture.AddWindowListener (new windowadapter () {public void windowclosing (windowEvent e) {OpenPicture.setVisible (false);}});

SavePicture.addwindowListener (New Windowadapter () {public void windowclosing (windowevent e) {savePicture.setVisible (false);}});

AddwindowListener (New Windowadapter () {public void window {system.exit (0);}})

Public void Paint (Graphics G) {Graphics2D G2D = (Graphics2D) g;

POINT P1, P2;

N = paintinfo.size (); if (Toolflag == 2) g.clearRect (0, 0, getSize (). width, getsize (). height); // Clear

For (int i = 0; i

g2d.setcolor (p1.col); g2d.setstroke (size);

IF (p1.tool == p2.tool) {switch (p1.tool) {case 0: // brush

Line2D line1 = new line2d.double (p1.x, p1.y, p2.x, p2.y); g2d.draw (line1); Break;

Case 1: // Eraser G. ClearRect (p1.x, p1.y, p1.boarder, p1.boarder; Break;

Case 3: // Painted line2d line2 = new line2d.double (p1.x, p1.y, p2.x, p2.y); g2d.draw (line2); Break; case 4: // Painting Ellipse2D Ellipse = New Ellipse2d.double (p1.x, p1.y, math.abs (p2.x-p1.x), Math.Abs ​​(p2.y-p1.y)); g2d.draw (Ellipse); Break;

Case 5: // Draw Rectangle2d Rect = New Rectangle2d.double (p1.x, p1.y, math.abs (p2.x-p1.x), Math.Abs ​​(p2.y-p1.y)); g2d.draw (rect); Break;

Case 6: // truncated, skip I = i 1; Break;

DEFAULT:} // end} // end for} // end for}

Public Void ItemStateChanged (ITEMEVENT E) {if (E.GetSource () == Colchoice) // Pre-select Color {String Name = Colchoice.getSelectedItem (); if (Name == "Black") {c = new color (0, 0,0);} else if (name == "red") {c = new color (255, 0);} else if (name == "green") {c = new color (0,255,0) } Else if (name == "blue") {c = new color (0,0,255);}} else if (E.Getsource () == sizechoice) // brush size {string selected = sizechoice.getSelectedItem () ; If (SELECTED == "1") {con = 1; size = new basicstroke (con, basicstroke.cap_butt, basicstroke.join_bevel);

} Else if (SELECTED == "3") {con = 3; size = new basicstroke (con, basicstroke.cap_butt, baricstroke.join_bevel);

} Else if (SELECTED == "5") {con = 5; size = new basicstroke (con, basicstroke.cap_butt, baricstroke.join_bevel);

} Else IF (SELECTED == "7") {con = 7; size = new basicstroke (con, basicstroke.cap_butt, basicstroke.join_bevel);

} Else if (SELECTED == "9") {con = 9; size = new basicstroke (con, basicstroke.cap_butt, basicstroke.join_bevel);}} else if (E.Getsource () == Eraserchoice) // Rubber Size {String Esize = EraSerchoice.getSelectedItem ();

IF (eSize == "5") {ECON = 5 * 2;} else = f (eSize == "9") {ECON = 9 * 2;} else} (eSize == "13") {ECON = 13 * 2;} else f (eSize == "17") {ECON = 17 * 3;}

}

}

Public void mousedragged (mouseevent e) {Point P1; switch (toolflag) {case 0: // brush x = (int) E.GETX (); y = (int) E.GETY (); p1 = new point (x , Y, C, Toolflag, Con); Painfo.addeElement (P1); repaint (); Break;

Case 1: // Rubber x = (int) E.GETX (); y = (int) E.GETY (); p1 = new point (x, y, null, toolflag, econ); Painfo.addeElement (P1) Repaint (); BREAK;

Default:}}

Public void mousemoved (mouseevent e) {}

Public void Update (graphics g) {Paint (g);

Public void mousepressed (MouseEvent E) {Point P2; Switch (Toolflag) {Case 3: // Linex = (int) E.GETX (); y = (int) E.GETY (); p2 = new point (x , Y, C, Toolflag, Con); Paintinfo.addeElement (P2); Break;

Case 4: // Circle x = (int) E.GETX (); y = (int) E.GETY (); p2 = new point (x, y, c, toolflag, con); Painfo.addelement (P2) ;

Case 5: // Rectangular X = (int) E.GETX (); y = (int) E.GETY (); p2 = new point (x, y, c, toolflag, con); Painfo.addeElement (P2) ;

Default:}}

Public void mousereleased (MouseEvent E) {Point P3; Switch (Toolflag) {Case 0: // Brush Painfo.addeElement (cutflag); Break;

Case 1: // Eraser Painfo.addeElement (CUTFLAG); Break; Case 3: // Straight line x = (int) E.GETX (); y = (int) E.GETY (); p3 = new point (x, Y, C, Toolflag, Con); Painfo.addelement (P3); Painfo.addeElement (CUTFLAG); repaint ();

Case 4: // Circle x = (int) E.GETX (); y = (int) E.Gety (); p3 = new point (x, y, c, toolflag, con); Painfo.addelement (P3) Paintinfo.addelement (cutflag); repaint (); Break;

Case 5: // Rectangular X = (int) E.GETX (); y = (int) E.GETY (); p3 = new point (x, y, c, toolflag, con); Painfo.addelement (P3) Paintinfo.addelement (cutflag); repaint (); Break;

Default:}}

Public void mouseentered (mouseevent e) {}

Public void mouseexited (mouseevent e) {}

Public void mouseclicked (mouseevent e) {}

Public void actionperformed (ActionEvent E) {IF (E.Getsource () == PEN) // Brush {Toolflag = 0;}

IF (E.GetSource () == Eraser) // Rubber {Toolflag = 1;} IF (E.GetSource () == CLEAR) // Clear {Toolflag = 2; Paintinfo.removeLlelements (); repaint ();} IF (E.getsource () == DRLINE) // Picture {Toolflag = 3;}

IF (E.getsource () == DRCIRCLE) // Painted round {Toolflag = 4;}

IF (E.getsource () == DrRECT) // painted rectangle {Toolflag = 5;}

IF (E.GetSource () == colchooser) // Toner {color newcolor = jcolorchooser.showdialog (this, "palette", c); c = newcolor;}

IF (E.GetSource () == OpenPIC) // Opened Drawing {OpenPicture.setVisible (TRUE); if (OpenPicture.GetFile ()! = null) {Int Tempflag; TEMPFLAG = Toolflag; Toolflag = 2; repaint ();

try {paintInfo.removeAllElements (); File filein = new File (openPicture.getDirectory (), openPicture.getFile ()); picIn = new FileInputStream (filein); VIn = new ObjectInputStream (picIn); paintInfo = (Vector) VIn. ReadObject (); vin.close (); repaint (); Toolflag = Tempflag;

catch (ClassNotFoundException IOe2) {repaint (); toolFlag = tempflag; System.out.println ( "can not read object");} catch (IOException IOe) {repaint (); toolFlag = tempflag; System.out.println ( " CAN not read file ");}}

}

if (e.getSource () == savePic) // save the picture {savePicture.setVisible (true); try {File fileout = new File (savePicture.getDirectory (), savePicture.getFile ()); picOut = new FileOutputStream (fileout VOUT = New ObjectOutputStream (PICOUT); Vout.writeObject (Paintinfo); Vout.close ();} catch (ioException ie) {system.out.println ("can not write object");}

}}} // End Paintboard

Public class pb {public static void main (string args []) {new Paintboard ("Draw Program");}}

转载请注明原文地址:https://www.9cbs.com/read-36310.html

New Post(0)