JAVA实现简单的画图板
这个画图板只有3个选项:画直线,画正四边形,画圆选哪个就在下面的画板上画哪个还有一个要求就是要求每次画新的时候,旧的图还会保留在画布上(比如先画了个直线,再画圆的时候直线...
这个画图板只有3个选项:画直线,画正四边形,画圆
选哪个就在下面的画板上画哪个
还有一个要求就是要求每次画新的时候,旧的图还会保留在画布上
(比如先画了个直线,再画圆的时候直线不会消失)
求大虾们怎么实现这个程序,具体程序可以加分
应该不是很长的程序 还有就是最好能用applet实现 不要直接复制网上的程序。。明显跟我的要求不一样 展开
选哪个就在下面的画板上画哪个
还有一个要求就是要求每次画新的时候,旧的图还会保留在画布上
(比如先画了个直线,再画圆的时候直线不会消失)
求大虾们怎么实现这个程序,具体程序可以加分
应该不是很长的程序 还有就是最好能用applet实现 不要直接复制网上的程序。。明显跟我的要求不一样 展开
4个回答
展开全部
楼主写一个html,很容易把下面代码嵌入到applet,可以google一下实现,
还有copy自己不知道算不算复制。。。-_-!
--------------------------------------------------------------------
楼主给你一个我的,直接保存成pb.java编译运行,就是你要的画图功能 ,可以参考一下
____________________________________________________________________
import 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 col;
int tool;
int boarder;
Point(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 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,BasicStroke.CAP_BUTT,BasicStroke.JOIN_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();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
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("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
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);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",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)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; i<n ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)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://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
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://画矩形
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://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
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)//画笔大小
{
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)//橡皮大小
{
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)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(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)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(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)
{
if(e.getSource()==pen)//画笔
{toolFlag = 0;}
if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{toolFlag = 3;}
if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}
if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
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)//保存图画
{
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 IOe)
{
System.out.println("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
还有copy自己不知道算不算复制。。。-_-!
--------------------------------------------------------------------
楼主给你一个我的,直接保存成pb.java编译运行,就是你要的画图功能 ,可以参考一下
____________________________________________________________________
import 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 col;
int tool;
int boarder;
Point(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 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,BasicStroke.CAP_BUTT,BasicStroke.JOIN_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();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
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("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
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);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",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)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; i<n ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)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://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
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://画矩形
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://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
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)//画笔大小
{
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)//橡皮大小
{
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)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(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)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(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)
{
if(e.getSource()==pen)//画笔
{toolFlag = 0;}
if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{toolFlag = 3;}
if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}
if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
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)//保存图画
{
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 IOe)
{
System.out.println("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
--
2022-12-05 广告
2022-12-05 广告
图形化编程简单理解为用积木块形式编程,scratch和python也是其中的一种,属于入门级编程,以其简单生动的画面获得无数学生的喜爱,深圳市创客火科技有限公司是一家做教育无人机的公司,旗下有编程无人机,积木无人机及室内外编队,每款飞机含有...
点击进入详情页
本回答由--提供
展开全部
这个程序多画了两个图形,但你要的都在上面,要求保留图形是和“repaint()”有关
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DrawGraphicsDemo extends Applet implements ActionListener{
int flag=0;
Panel p=new Panel();
Button line=new Button("划线");
Button oval=new Button("画圈");
Button rect=new Button("画矩形");
Button polygon=new Button("画多边形");
Button arc=new Button("画扇形");
int xPoints[]={50,60,90,120,150
};
int yPoints[]={130,40,95,120,50
};
public void init(){
p.add(line);p.add(arc);
p.add(rect);p.add(polygon);
p.add(oval);
line.addActionListener(this);
rect.addActionListener(this);
arc.addActionListener(this);
polygon.addActionListener(this);
oval.addActionListener(this);
this.add(p);
p.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==line) flag=1;
if(e.getSource()==oval) flag=2;
if(e.getSource()==rect) flag=3;
if(e.getSource()==polygon) flag=4;
if(e.getSource()==arc) flag=5;
// repaint();
}
public void paint(Graphics g){
Color c=g.getColor();
switch(flag){
case 1:g.drawLine(150,150,500,500);break;
case 2:g.drawOval(50,80,40,40);
g.setColor(Color.red);
g.fillOval(100,80,40,40);
g.drawOval(150,80,60,40);
g.setColor(c);
break;
case 3:g.drawRect(20,80,40,30);
g.fillRect(80,80,40,60);
g.drawRoundRect(150,80,80,60,20,10);
g.fillRoundRect(170,90,40,40,10,20);
g.setColor(c);
break;
case 4:g.drawPolygon(xPoints,yPoints,5);break;
case 5:g.drawArc(30,50,100,120,0,45);
g.fillArc(120,80,150,100,45,90);
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DrawGraphicsDemo extends Applet implements ActionListener{
int flag=0;
Panel p=new Panel();
Button line=new Button("划线");
Button oval=new Button("画圈");
Button rect=new Button("画矩形");
Button polygon=new Button("画多边形");
Button arc=new Button("画扇形");
int xPoints[]={50,60,90,120,150
};
int yPoints[]={130,40,95,120,50
};
public void init(){
p.add(line);p.add(arc);
p.add(rect);p.add(polygon);
p.add(oval);
line.addActionListener(this);
rect.addActionListener(this);
arc.addActionListener(this);
polygon.addActionListener(this);
oval.addActionListener(this);
this.add(p);
p.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==line) flag=1;
if(e.getSource()==oval) flag=2;
if(e.getSource()==rect) flag=3;
if(e.getSource()==polygon) flag=4;
if(e.getSource()==arc) flag=5;
// repaint();
}
public void paint(Graphics g){
Color c=g.getColor();
switch(flag){
case 1:g.drawLine(150,150,500,500);break;
case 2:g.drawOval(50,80,40,40);
g.setColor(Color.red);
g.fillOval(100,80,40,40);
g.drawOval(150,80,60,40);
g.setColor(c);
break;
case 3:g.drawRect(20,80,40,30);
g.fillRect(80,80,40,60);
g.drawRoundRect(150,80,80,60,20,10);
g.fillRoundRect(170,90,40,40,10,20);
g.setColor(c);
break;
case 4:g.drawPolygon(xPoints,yPoints,5);break;
case 5:g.drawArc(30,50,100,120,0,45);
g.fillArc(120,80,150,100,45,90);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package miniPad;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
public class MinidrawPad extends JFrame implements Serializable{
String names[]={
//"New",
//"Open",
//"Save", //这三个是基本操作按钮,包括"新建"、"打开"、"保存"
/*接下来是我们的画图板上面有的基本的几个绘图单元按钮*/
"Pencil", //铅笔画,也就是用鼠标拖动着随意绘图
"Line", //绘制直线
"Rect", //绘制空心矩形
"fRect", //绘制以指定颜色填充的实心矩形
"Oval", //绘制空心椭圆
"fOval", //绘制以指定颜色填充的实心椭圆
"Circle", //绘制圆形
"fCircle", //绘制以指定颜色填充的实心圆形
"RoundRect", //绘制空心圆角矩形
"frRect", //绘制以指定颜色填充的实心圆角矩形
"Rubber", //橡皮擦,可用来擦去已经绘制好的图案
"Color", //选择颜色按钮,可用来选择需要的颜色
"Stroke", //选择线条粗细的按钮,输入需要的数值可以实现绘图线条粗细的变化
"Word" //输入文字按钮,可以在绘图板上实现文字输入
};
String tipText[]={
//这里是鼠标移动到相应按钮上面上停留时给出的提示说明条
//读者可以参照上面的按钮定义对照着理解
// "Draw a new picture",
//"Open a saved picture",
//"Save current drawing",
"Draw at will",
"Draw a straight line",
"Draw a rectangle",
"Fill a ractangle",
"Draw an oval",
"Fill an oval",
"Draw a circle",
"Fill a circle",
"Draw a round rectangle",
"Fill a round rectangle",
"Erase at will",
"Choose current drawing color",
"Set current drawing stroke",
"Write down what u want"
};
static final int DEFAULTWIDTH=900;
static final int DEFAULTHEIGHT=500;
JMenu file,color,stroke,help;
JMenuItem neww, save,open,exit,colorChooser,strokeChooser,helpTheme;
ArrayList<JMenuItem> mitems=new ArrayList<JMenuItem>();
ArrayList<JRadioButton> drawButtons=new ArrayList<JRadioButton>();
ArrayList<ToolDraws> toolList=new ArrayList<ToolDraws>();
ButtonGroup bgroup=new ButtonGroup();
DrawPanel drawpanel;
JLabel status;
int x1,y1,x2,y2;
int currentSelect;
public MinidrawPad(){
setSize(DEFAULTWIDTH,DEFAULTHEIGHT);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
file=new JMenu("文件");
color=new JMenu("颜色");
stroke=new JMenu("笔性");
help=new JMenu("帮助");
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
bar.add(file);
bar.add(color);
bar.add(stroke);
bar.add(help);
neww=new JMenuItem("新建(N)");
save=new JMenuItem("保存(S)");
open=new JMenuItem("打开(O)");
exit=new JMenuItem("退出(Q)");
colorChooser=new JMenuItem("颜色选择器");
strokeChooser=new JMenuItem("线条设置");
helpTheme= new JMenuItem("帮助主题");
color.add(colorChooser);
stroke.add(strokeChooser);
help.add(helpTheme);
colorChooser.addActionListener(new ItemHandler());
strokeChooser.addActionListener(new ItemHandler());
helpTheme.addActionListener(new ItemHandler());
mitems.add(neww);
mitems.add(save);
mitems.add(open);
mitems.add(exit);
char[] mo={'N','S','O','Q'};
KeyStroke[] ks={KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_0,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK)
};
for(int i=0;i<mitems.size();i++){
mitems.get(i).addActionListener(new ItemHandler());
mitems.get(i).setMnemonic(mo[i]);
mitems.get(i).setAccelerator(ks[i]);
file.add(mitems.get(i));
}
JToolBar tool =new JToolBar();
add(tool,BorderLayout.NORTH);
status =new JLabel(" 1");
add(status,BorderLayout.SOUTH);
for(int i=0;i<names.length;i++)
drawButtons.add(new JRadioButton(names[i]));
for(int i=0;i<drawButtons.size();i++){
drawButtons.get(i).addActionListener(new drawButtonHandler());
drawButtons.get(i).setToolTipText(tipText[i]);
bgroup.add(drawButtons.get(i));
tool.add(drawButtons.get(i));
}
drawpanel =new DrawPanel();
drawpanel.setBackground(Color.white);
add(drawpanel,BorderLayout.CENTER);
drawpanel.addMouseListener(new MouseMoniter());
drawpanel.addMouseMotionListener(new MouseMoniter2());
setVisible(true);
}
class MouseMoniter extends MouseAdapter{
public void mousePressed(MouseEvent e){
// x1=e.getX();
//y1=e.getY();
createObject(currentSelect);
toolList.get(toolList.size()-1).x1=e.getX();
toolList.get(toolList.size()-1).y1=e.getY();
System.out.println("press");
}
public void mouseReleased(MouseEvent e){
//x2=e.getX();
//y2=e.getY();
if(currentSelect==6||currentSelect==7)
return;
toolList.get(toolList.size()-1).x2=e.getX();
toolList.get(toolList.size()-1).y2=e.getY();
System.out.println("released");
drawpanel.repaint();
}
}
public void createObject(int currentSelect){
//生成画图工具对象
System.out.println(currentSelect);
switch(currentSelect){
case 0 :toolList.add(new Will());break;
case 1 :toolList.add(new Line());break;
case 2 :toolList.add(new Rect());break;
case 3 :toolList.add(new Rect_f());break;
case 4 :toolList.add(new Oval());break;
case 5 :toolList.add(new Oval_f());break;
case 6 :toolList.add(new Circle());break;
case 7 :toolList.add(new Circle_f());break;
case 8 :toolList.add(new Roundrect());break;
case 9 :toolList.add(new Roundrect_f());break;
case 10 :toolList.add(new Rub());break;
default : ;
}
}
class MouseMoniter2 extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
status.setText("mouse position: [x:"+e.getX()+" y:"+e.getY()+"]");
}
public void mouseDragged(MouseEvent e){
System.out.println("dragged");
x2=e.getX();
y2=e.getY();
toolList.get(toolList.size()-1).x2=e.getX();
toolList.get(toolList.size()-1).y2=e.getY();
//特别的类 Will rub
if(currentSelect==0){
createObject(0);
toolList.get(toolList.size()-1).x1=
toolList.get(toolList.size()-2).x2;
toolList.get(toolList.size()-1).y1=
toolList.get(toolList.size()-2).y2;
toolList.get(toolList.size()-1).x2=
e.getX();
toolList.get(toolList.size()-1).y2=
e.getY();
}
if(currentSelect==10){
createObject(10);
toolList.get(toolList.size()-1).x1=e.getX();
toolList.get(toolList.size()-1).y1=e.getY();
}
if(currentSelect==6||currentSelect==7){
toolList.get(toolList.size()-1).x2=
e.getX();
toolList.get(toolList.size()-1).y2=
toolList.get(toolList.size()-1).x2-
toolList.get(toolList.size()-1).x1+
toolList.get(toolList.size()-1).y1;
}
drawpanel.repaint();
}
}
class drawButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
int i=drawButtons.indexOf((JRadioButton)e.getSource());
currentSelect=i;
}
}
class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
if(e.getSource()==save){
FileDialog ff=new FileDialog(MinidrawPad.this,"保存文件",
FileDialog.SAVE);
ff.setVisible(true);
String dir=ff.getDirectory();
String name=ff.getFile();
if(dir==null||name==null){
System.out.println("no select");
return;
}
try{
System.out.println("dir="+dir+"\tname="+name);
ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream(new File(dir,name))));
out.writeObject(toolList);
out.flush();
out.close();
}catch(IOException e3){e3.printStackTrace();}
}
if(e.getActionCommand().equals("打开(O)")) {
FileDialog ff=new FileDialog(MinidrawPad.this,"打开文件",
FileDialog.LOAD);
ff.setVisible(true);
String dir=ff.getDirectory();
String name=ff.getName();
if(dir==null||name==null){
return;
}
try{
ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(
new FileInputStream(new File(dir,name))));
toolList=(ArrayList<ToolDraws>)in.readObject();
drawpanel.repaint();
}catch(Exception ee){ee.printStackTrace();}
}
}
}
class DrawPanel extends JPanel{
public void paint(Graphics g){
System.out.println("toolListSize:"+toolList.size());
super.paint(g);
if(currentSelect==10){
g.setColor(Color.black);
g.drawOval(x2, y2, 10, 10);}
g.setColor(Color.red);
for(int j=0;j<toolList.size();j++){
if(toolList.get(j)!=null){
toolList.get(j).draw(g);}
}
}
}
public static void main(String arg[]){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){e.printStackTrace();}
new MinidrawPad();
}
}
class ToolDraws implements Serializable{
Color color;
Stroke stroke;
int x1=0,y1=0,x2=0,y2=0;
public void draw(Graphics g){}
}
class Will extends ToolDraws{
public void draw(Graphics g){
//
g.drawLine(x1, y1, x2, y2);
}
}
class Line extends ToolDraws{
public void draw(Graphics g){
g.drawLine(x1, y1, x2, y2);
}
}
class Rect extends ToolDraws{
public void draw(Graphics g){
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x2-x1), Math.abs(y2-y1));
}
}
class Rect_f extends ToolDraws{
public void draw(Graphics g){
g.fillRect(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Oval extends ToolDraws{
public void draw(Graphics g){
g.drawOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Oval_f extends ToolDraws{
public void draw(Graphics g){
g.fillOval(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2));
}
}
class Circle extends ToolDraws{
public void draw(Graphics g){
g.drawOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Circle_f extends ToolDraws{
public void draw(Graphics g){
g.fillOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Roundrect extends ToolDraws{
public void draw(Graphics g){
/*drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
用此图形上下文的当前颜色绘制圆角矩形的边框。*/
g.drawRoundRect(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/5,
Math.abs(y1-y2)/5);
}
}
class Roundrect_f extends ToolDraws{
public void draw(Graphics g){
g.fillRoundRect(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/5,
Math.abs(y1-y2)/5);
}
}
class Rub extends ToolDraws{
public void draw(Graphics g){
g.setColor(Color.white);
g.fillOval(x1,y1,10,10);
}
}
//这个是我以前写的,有些功能未写完,但是你的要求还是实现了
//我还有个写的很完整的,搞得不知道那去了,所以,就发个不完整的拉
//
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
public class MinidrawPad extends JFrame implements Serializable{
String names[]={
//"New",
//"Open",
//"Save", //这三个是基本操作按钮,包括"新建"、"打开"、"保存"
/*接下来是我们的画图板上面有的基本的几个绘图单元按钮*/
"Pencil", //铅笔画,也就是用鼠标拖动着随意绘图
"Line", //绘制直线
"Rect", //绘制空心矩形
"fRect", //绘制以指定颜色填充的实心矩形
"Oval", //绘制空心椭圆
"fOval", //绘制以指定颜色填充的实心椭圆
"Circle", //绘制圆形
"fCircle", //绘制以指定颜色填充的实心圆形
"RoundRect", //绘制空心圆角矩形
"frRect", //绘制以指定颜色填充的实心圆角矩形
"Rubber", //橡皮擦,可用来擦去已经绘制好的图案
"Color", //选择颜色按钮,可用来选择需要的颜色
"Stroke", //选择线条粗细的按钮,输入需要的数值可以实现绘图线条粗细的变化
"Word" //输入文字按钮,可以在绘图板上实现文字输入
};
String tipText[]={
//这里是鼠标移动到相应按钮上面上停留时给出的提示说明条
//读者可以参照上面的按钮定义对照着理解
// "Draw a new picture",
//"Open a saved picture",
//"Save current drawing",
"Draw at will",
"Draw a straight line",
"Draw a rectangle",
"Fill a ractangle",
"Draw an oval",
"Fill an oval",
"Draw a circle",
"Fill a circle",
"Draw a round rectangle",
"Fill a round rectangle",
"Erase at will",
"Choose current drawing color",
"Set current drawing stroke",
"Write down what u want"
};
static final int DEFAULTWIDTH=900;
static final int DEFAULTHEIGHT=500;
JMenu file,color,stroke,help;
JMenuItem neww, save,open,exit,colorChooser,strokeChooser,helpTheme;
ArrayList<JMenuItem> mitems=new ArrayList<JMenuItem>();
ArrayList<JRadioButton> drawButtons=new ArrayList<JRadioButton>();
ArrayList<ToolDraws> toolList=new ArrayList<ToolDraws>();
ButtonGroup bgroup=new ButtonGroup();
DrawPanel drawpanel;
JLabel status;
int x1,y1,x2,y2;
int currentSelect;
public MinidrawPad(){
setSize(DEFAULTWIDTH,DEFAULTHEIGHT);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
file=new JMenu("文件");
color=new JMenu("颜色");
stroke=new JMenu("笔性");
help=new JMenu("帮助");
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
bar.add(file);
bar.add(color);
bar.add(stroke);
bar.add(help);
neww=new JMenuItem("新建(N)");
save=new JMenuItem("保存(S)");
open=new JMenuItem("打开(O)");
exit=new JMenuItem("退出(Q)");
colorChooser=new JMenuItem("颜色选择器");
strokeChooser=new JMenuItem("线条设置");
helpTheme= new JMenuItem("帮助主题");
color.add(colorChooser);
stroke.add(strokeChooser);
help.add(helpTheme);
colorChooser.addActionListener(new ItemHandler());
strokeChooser.addActionListener(new ItemHandler());
helpTheme.addActionListener(new ItemHandler());
mitems.add(neww);
mitems.add(save);
mitems.add(open);
mitems.add(exit);
char[] mo={'N','S','O','Q'};
KeyStroke[] ks={KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_0,InputEvent.CTRL_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK)
};
for(int i=0;i<mitems.size();i++){
mitems.get(i).addActionListener(new ItemHandler());
mitems.get(i).setMnemonic(mo[i]);
mitems.get(i).setAccelerator(ks[i]);
file.add(mitems.get(i));
}
JToolBar tool =new JToolBar();
add(tool,BorderLayout.NORTH);
status =new JLabel(" 1");
add(status,BorderLayout.SOUTH);
for(int i=0;i<names.length;i++)
drawButtons.add(new JRadioButton(names[i]));
for(int i=0;i<drawButtons.size();i++){
drawButtons.get(i).addActionListener(new drawButtonHandler());
drawButtons.get(i).setToolTipText(tipText[i]);
bgroup.add(drawButtons.get(i));
tool.add(drawButtons.get(i));
}
drawpanel =new DrawPanel();
drawpanel.setBackground(Color.white);
add(drawpanel,BorderLayout.CENTER);
drawpanel.addMouseListener(new MouseMoniter());
drawpanel.addMouseMotionListener(new MouseMoniter2());
setVisible(true);
}
class MouseMoniter extends MouseAdapter{
public void mousePressed(MouseEvent e){
// x1=e.getX();
//y1=e.getY();
createObject(currentSelect);
toolList.get(toolList.size()-1).x1=e.getX();
toolList.get(toolList.size()-1).y1=e.getY();
System.out.println("press");
}
public void mouseReleased(MouseEvent e){
//x2=e.getX();
//y2=e.getY();
if(currentSelect==6||currentSelect==7)
return;
toolList.get(toolList.size()-1).x2=e.getX();
toolList.get(toolList.size()-1).y2=e.getY();
System.out.println("released");
drawpanel.repaint();
}
}
public void createObject(int currentSelect){
//生成画图工具对象
System.out.println(currentSelect);
switch(currentSelect){
case 0 :toolList.add(new Will());break;
case 1 :toolList.add(new Line());break;
case 2 :toolList.add(new Rect());break;
case 3 :toolList.add(new Rect_f());break;
case 4 :toolList.add(new Oval());break;
case 5 :toolList.add(new Oval_f());break;
case 6 :toolList.add(new Circle());break;
case 7 :toolList.add(new Circle_f());break;
case 8 :toolList.add(new Roundrect());break;
case 9 :toolList.add(new Roundrect_f());break;
case 10 :toolList.add(new Rub());break;
default : ;
}
}
class MouseMoniter2 extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
status.setText("mouse position: [x:"+e.getX()+" y:"+e.getY()+"]");
}
public void mouseDragged(MouseEvent e){
System.out.println("dragged");
x2=e.getX();
y2=e.getY();
toolList.get(toolList.size()-1).x2=e.getX();
toolList.get(toolList.size()-1).y2=e.getY();
//特别的类 Will rub
if(currentSelect==0){
createObject(0);
toolList.get(toolList.size()-1).x1=
toolList.get(toolList.size()-2).x2;
toolList.get(toolList.size()-1).y1=
toolList.get(toolList.size()-2).y2;
toolList.get(toolList.size()-1).x2=
e.getX();
toolList.get(toolList.size()-1).y2=
e.getY();
}
if(currentSelect==10){
createObject(10);
toolList.get(toolList.size()-1).x1=e.getX();
toolList.get(toolList.size()-1).y1=e.getY();
}
if(currentSelect==6||currentSelect==7){
toolList.get(toolList.size()-1).x2=
e.getX();
toolList.get(toolList.size()-1).y2=
toolList.get(toolList.size()-1).x2-
toolList.get(toolList.size()-1).x1+
toolList.get(toolList.size()-1).y1;
}
drawpanel.repaint();
}
}
class drawButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
int i=drawButtons.indexOf((JRadioButton)e.getSource());
currentSelect=i;
}
}
class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
if(e.getSource()==save){
FileDialog ff=new FileDialog(MinidrawPad.this,"保存文件",
FileDialog.SAVE);
ff.setVisible(true);
String dir=ff.getDirectory();
String name=ff.getFile();
if(dir==null||name==null){
System.out.println("no select");
return;
}
try{
System.out.println("dir="+dir+"\tname="+name);
ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream(new File(dir,name))));
out.writeObject(toolList);
out.flush();
out.close();
}catch(IOException e3){e3.printStackTrace();}
}
if(e.getActionCommand().equals("打开(O)")) {
FileDialog ff=new FileDialog(MinidrawPad.this,"打开文件",
FileDialog.LOAD);
ff.setVisible(true);
String dir=ff.getDirectory();
String name=ff.getName();
if(dir==null||name==null){
return;
}
try{
ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(
new FileInputStream(new File(dir,name))));
toolList=(ArrayList<ToolDraws>)in.readObject();
drawpanel.repaint();
}catch(Exception ee){ee.printStackTrace();}
}
}
}
class DrawPanel extends JPanel{
public void paint(Graphics g){
System.out.println("toolListSize:"+toolList.size());
super.paint(g);
if(currentSelect==10){
g.setColor(Color.black);
g.drawOval(x2, y2, 10, 10);}
g.setColor(Color.red);
for(int j=0;j<toolList.size();j++){
if(toolList.get(j)!=null){
toolList.get(j).draw(g);}
}
}
}
public static void main(String arg[]){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){e.printStackTrace();}
new MinidrawPad();
}
}
class ToolDraws implements Serializable{
Color color;
Stroke stroke;
int x1=0,y1=0,x2=0,y2=0;
public void draw(Graphics g){}
}
class Will extends ToolDraws{
public void draw(Graphics g){
//
g.drawLine(x1, y1, x2, y2);
}
}
class Line extends ToolDraws{
public void draw(Graphics g){
g.drawLine(x1, y1, x2, y2);
}
}
class Rect extends ToolDraws{
public void draw(Graphics g){
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x2-x1), Math.abs(y2-y1));
}
}
class Rect_f extends ToolDraws{
public void draw(Graphics g){
g.fillRect(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Oval extends ToolDraws{
public void draw(Graphics g){
g.drawOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Oval_f extends ToolDraws{
public void draw(Graphics g){
g.fillOval(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2));
}
}
class Circle extends ToolDraws{
public void draw(Graphics g){
g.drawOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Circle_f extends ToolDraws{
public void draw(Graphics g){
g.fillOval(Math.min(x1,x2),Math.min(y1, y2),
Math.abs(x2-x1),Math.abs(y2-y1));
}
}
class Roundrect extends ToolDraws{
public void draw(Graphics g){
/*drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
用此图形上下文的当前颜色绘制圆角矩形的边框。*/
g.drawRoundRect(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/5,
Math.abs(y1-y2)/5);
}
}
class Roundrect_f extends ToolDraws{
public void draw(Graphics g){
g.fillRoundRect(Math.min(x1,x2), Math.min(y1, y2),
Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/5,
Math.abs(y1-y2)/5);
}
}
class Rub extends ToolDraws{
public void draw(Graphics g){
g.setColor(Color.white);
g.fillOval(x1,y1,10,10);
}
}
//这个是我以前写的,有些功能未写完,但是你的要求还是实现了
//我还有个写的很完整的,搞得不知道那去了,所以,就发个不完整的拉
//
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我以前写过一个 有现成的程序 你留邮箱我发给你
很简单的实现鼠标画线、方块、椭圆
还有是否填充和颜色选择
双缓冲区 保留之前图案 刷新不闪屏的
很简单的实现鼠标画线、方块、椭圆
还有是否填充和颜色选择
双缓冲区 保留之前图案 刷新不闪屏的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询