📄 newdraw.java
字号:
//MouseDraw
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
public class Newdraw
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MouseFrame extends JFrame implements ActionListener
{
public JMenuItem save;
public JMenuItem open;
public JMenuItem close;
MousePanel panel;
int n;
public MouseFrame()
{
setTitle("利用鼠标画图");
setSize(WIDTH, HEIGHT);
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu=new JMenu("File");
save=new JMenuItem("Save");
save.addActionListener(this);
fileMenu.add(save);
open=new JMenuItem("open");
open.addActionListener(this);
fileMenu.add(open);
menuBar.add(fileMenu);
// 将panel加入到frame
panel = new MousePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==save)
{
n=0;
FileDialog filedialog_save=new FileDialog(this,"save",FileDialog.SAVE);
filedialog_save.setVisible(true);
try{
File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
FileWriter tofile=new FileWriter(file);
BufferedWriter out=new BufferedWriter(tofile);
for(int i=0;i<panel.list.size();i++)
{
MyShape ms=(MyShape)(panel.list.get(i));
String ws=ms.getX()+" "+ms.getY()+" "+ms.getW()+" "+ms.getH()+" "+ms.getShape()+" ";
out.write(ws,0,ws.length());
out.flush();
n++;
}
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
}
else if(e.getSource()==open)
{
int i=0;
panel.list.clear();
FileDialog filedialog_open=new FileDialog(this,"open",FileDialog.LOAD);
filedialog_open.setVisible(true);
try
{
File file=new File(filedialog_open.getDirectory(),filedialog_open.getFile());
FileReader file_reader=new FileReader(file);
BufferedReader in=new BufferedReader(file_reader);
String rs;
rs=in.readLine();
int rect[]=new int[5];
int count=-1;
Color c;
StringTokenizer st=new StringTokenizer(rs," ");
while(st.hasMoreTokens())
{
String ms=st.nextToken();
count=++count%5;
rect[count]=Integer.parseInt(ms);
if(count==4&&rect[4]!=0)
{
panel.currentS=new MyShape(0,0,0,0,Color.black,0);
switch(rect[4])
{
case 1:
{
MyArc a=new MyArc(rect[0],rect[1],rect[2],rect[3],Color.red,rect[4]);
panel.list.add(a);
}
break;
case 2:
{
MyRect r=new MyRect(rect[0],rect[1],rect[2],rect[3],Color.red,rect[4]);
panel.list.add(r);
}
break;
case 3:
{
MyLine r=new MyLine(rect[0],rect[1],rect[2],rect[3],Color.red,rect[4]);
panel.list.add(r);
}
break;
}
}
}
panel.repaint();
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
}
}
public static final int WIDTH = 500;
public static final int HEIGHT = 600;
}
class MousePanel extends JPanel implements ActionListener
{
public int shapetype=1; //当前所画图形类型
private int mouseFromx=0,mouseFromy=0,mouseEndx=0,mouseEndy=0;//鼠标的位置
private JButton bt1,bt2,bt3,bt4,bt5,bt6;//按钮
public ArrayList list; //所有 图形所在数组
public MyShape currentS;
private boolean mousePressed=false; //鼠标是否按下
private Color c;
public MousePanel()
{
c=new Color(0,0,0);
currentS=new MyShape(0,0,0,0,c,0);
bt1=new JButton("圆");
bt2=new JButton("矩形");
bt3=new JButton("直线");
bt4=new JButton("清空");
bt5=new JButton("红色");
bt6=new JButton("蓝色");
list=new ArrayList();
add(bt1);
add(bt2);
add(bt3);
add(bt4);
add(bt5);
add(bt6);
bt1.addActionListener(this);
bt3.addActionListener(this);
bt2.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
//注册监听器
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int i;
MyShape s=new MyShape(0,0,0,0,c,0);
if(mousePressed==true)
{
currentS.drawShape(g);
//画此前所有的图形.
for( i=0;i<list.size();i++)
{
s=(MyShape)list.get(i);
s.drawShape(g);
}
}
}
public void actionPerformed(ActionEvent event)
{
Object source=event.getSource();
if(source==bt1)
shapetype=1;
else if(source==bt2)
shapetype=2;
else if(source==bt3)
shapetype=3;
else if(source==bt4)
{
list.clear();
c=new Color(0,0,0);
currentS=new MyShape(0,0,0,0,c,0);
repaint();
}
if(source==bt5)
{
c=new Color(255,0,0);
}
else if(source==bt6)
{
c=new Color(0,0,255);
}
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
//得到鼠标光标的当前位置
mouseFromx = event.getX();
mouseFromy = event.getY();
mousePressed=true;
}
public void mouseReleased(MouseEvent event)
{
mouseEndx=event.getX();
mouseEndy=event.getY();
//鼠标松开后,将此前正在画的矩形current放入list,形状放入shape
int w=mouseEndx-mouseFromx;
int h=mouseEndy-mouseFromy;
switch(shapetype)
{
case 1:
{
MyArc a=new MyArc(mouseFromx,mouseFromy,w,h,c,1);
list.add(a);
}
break;
case 2:
{
MyRect r=new MyRect(mouseFromx,mouseFromy,w,h,c,2);
list.add(r);
}
break;
case 3:
{
MyLine l=new MyLine(mouseFromx,mouseFromy,w,h,c,3);
list.add(l);
}
break;
}
repaint();
}
}
private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
}
public void mouseDragged(MouseEvent event)
{
mouseEndx = event.getX();
mouseEndy = event.getY();
//得到鼠标移动过程中的当前图形
switch(shapetype)
{
case 1:
{
MyArc a=new MyArc(mouseFromx,mouseFromy,mouseEndx-mouseFromx,mouseEndy-mouseFromy,c,1);
currentS=a;
}
break;
case 2:
{
MyRect r=new MyRect(mouseFromx,mouseFromy,mouseEndx-mouseFromx,mouseEndy-mouseFromy,c,2);
currentS=r;
}
break;
case 3:
{
MyLine l=new MyLine(mouseFromx,mouseFromy,mouseEndx-mouseFromx,mouseEndy-mouseFromy,c,3);
currentS=l;
}
break;
}
repaint();
}
}
}
class MyShape
{
protected int x,y,w,h,s;
Color color;
public MyShape(int x1,int y1,int w1,int h1,Color c1,int s1)
{
x=x1;
y=y1;
w=w1;
h=h1;
s=s1;
color=c1;
}
public void drawShape(Graphics g)
{
g.setColor(color);
}
public String getX()
{
return x+"";
}
public String getY()
{
return y+"";
}
public String getW()
{
return w+"";
}
public String getH()
{
return h+"";
}
public Color getColor()
{
return color;
}
public int getShape()
{
return s;
}
}
class MyLine extends MyShape
{
public MyLine(int x1,int y1,int w1,int h1,Color c1,int s1)
{
super(x1,y1,w1,h1,c1,s1);
}
public void drawShape(Graphics g)
{
super.drawShape(g);
g.drawLine(x,y,x+w,y+h);
}
}
class MyArc extends MyShape
{
public MyArc(int x1,int y1,int w1,int h1,Color c1,int s1)
{
super(x1,y1,w1,h1,c1,s1);
}
public void drawShape(Graphics g)
{
super.drawShape(g);
if(w>0&&h>0)
g.drawArc(x,y,w,h,0,360);
if(w<0&&h>0)
g.drawArc(x+w,y,Math.abs(w),h,0,360);
if(w<0&&h<0)
g.drawArc(x+w,y+h,Math.abs(w),Math.abs(h),0,360);
if(w>0&&h<0)
g.drawArc(x,y+h,Math.abs(w),Math.abs(h),0,360);
}
}
class MyRect extends MyShape
{
public MyRect(int x1,int y1,int w1,int h1,Color c1,int s1)
{
super(x1,y1,w1,h1,c1,s1);
}
public void drawShape(Graphics g)
{
super.drawShape(g);
if(w>0&&h>0)
g.drawRect(x,y,w,h);
if(w<0&&h>0)
g.drawRect(x+w,y,Math.abs(w),h);
if(w<0&&h<0)
g.drawRect(x+w,y+h,Math.abs(w),Math.abs(h));
if(w>0&&h<0)
g.drawRect(x,y+h,Math.abs(w),Math.abs(h));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -