我先大概做出了个框架,然后实现了花直线,圆,矩形,还有清空,,每次画,都会覆盖了之前的图,,为什么?是不是和repiant()的用法有关?怎么用?
[code=Java][import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.border.*;
public class ia implements ActionListener,MouseMotionListener,MouseListener{ /**
* @param args
*/
JFrame frame = new JFrame("zPaint"); JButton Save = new JButton("保存");
JButton Clear = new JButton("Clear");
JButton ColorBoard = new JButton("调色板");
MyCanvas Canvas = new MyCanvas();
JRadioButton Pen = new JRadioButton("铅笔");
JRadioButton Line = new JRadioButton("直线");
JRadioButton Rect = new JRadioButton("矩形");
JRadioButton Circle = new JRadioButton("圆形");
JComboBox LineSize,LineColor;
int tag = -1,Flag_Color = -1;
public static void main(String[] args) {
// TODO Auto-generated method stub
ia ts = new ia();
ts.go();
}
public void go(){
JPanel p2 = new JPanel();
JPanel p1 = new JPanel();
JPanel p3 = new JPanel();
JPanel pa = new JPanel();
JPanel pb = new JPanel();
ColorBoard.addActionListener(this);
Clear.addActionListener(this);
Pen.addActionListener(this);
Line.addActionListener(this);
Rect.addActionListener(this);
Circle.addActionListener(this);
String[] size = {"小笔画","中笔画","大笔画"};
LineSize = new JComboBox(size);
LineSize.setSelectedIndex(1);
String[] color = {"黑色","红色","蓝色","绿色"};
LineColor = new JComboBox(color);
LineColor.setSelectedIndex(0);
p2.add(Save);
p2.add(Clear);
p2.add(ColorBoard);
p3.add(LineSize);
p3.add(LineColor);
Border etched = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etched,"笔画大小及颜色");
p3.setBorder(border);
p1.add(Pen);
p1.add(Line);
p1.add(Rect);
p1.add(Circle);
p1.setBounds(12,3,3,44);
border = BorderFactory.createTitledBorder(etched,"绘制图形");
ButtonGroup group1 = new ButtonGroup();
group1.add(Pen);
group1.add(Line);
group1.add(Rect);
group1.add(Circle);
pa.setLayout(new GridLayout(0,4));
pa.add(p2);
pa.add(p3);
pa.add(p1);
Container cp = frame.getContentPane();
cp.add(pa,BorderLayout.NORTH);
cp.add(Canvas,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==Clear)//清空
{
tag = 0;
Canvas.repaint();
}
if(e.getSource()==Pen) //铅笔
tag = 0;
if(e.getSource()==Line)//直线
tag = 1;
if(e.getSource()==Rect)//矩形
tag = 2;
if(e.getSource()==Circle)//椭圆
tag = 3;
//Canvas.repaint();
}class MyCanvas extends JPanel implements MouseMotionListener,MouseListener{
int x1,y1,x2,y2,x3,y3;
public void paintComponent(Graphics g){
super.paintComponent(g);
Canvas.addMouseListener(this);
Canvas.addMouseMotionListener(this);
//g.setColor(Color.black);
/*if(Flag_Color==1)*/
//g.setColor(c);
g.setColor(Color.black);
switch(tag)
{
case 0://铅笔
{
break;
}
case 1://直线
{
g.drawLine(x1,y1,x3,y3);
break;
}
case 2://矩形
{
//g.setColor(Color.black);
if(x1<=x2 && y1<=y2)
g.drawRect(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1>=y2)
g.drawRect(x2,y2,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1<=x2 && y1>=y2)
g.drawRect(x1,y1-Math.abs(y2-y1),Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1<=y2)
g.drawRect(x1-Math.abs(x2-x1),y1,Math.abs(x2-x1),Math.abs(y2-y1));
break;
}
case 3://椭圆
{
if(x1<=x2 && y1<=y2)
g.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1>=y2)
g.drawOval(x2,y2,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1<=x2 && y1>=y2)
g.drawOval(x1,y1-Math.abs(y2-y1),Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1<=y2)
g.drawOval(x1-Math.abs(x2-x1),y1,Math.abs(x2-x1),Math.abs(y2-y1));
break;
}
default :
}
}
public void update(Graphics g)
{
paintComponent(Graphics g);
}
public void mouseDragged(MouseEvent e)
{
x2 = (int)e.getX();
y2 = (int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x1 = (int)e.getX();
y1 = (int)e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
x3 = (int)e.getX();
y3 = (int)e.getY();
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
}
}
]
[code=Java][import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.border.*;
public class ia implements ActionListener,MouseMotionListener,MouseListener{ /**
* @param args
*/
JFrame frame = new JFrame("zPaint"); JButton Save = new JButton("保存");
JButton Clear = new JButton("Clear");
JButton ColorBoard = new JButton("调色板");
MyCanvas Canvas = new MyCanvas();
JRadioButton Pen = new JRadioButton("铅笔");
JRadioButton Line = new JRadioButton("直线");
JRadioButton Rect = new JRadioButton("矩形");
JRadioButton Circle = new JRadioButton("圆形");
JComboBox LineSize,LineColor;
int tag = -1,Flag_Color = -1;
public static void main(String[] args) {
// TODO Auto-generated method stub
ia ts = new ia();
ts.go();
}
public void go(){
JPanel p2 = new JPanel();
JPanel p1 = new JPanel();
JPanel p3 = new JPanel();
JPanel pa = new JPanel();
JPanel pb = new JPanel();
ColorBoard.addActionListener(this);
Clear.addActionListener(this);
Pen.addActionListener(this);
Line.addActionListener(this);
Rect.addActionListener(this);
Circle.addActionListener(this);
String[] size = {"小笔画","中笔画","大笔画"};
LineSize = new JComboBox(size);
LineSize.setSelectedIndex(1);
String[] color = {"黑色","红色","蓝色","绿色"};
LineColor = new JComboBox(color);
LineColor.setSelectedIndex(0);
p2.add(Save);
p2.add(Clear);
p2.add(ColorBoard);
p3.add(LineSize);
p3.add(LineColor);
Border etched = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etched,"笔画大小及颜色");
p3.setBorder(border);
p1.add(Pen);
p1.add(Line);
p1.add(Rect);
p1.add(Circle);
p1.setBounds(12,3,3,44);
border = BorderFactory.createTitledBorder(etched,"绘制图形");
ButtonGroup group1 = new ButtonGroup();
group1.add(Pen);
group1.add(Line);
group1.add(Rect);
group1.add(Circle);
pa.setLayout(new GridLayout(0,4));
pa.add(p2);
pa.add(p3);
pa.add(p1);
Container cp = frame.getContentPane();
cp.add(pa,BorderLayout.NORTH);
cp.add(Canvas,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==Clear)//清空
{
tag = 0;
Canvas.repaint();
}
if(e.getSource()==Pen) //铅笔
tag = 0;
if(e.getSource()==Line)//直线
tag = 1;
if(e.getSource()==Rect)//矩形
tag = 2;
if(e.getSource()==Circle)//椭圆
tag = 3;
//Canvas.repaint();
}class MyCanvas extends JPanel implements MouseMotionListener,MouseListener{
int x1,y1,x2,y2,x3,y3;
public void paintComponent(Graphics g){
super.paintComponent(g);
Canvas.addMouseListener(this);
Canvas.addMouseMotionListener(this);
//g.setColor(Color.black);
/*if(Flag_Color==1)*/
//g.setColor(c);
g.setColor(Color.black);
switch(tag)
{
case 0://铅笔
{
break;
}
case 1://直线
{
g.drawLine(x1,y1,x3,y3);
break;
}
case 2://矩形
{
//g.setColor(Color.black);
if(x1<=x2 && y1<=y2)
g.drawRect(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1>=y2)
g.drawRect(x2,y2,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1<=x2 && y1>=y2)
g.drawRect(x1,y1-Math.abs(y2-y1),Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1<=y2)
g.drawRect(x1-Math.abs(x2-x1),y1,Math.abs(x2-x1),Math.abs(y2-y1));
break;
}
case 3://椭圆
{
if(x1<=x2 && y1<=y2)
g.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1>=y2)
g.drawOval(x2,y2,Math.abs(x2-x1),Math.abs(y2-y1));
if(x1<=x2 && y1>=y2)
g.drawOval(x1,y1-Math.abs(y2-y1),Math.abs(x2-x1),Math.abs(y2-y1));
if(x1>=x2 && y1<=y2)
g.drawOval(x1-Math.abs(x2-x1),y1,Math.abs(x2-x1),Math.abs(y2-y1));
break;
}
default :
}
}
public void update(Graphics g)
{
paintComponent(Graphics g);
}
public void mouseDragged(MouseEvent e)
{
x2 = (int)e.getX();
y2 = (int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x1 = (int)e.getX();
y1 = (int)e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
x3 = (int)e.getX();
y3 = (int)e.getY();
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
}
}
]
解决方案 »
- 一个奇怪的问题:3个double类型相加出现多余小数位
- LinkedList 问题!
- JAVA操作文本文件为什么超过3万行就内存益处啊?
- 小问一下swing中的布局类,请兄台们指教下。
- APPLET形式是不是无法调用JDBC?
- 为什么会出错呢?希望各位大侠们仗义!!!
- 救命!解决了一定在给分,javascript调用applet的问题
- illegal character: \12288???什么是不合法的存在??
- 各位大虾!!在java下怎么用ping命令?谢谢!
- 各位大佬帮忙解答一下萌新的疑问,感激不尽!!
- 哥们帮帮忙,关于String Split拆分“||”的问题
- 频繁操作数据库,会不会使数据库断开?
Canvas.addMouseListener(this);
Canvas.addMouseMotionListener(this);
这两行不要放到paintComponent方法里。
这样就不会了
而且也要管理和移除