只提指导,没有源码。
在 Java 中要自定义组件,一般是覆盖掉 protected void paintComponent(Graphics g); 方法就可以了,对于你这个类似画笔的程序,首先,整个画布是一个自定义的继承自像 JPanel 一样的东西,不过我们需要覆盖它的 paintComponent 方法,因为:一、我们需要在当鼠标拖放一个东西时我们移动这个选中的图形时记住它的位置;二、设定 label 时需要保存它的 Label。三、知道图形的形状。
,然后在 paintComponent 时依次画出各个图形来。
要绘图:
1、直线,gdrawLine(x,y,x2,y2); // 参数分别是起止点坐标。
2、矩形,gfillRect(x, y, w, h); // 参数分别是左上角坐标和宽及高。
3、椭圆,gfillOval(x, y, w, h); // 参数分别是椭圆形的外切矩形的左上角坐标及宽和高,当w 和 h 相等时是个圆。
准备绘图前 gsetColor() 设置前景色;先把整个画布用 gfillRect() 涂成白色,再分别画各个图形,最后绘制 Label 应该在画图形之后再做。
移动图形,是给 画布组件 addMouseMotionListener 来监听事件的,在拖动时先通过 mouseDragged 事件的 MouseEventpoint 知道它的位置是在哪个图形的内部,之后的移动就修改这个图形的位置。
java创建图形 用的是
javaawtGrahpic
这个类完成的 包括 自己绘制 使用现成的 jpg格式的文件什么的
具体内容太多了自己看帮助把
----------------------------------------------------
继承
JPanel 类 并且从写 paintComponent 方法 里边有画图的方法
注意这个 Graphics 的对象 画图主要就用这个 需要程序自己去调用自己不能调用 具体的画法 也都是这个类里的方法你自己去看看把
Graphics 是 javaawt包里的类
class A
{
JFrame frame;
public static void main(String[] args)
{
A a=new A();
ago();
}
public void go()
{
frame =new JFrame();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
MyDrawPanel drawPanel =new MyDrawPanel();
framegetContentPane()add(BorderLayoutCENTER,drawPanel);
framesetSize(300,300);
framesetVisible(true);
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
gsetColor(Colorred);
gfillOval(0,0,thisgetWidth(),thisgetHeight());
}
}
}
首先,如果你只是要实现电子时钟,根本就不需要用到多线程。
如果你真的是要使用,就新建一个类,实现Runnable接口就是了。
如:
class MyThread1 implements Runnable{
public MyThread(){
}
public void run(){
}
}
使用的时候,就:
Thread myThread=new Thread(new MyThread());
myThreadstart();
如果要画图,你就直接把组件通过构造方法传到MyThread中就是了
////保存一个pbjava文件直接编译执行
import javaapplet;
import javaawt;
import javaawtevent;
import javautil;
import javaxswing;
import javaawtgeom;
import javaio;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
thisx = x;
thisy = y;
thiscol = col;
thistool = tool;
thisboarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// 工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/各工具按钮及选择项/
//颜色选择
ColChoice = new Choice();
ColChoiceadd("black");
ColChoiceadd("red");
ColChoiceadd("blue");
ColChoiceadd("green");
ColChoiceaddItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoiceadd("1");
SizeChoiceadd("3");
SizeChoiceadd("5");
SizeChoiceadd("7");
SizeChoiceadd("9");
SizeChoiceaddItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoiceadd("5");
EraserChoiceadd("9");
EraserChoiceadd("13");
EraserChoiceadd("17");
EraserChoiceaddItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clearaddActionListener(this);
eraseraddActionListener(this);
penaddActionListener(this);
drLineaddActionListener(this);
drCircleaddActionListener(this);
drRectaddActionListener(this);
openPicaddActionListener(this);
savePicaddActionListener(this);
colchooseraddActionListener(this);
颜色 = new Label("画笔颜色",LabelCENTER);
大小B = new Label("画笔大小",LabelCENTER);
大小E = new Label("橡皮大小",LabelCENTER);
//面板添加组件
toolPaneladd(openPic);
toolPaneladd(savePic);
toolPaneladd(pen);
toolPaneladd(drLine);
toolPaneladd(drCircle);
toolPaneladd(drRect);
toolPaneladd(颜色); toolPaneladd(ColChoice);
toolPaneladd(大小B); toolPaneladd(SizeChoice);
toolPaneladd(colchooser);
toolPaneladd(eraser);
toolPaneladd(大小E); toolPaneladd(EraserChoice);
toolPaneladd(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayoutNORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialogLOAD);
openPicturesetVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialogSAVE);
savePicturesetVisible(false);
openPictureaddWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicturesetVisible(false); }
});
savePictureaddWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicturesetVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ Systemexit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfosize();
if(toolFlag==2)
gclearRect(0,0,getSize()width,getSize()height);//清除
for(int i=0; i<n ;i++){
p1 = (Point)paintInfoelementAt(i);
p2 = (Point)paintInfoelementAt(i+1);
size = new BasicStroke(p1boarder,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
g2dsetColor(p1col);
g2dsetStroke(size);
if(p1tool==p2tool)
{
switch(p1tool)
{
case 0://画笔
Line2D line1 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line1);
break;
case 1://橡皮
gclearRect(p1x, p1y, p1boarder, p1boarder);
break;
case 3://画直线
Line2D line2 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(egetSource()==ColChoice)//预选颜色
{
String name = ColChoicegetSelectedItem();
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(egetSource()==SizeChoice)//画笔大小
{
String selected = SizeChoicegetSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
}
else if(egetSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoicegetSelectedItem();
if(Esize=="5")
{ Econ = 52; }
else if(Esize=="9")
{ Econ = 92; }
else if(Esize=="13")
{ Econ = 132; }
else if(Esize=="17")
{ Econ = 173; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p1);
repaint();
break;
case 1://橡皮
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfoaddElement(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://直线
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfoaddElement(cutflag);
break;
case 1: //eraser
paintInfoaddElement(cutflag);
break;
case 3://直线
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(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(egetSource()==pen)//画笔
{toolFlag = 0;}
if(egetSource()==eraser)//橡皮
{toolFlag = 1;}
if(egetSource()==clear)//清除
{
toolFlag = 2;
paintInforemoveAllElements();
repaint();
}
if(egetSource()==drLine)//画线
{toolFlag = 3;}
if(egetSource()==drCircle)//画圆
{toolFlag = 4;}
if(egetSource()==drRect)//画矩形
{toolFlag = 5;}
if(egetSource()==colchooser)//调色板
{
Color newColor = JColorChoosershowDialog(this,"调色板",c);
c = newColor;
}
if(egetSource()==openPic)//打开图画
{
openPicturesetVisible(true);
if(openPicturegetFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInforemoveAllElements();
File filein = new File(openPicturegetDirectory(),openPicturegetFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VInreadObject();
VInclose();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read file");
}
}
}
if(egetSource()==savePic)//保存图画
{
savePicturesetVisible(true);
try{
File fileout = new File(savePicturegetDirectory(),savePicturegetFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOutwriteObject(paintInfo);
VOutclose();
}
catch(IOException IOe)
{
Systemoutprintln("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
import javaapplet;
import javaawt;
import javaawtevent;
import javautil;
import javaxswing;
import javaawtgeom;
import javaio;
import javasqlConnection;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
thisx = x;
thisy = y;
thiscol = col;
thistool = tool;
thisboarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// 工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/各工具按钮及选择项/
//颜色选择
ColChoice = new Choice();
ColChoiceadd("black");
ColChoiceadd("red");
ColChoiceadd("blue");
ColChoiceadd("green");
ColChoiceaddItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoiceadd("1");
SizeChoiceadd("3");
SizeChoiceadd("5");
SizeChoiceadd("7");
SizeChoiceadd("9");
SizeChoiceaddItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoiceadd("5");
EraserChoiceadd("9");
EraserChoiceadd("13");
EraserChoiceadd("17");
EraserChoiceaddItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clearaddActionListener(this);
eraseraddActionListener(this);
penaddActionListener(this);
drLineaddActionListener(this);
drCircleaddActionListener(this);
drRectaddActionListener(this);
openPicaddActionListener(this);
savePicaddActionListener(this);
colchooseraddActionListener(this);
颜色 = new Label("画笔颜色",LabelCENTER);
大小B = new Label("画笔大小",LabelCENTER);
大小E = new Label("橡皮大小",LabelCENTER);
//面板添加组件
toolPaneladd(openPic);
toolPaneladd(savePic);
toolPaneladd(pen);
toolPaneladd(drLine);
toolPaneladd(drCircle);
toolPaneladd(drRect);
toolPaneladd(颜色); toolPaneladd(ColChoice);
toolPaneladd(大小B); toolPaneladd(SizeChoice);
toolPaneladd(colchooser);
toolPaneladd(eraser);
toolPaneladd(大小E); toolPaneladd(EraserChoice);
toolPaneladd(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayoutNORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialogLOAD);
openPicturesetVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialogSAVE);
savePicturesetVisible(false);
openPictureaddWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicturesetVisible(false); }
});
savePictureaddWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{ savePicturesetVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ Systemexit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfosize();
if(toolFlag==2)
gclearRect(0,0,getSize()width,getSize()height);//清除
for(int i=0; i<n ;i++){
p1 = (Point)paintInfoelementAt(i);
p2 = (Point)paintInfoelementAt(i+1);
size = new BasicStroke(p1boarder,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
g2dsetColor(p1col);
g2dsetStroke(size);
if(p1tool==p2tool)
{
switch(p1tool)
{
case 0://画笔
Line2D line1 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line1);
break;
case 1://橡皮
gclearRect(p1x, p1y, p1boarder, p1boarder);
break;
case 3://画直线
Line2D line2 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(egetSource()==ColChoice)//预选颜色
{
String name = ColChoicegetSelectedItem();
switch (name) {
case "black":
c = new Color(0,0,0);
break;
case "red":
c = new Color(255,0,0);
break;
case "green":
c = new Color(0,255,0);
break;
case "blue":
c = new Color(0,0,255);
break;
}
}
else if(egetSource()==SizeChoice)//画笔大小
{
String selected = SizeChoicegetSelectedItem();
switch (selected) {
case "1":
con = 1;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
break;
case "3":
con = 3;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
break;
case "5":
con = 5;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
break;
case "7":
con = 7;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
break;
case "9":
con = 9;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
break;
}
}
else if(egetSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoicegetSelectedItem();
if(Esize=="5")
{ Econ = 52; }
else if(Esize=="9")
{ Econ = 92; }
else if(Esize=="13")
{ Econ = 132; }
else if(Esize=="17")
{ Econ = 173; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p1);
repaint();
break;
case 1://橡皮
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfoaddElement(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://直线
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfoaddElement(cutflag);
break;
case 1: //eraser
paintInfoaddElement(cutflag);
break;
case 3://直线
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(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(egetSource()==pen)//画笔
{toolFlag = 0;}
if(egetSource()==eraser)//橡皮
{toolFlag = 1;}
if(egetSource()==clear)//清除
{
toolFlag = 2;
paintInforemoveAllElements();
repaint();
}
if(egetSource()==drLine)//画线
{toolFlag = 3;}
if(egetSource()==drCircle)//画圆
{toolFlag = 4;}
if(egetSource()==drRect)//画矩形
{toolFlag = 5;}
if(egetSource()==colchooser)//调色板
{
Color newColor = JColorChoosershowDialog(this,"调色板",c);
c = newColor;
}
if(egetSource()==openPic)//打开图画
{
openPicturesetVisible(true);
if(openPicturegetFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInforemoveAllElements();
File filein = new File(openPicturegetDirectory(),openPicturegetFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VInreadObject();
VInclose();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read file");
}
}
}
if(egetSource()==savePic)//保存图画
{
savePicturesetVisible(true);
try{
File fileout = new File(savePicturegetDirectory(),savePicturegetFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOutwriteObject(paintInfo);
VOutclose();
}
catch(IOException IOe)
{
Systemoutprintln("can not write object");
}
}
}
}//end paintboard
public class Pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
public enum ShapeTypes {
LINE, CIRCLE, RECTANGLE
}public interface Shape {
void paint(Graphics g);
}public class Rectangle implements Shape {
// 矩形左上角的坐标
private int x, y;
// 矩形的宽度和高度
private int width, height;
private Color rectangleColor;
public Rectangle() {
super();
}
public Rectangle(int x, int y, int width, int height, Color rectangleColor) {
super();
thisx = x;
thisy = y;
thiswidth = width;
thisheight = height;
thisrectangleColor = rectangleColor;
}
@Override
public void paint(Graphics g) {
gsetColor(rectangleColor);
gdrawRect(x, y, width, height);
}
}public class Line implements Shape {
// 直线的起始位置
private int x1, y1;
// 直线的终止位置
private int x2, y2;
private Color lineColor;
public Line(int x1, int y1, int x2, int y2, Color lineColor) {
super();
thisx1 = x1;
thisy1 = y1;
thisx2 = x2;
thisy2 = y2;
thislineColor = lineColor;
}
public Line() {
super();
}
@Override
public void paint(Graphics g) {
gsetColor(lineColor);
gdrawLine(x1, y1, x2, y2);
}
}public class Circle implements Shape {
// 圆的颜色
private Color circleColor;
// 圆心的坐标
private int x, y;
// 圆的半径
private int radius;
public Circle() {
super();
}
public Circle(int x, int y, int radius, Color circleColor) {
super();
thiscircleColor = circleColor;
thisx = x;
thisy = y;
thisradius = radius;
}
@Override
public void paint(Graphics g) {
gsetColor(circleColor);
// 画弧, 当弧的宽度和高度一致且从0~360度时就是原形了
gdrawArc(x, y, radius, radius, 0, 360);
}
}public class SketchpadPanel extends Canvas implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = -5229161042153132522L;
// 鼠标点击起始坐标和当前坐标
private int beginX = 0, beginY = 0, currentX = 0, currentY = 0;
// 判断鼠标是否被按下
private boolean isMousePressing = false;
// 保存当前的图形, 在撤销和恢复时使用
private final Stack<Shape> currentShapes = new Stack<Shape>();
// 保存已经删除过的图形
private final Stack<Shape> deletedShapes = new Stack<Shape>();
private ShapeTypes type;
private Color color;
public SketchpadPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
/
撤销方法
/
public void undo() {
if (currentShapessize() > 0) {
// 从所有保存过的图形中取出最后一个, 放入到已删除的图形中去
Shape shape = currentShapespop();
deletedShapespush(shape);
repaint();
}
}
/
恢复撤销方法
/
public void redo() {
if (deletedShapessize() > 0) {
// 从所有删除的图形中取出最后一个, 放入保存的图形中
Shape shape = deletedShapespop();
currentShapespush(shape);
repaint();
}
}
/
设置命令
@param type
/
public void setShapeType(ShapeTypes type) {
thistype = type;
}
/
设置颜色
@param color
/
public void setColor(Color color) {
thiscolor = color;
}
public void updete(Graphics g) {
paint(g);
}
/
绘制画板
/
@Override
public void paint(Graphics g) {
// 绘制画板
Dimension size = getSize();
int width = sizewidth;
int height = sizeheight;
gsetColor(ColorWHITE);
gfillRect(0, 0, width, height);
// 绘制所有图形
Shape shape = null;
Enumeration<Shape> e = currentShapeselements();
while (ehasMoreElements()) {
shape = enextElement();
shapepaint(g);
}
// 如果当前鼠标没有释放
if (isMousePressing) {
gsetColor(color);
switch (type) {
// 绘制直线
case LINE:
gdrawLine(beginX, beginY, currentX, currentY);
break;
// 绘制矩形
case RECTANGLE:
if (currentX < beginX) {
if (currentY < beginY) {
// 如果当前位置在起始位置的左上方, 则以鼠标当前位置为矩形的左上角位置
gdrawRect(currentX, currentY, beginX - currentX, beginY - currentY);
} else {
// 如果当前位置在起始位置的左下方, 则以鼠标当前位置的横坐标和起始位置的纵坐标作为矩形的左上角位置
gdrawRect(currentX, beginY, beginX - currentX, currentY - beginY);
}
} else {
if (currentY < beginY) {
// 如果当前位置在起始位置的右上方, 则以鼠标起始位置的很坐标和当前位置的纵坐标作为矩形的左上角位置
gdrawRect(beginX, currentY, currentX - beginX, beginY - currentY);
} else {
// 如果当前位置在起始位置的右下方, 则已起始位置作为矩形的左上叫位置
gdrawRect(beginX, beginY, currentX - beginX, currentY - beginY);
}
}
break;
// 绘制圆形
case CIRCLE:
// 半径为aa + bb的平方根
int radius = (int) Math
sqrt((beginX - currentX) (beginX - currentX) + (beginY - currentY) (beginY - currentY));
gdrawArc(beginX - radius / 2, beginY - radius / 2, radius, radius, 0, 360);
break;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
/
当鼠标按下的时候获得起始坐标
/
@Override
public void mousePressed(MouseEvent e) {
beginX = egetX();
beginY = egetY();
isMousePressing = true;
}
/
当鼠标释放时获得当前坐标
/
@Override
public void mouseReleased(MouseEvent e) {
currentX = egetX();
currentY = egetY();
isMousePressing = false;
// 当释放鼠标时, 将绘制的图形保存到shapes中
switch (type) {
// 绘制直线
case LINE:
Line line = new Line(beginX, beginY, currentX, currentY, color);
currentShapespush(line);
break;
// 绘制圆形
case CIRCLE:
// 半径为aa + bb的平方根
int radius = (int) Math
sqrt((beginX - currentX) (beginX - currentX) + (beginY - currentY) (beginY - currentY));
Circle circle = new Circle(beginX - radius / 2, beginY - radius / 2, radius, color);
currentShapespush(circle);
break;
// 绘制矩形
case RECTANGLE:
Rectangle rectangle = null;
if (currentX < beginX) {
if (currentY < beginY) {
rectangle = new Rectangle(currentX, currentY, beginX - currentX, beginY - currentY, color);
} else {
rectangle = new Rectangle(currentX, beginY, beginX - currentX, currentY - beginY, color);
}
} else {
if (currentY < beginY) {
rectangle = new Rectangle(beginX, currentY, currentX - beginX, beginY - currentY, color);
} else {
rectangle = new Rectangle(beginX, beginY, currentX - beginX, currentY - beginY, color);
}
}
currentShapespush(rectangle);
break;
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currentX = egetX();
currentY = egetY();
thisrepaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}public class SketchpadFrame extends JFrame {
private static final long serialVersionUID = -7080053971741609904L;
private final JPanel commandPanel = new JPanel(); // 存放命令的面板
private final JPanel colorPanel = new JPanel(); // 存放颜色的面板
private final JPanel mainPanel = new JPanel(); // 主面板
private final JButton redButton = new JButton("红色");
private final JButton blueButton = new JButton("蓝色");
private final JButton greenButton = new JButton("绿色");
private final JButton lineButton = new JButton("直线");
private final JButton circleButton = new JButton("圆");
private final JButton rectangleButton = new JButton("矩形");
private final JButton undoButton = new JButton("撤销");
private final JButton redoButton = new JButton("恢复撤销");
private final JButton exitButton = new JButton("退出");
SketchpadPanel sketchPanel = new SketchpadPanel();
private void initFrame() {
commandPanelsetLayout(new FlowLayout());
commandPaneladd(lineButton);
commandPaneladd(circleButton);
commandPaneladd(rectangleButton);
commandPaneladd(undoButton);
commandPaneladd(redoButton);
commandPaneladd(exitButton);
colorPanelsetLayout(new FlowLayout());
colorPaneladd(redButton);
colorPaneladd(blueButton);
colorPaneladd(greenButton);
mainPanelsetLayout(new BorderLayout());
mainPaneladd(commandPanel, BorderLayoutNORTH);
mainPaneladd(colorPanel, BorderLayoutCENTER);
getContentPane()add("South", mainPanel);
getContentPane()add("Center", sketchPanel);
// 初始化设置:颜色和命令
lineButtonsetForeground(ColorRED);
sketchPanelsetColor(ColorRED);
redButtonsetForeground(ColorRED);
sketchPanelsetShapeType(ShapeTypesLINE);
}
private void initListener() {
redButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redAction(e);
}
});
blueButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
blueAction(e);
}
});
greenButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
greenAction(e);
}
});
undoButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
undoAction(e);
}
});
redoButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redoAction(e);
}
});
exitButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitAction(e);
}
});
lineButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lineAction(e);
}
});
circleButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
circleAction(e);
}
});
rectangleButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rectangleAction(e);
}
});
}
public SketchpadFrame() {
initFrame();
initListener();
thissetSize(500, 600);
setLocationByPlatform(true);
setResizable(true);
}
/ 处理事件 /
private void undoAction(ActionEvent e) {
sketchPanelundo();
}
private void redoAction(ActionEvent e) {
sketchPanelredo();
}
private void exitAction(ActionEvent e) {
Systemexit(0);
}
private void lineAction(ActionEvent e) {
// 选中按钮为红色, 其余为黑色
lineButtonsetForeground(ColorRED);
circleButtonsetForeground(ColorBLACK);
rectangleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesLINE);
}
private void circleAction(ActionEvent e) {
circleButtonsetForeground(ColorRED);
lineButtonsetForeground(ColorBLACK);
rectangleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesCIRCLE);
}
private void rectangleAction(ActionEvent e) {
rectangleButtonsetForeground(ColorRED);
lineButtonsetForeground(ColorBLACK);
circleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesRECTANGLE);
}
private void redAction(ActionEvent e) {
redButtonsetForeground(ColorRED);
blueButtonsetForeground(ColorBLACK);
greenButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorRED);
}
private void blueAction(ActionEvent e) {
blueButtonsetForeground(ColorRED);
redButtonsetForeground(ColorBLACK);
greenButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorBLUE);
}
private void greenAction(ActionEvent e) {
greenButtonsetForeground(ColorRED);
redButtonsetForeground(ColorBLACK);
blueButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorGREEN);
}
}/
@author 不落的太阳(Sean Yang)
@version 10
@since JDK 18
/
public class SketchpadMain {
/
测试方法
@param args命令行参数
/
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new SketchpadFrame();
framesetVisible(true);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
});
}
}
以上就是关于如何实现java画图程序,并可在已画的图上添加文字等,求大神T-T全部的内容,包括:如何实现java画图程序,并可在已画的图上添加文字等,求大神T-T、java画图怎么解决、java如何实现多线程绘图等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)