Drawing program __javaio of Javaswing graphic interface programming

Source: Internet
Author: User
Tags abs gety


Package three.day.frame;

Import java.awt.*;
Import java.awt.event.*;
Import java.awt.geom.*;
Import java.io.*;
Import java.util.*;
Import javax.swing.*;


Class Point1 implements Serializable {
int x, position of y;//mouse
Color col;//Paint Selected colors
int tool;//Paint Select which graphic to draw
int boarder;//line width


Point1 (int x, int y, Color col, int tool, int boarder) {
this.x = x;
This.y = y;
This.col = col;
This.tool = tool;
This.boarder = boarder;
}
}


Class Paintboard1 extends Frame implements ActionListener, Mousemotionlistener,
MouseListener, ItemListener {
int x =-1, y = -1;//Initialize mouse position
int con = 1;//brush size
int Econ = 5;//Eraser Size


int toolflag = 0;//toolflag: Tool tag
Toolflag tool corresponding Table:
(0--brush); (1--eraser); (2--clear);
(3--line); (4--circle); (5--rectangle);


Color c = new color (0, 0, 0); Brush color
Basicstroke size = new Basicstroke (Con, Basicstroke.cap_butt,
Basicstroke.join_bevel);//Brush thickness
Point1 Cutflag = new Point1 ( -1,-1, C, 6, con);/TRUNCATE flag


Vector paintinfo = null;//point Information Vectors Group
int n = 1;


FileInputStream picin = null;
FileOutputStream picout = null;


ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;


* Tool Panel--brushes, lines, circles, rectangles, polygons, erasers, clear/
Panel Toolpanel;
Button Eraser, Drline, drcircle, Drrect;
Button Clear, pen;
Choice Colchoice, Sizechoice, Eraserchoice;
Button Colchooser;
Label color, size b, size E;
Save feature
Button Openpic, Savepic;
FileDialog openpicture, SavePicture;


Paintboard1 (String s) {
Super (s);
Addmousemotionlistener (this);
Addmouselistener (this);


Paintinfo = new Vector ();


/* All tools buttons and options * *
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 ("1");
Sizechoice.add ("3");
Sizechoice.add ("5");
Sizechoice.add ("7");
Sizechoice.add ("9");
Sizechoice.additemlistener (this);
Eraser Size 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 ("purge");
Eraser = New button ("Eraser");
Pen = New button ("brush");
Drline = New button ("Draw straight line");
drcircle = New button ("Draw Round");
Drrect = New button ("Draw rectangle");


Openpic = New button ("Open drawing");
Savepic = New button ("Save Picture");


Colchooser = New button ("Display palette");


Individual component Event Monitoring
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 ("Eraser Size", label.center);
Panel Add component
Toolpanel.add (Openpic);
Toolpanel.add (Savepic);


Toolpanel.add (pen);
Toolpanel.add (Drline);
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);
Tool Panel to Applet panel
Add (Toolpanel, Borderlayout.north);


SetBounds (230, 50, 900, 650);
SetVisible (TRUE);
Validate ();
dialog for Save and load


Openpicture = new FileDialog (This, "open drawing", filedialog.load);
Openpicture.setvisible (FALSE);
SavePicture = new FileDialog (this, "Save Picture", 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 windowclosing (WindowEvent e) {
System.exit (0);
}
});


}


public void Paint (Graphics g) {
System.out.println ("Paint");
Graphics2D g2d = (graphics2d) g;


Point1 P1, p2;


n = paintinfo.size ();


if (Toolflag = 2)
G2d.clearrect (0, 0, GetSize (). width-100, GetSize (). height-100);/clear


for (int i = 0; i < n-1; i++) {
P1 = (Point1) paintinfo.elementat (i);
P2 = (Point1) paintinfo.elementat (i + 1);
size = new Basicstroke (P1.boarder, Basicstroke.cap_butt,
Basicstroke.join_bevel);


G2d.setcolor (P1.col);
G2d.setstroke (size);


if (P1.tool = = P2.tool) {
Switch (p1.tool) {
Case 0://Brushes


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://draw a straight line
Line2D line2 = new Line2d.double (p1.x, P1.y, p2.x, P2.Y);
G2d.draw (line2);
Break


Case 4://Draw Circle
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 a rectangle
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, skipped
i = i + 1;
Break


Default
}//End Switch
}//End If
}//End for
}


public void itemstatechanged (ItemEvent e) {
System.out.println ("itemstatechanged");
if (e.getsource () = = colchoice)/Preselection color
{
String name = Colchoice.getselecteditem ();


if (name = = "Black") {
c = new Color (0, 0, 0);
else if (name = = "Red") {
c = new Color (255, 0, 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,
Basicstroke.join_bevel);


else if (selected = "5") {
con = 5;
size = new Basicstroke (Con, Basicstroke.cap_butt,
Basicstroke.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)//Eraser Size
{
String esize = Eraserchoice.getselecteditem ();


if (esize = = "5") {
Econ = 5 * 2;
else if (esize = "9") {
Econ = 9 * 2;
else if (esize = "13") {
Econ = 13 * 2;
else if (esize = "17") {
Econ = 17 * 3;
}


}


}


public void mousedragged (MouseEvent e) {
System.out.println ("mousedragged");
Point1 P1;
Switch (Toolflag) {
Case 0://Brushes
x = (int) e.getx ();
y = (int) e.gety ();
P1 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p1);
Repaint ();
Break


Case 1://Eraser
x = (int) e.getx ();
y = (int) e.gety ();
P1 = new Point1 (x, y, NULL, Toolflag, Econ);
Paintinfo.addelement (p1);
Repaint ();
Break


Default
}
}


public void mousemoved (MouseEvent e) {
}


public void Update (Graphics g) {
SYSTEM.OUT.PRINTLN ("Update");
Paint (g);
}


public void mousepressed (MouseEvent e) {
System.out.println ("mousepressed");
Point1 P2;
Switch (Toolflag) {
Case 3://Line
x = (int) e.getx ();
y = (int) e.gety ();
P2 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p2);
Break


Case 4://Round
x = (int) e.getx ();
y = (int) e.gety ();
P2 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p2);
Break


Case 5://Rectangle
x = (int) e.getx ();
y = (int) e.gety ();
P2 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p2);
Break


Default
}
}


public void mousereleased (MouseEvent e) {
System.out.println ("mousereleased");
Point1 P3;
Switch (Toolflag) {
Case 0://brushes
Paintinfo.addelement (Cutflag);
Break


Case 1://Eraser
Paintinfo.addelement (Cutflag);
Break


Case 3://Straight Line
x = (int) e.getx ();
y = (int) e.gety ();
P3 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p3);
Paintinfo.addelement (Cutflag);
Repaint ();
Break


Case 4://Round
x = (int) e.getx ();
y = (int) e.gety ();
P3 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.addelement (p3);
Paintinfo.addelement (Cutflag);
Repaint ();
Break


Case 5://Rectangle
x = (int) e.getx ();
y = (int) e.gety ();
P3 = new Point1 (x, Y, C, toolflag, con);
Paintinfo.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) {
System.out.println ("actionperformed");
if (e.getsource () = = pen)//Brush
{
System.out.println ("pen");
Toolflag = 0;
}


if (e.getsource () = = eraser)//Eraser
{
System.out.println ("Eraser");
Toolflag = 1;
}


if (e.getsource () = clear)//purge
{
System.out.println ("clear");
Toolflag = 2;
Paintinfo.removeallelements ();
Repaint ();
}


if (e.getsource () = = Drline)//Draw line
{
System.out.println ("Drline");
Toolflag = 3;
}


if (e.getsource () = = drcircle)//Draw Circle
{
System.out.println ("drcircle");
Toolflag = 4;
}


if (e.getsource () = = Drrect)//Draw a rectangle
{
System.out.println ("Drrect");
Toolflag = 5;
}


if (e.getsource () = = Colchooser)//Palette
{
System.out.println ("Colchooser");
Color Newcolor = Jcolorchooser.showdialog (This, "My palette", C);
c = Newcolor;
}


if (e.getsource () = = openpic)//Open 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 Picture
{
Savep

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.