然后把两个Panel放到JFrame的内容面板里面.
基本功能确实实现了,能在上面画画,但是我最小化或最大化的时候,之前画的图就消失了.还有就是,比如我打开颜色对话框,那么也把之前画的给"擦掉"了.而且最不懂的就是,每次被擦掉重画的时候,会在画画的JPanel上面画出一个上面的Button来...
我想肯定是我有什么地方没做对.我在那个做画的地方使用的是paintComponent()方法.
我有代码,如果要的话,我贴上来.麻烦各位高手指点一下啊!!谢谢了!!
9 个解决方案
#1
贴吧
#2
/*装载两个面板的JFrame*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComponentTest extends JFrame implements ActionListener
{
private boolean b; //橡皮通知
private Color c; //颜色变量
private int clickFlag = 0; //橡皮按钮标记
JButton clearB = new JButton("橡皮"); //橡皮按钮
JButton colorB = new JButton("颜色"); //颜色按钮
JPanel buttonPane = new JPanel(); //按钮面板
MyPane pane = new MyPane();//绘图面班
public ComponentTest()
{
Container con = this.getContentPane(); //获取内容面班
//为按钮添加监视妻
buttonPane.add(clearB);
buttonPane.add(colorB); //在按钮面板中装置按钮
clearB.addActionListener(this);
colorB.addActionListener(this);
con.add(buttonPane,BorderLayout.NORTH);
con.add(pane,BorderLayout.CENTER); //安装两个面板
this.setSize(400,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == clearB)
{
if(clickFlag == 1)
{
pane.setClearOrDraw(false);
clickFlag = 0;
}
else
{
pane.setClearOrDraw(true);
clickFlag = 1;
}
}
if(e.getSource() == colorB)
{
pane.setColor(JColorChooser.showDialog(this,"颜色",Color.black));
}
}
public static void main(String[] args)
{
new ComponentTest();
}
}
/*画图的地方*/
public class MyPane extends JPanel
{
private int x; //鼠标坐标
private int y; //鼠标坐标
private boolean clear; //橡皮通知器
private Color color = Color.black; //默认颜色
public MyPane()
{
this.setSize(400,400);
this.setBackground(Color.white);
this.addMouseMotionListener(new MouseMotionAdapter() //鼠标移动适配器
{
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
//this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void setColor(Color c)
{
this.color = c;
}
public void setClearOrDraw(boolean b)
{
this.clear = b;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComponentTest extends JFrame implements ActionListener
{
private boolean b; //橡皮通知
private Color c; //颜色变量
private int clickFlag = 0; //橡皮按钮标记
JButton clearB = new JButton("橡皮"); //橡皮按钮
JButton colorB = new JButton("颜色"); //颜色按钮
JPanel buttonPane = new JPanel(); //按钮面板
MyPane pane = new MyPane();//绘图面班
public ComponentTest()
{
Container con = this.getContentPane(); //获取内容面班
//为按钮添加监视妻
buttonPane.add(clearB);
buttonPane.add(colorB); //在按钮面板中装置按钮
clearB.addActionListener(this);
colorB.addActionListener(this);
con.add(buttonPane,BorderLayout.NORTH);
con.add(pane,BorderLayout.CENTER); //安装两个面板
this.setSize(400,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == clearB)
{
if(clickFlag == 1)
{
pane.setClearOrDraw(false);
clickFlag = 0;
}
else
{
pane.setClearOrDraw(true);
clickFlag = 1;
}
}
if(e.getSource() == colorB)
{
pane.setColor(JColorChooser.showDialog(this,"颜色",Color.black));
}
}
public static void main(String[] args)
{
new ComponentTest();
}
}
/*画图的地方*/
public class MyPane extends JPanel
{
private int x; //鼠标坐标
private int y; //鼠标坐标
private boolean clear; //橡皮通知器
private Color color = Color.black; //默认颜色
public MyPane()
{
this.setSize(400,400);
this.setBackground(Color.white);
this.addMouseMotionListener(new MouseMotionAdapter() //鼠标移动适配器
{
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
//this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void setColor(Color c)
{
this.color = c;
}
public void setClearOrDraw(boolean b)
{
this.clear = b;
}
}
#3
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
这个里面x和y永远是最后一个点的位置, 如果你要panel被遮盖而不擦掉以前的东西, 需要把轨迹都记录下来.
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
这个里面x和y永远是最后一个点的位置, 如果你要panel被遮盖而不擦掉以前的东西, 需要把轨迹都记录下来.
#4
用Vector将你画的东西保存起来,然后在paint中实现重画
#5
添加update函数如下就搞定
public void update(Graphics g){
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void update(Graphics g){
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
#6
在你paint()方法中不要super.paint(g)
#7
lxl888(晨晨) 正解
#8
我也在写这个程序,这是我的做法。已经实现勒,就是用一个ArrayList
来存放画过的图,repaint();的时候画的是数组里的图。加偶QQ讨论。
360151455
package cn.edu.xmu.LHand;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.util.ArrayList;
public class DrawBoard extends JPanel implements
MouseListener,MouseMotionListener{
public static int buttonPressed = 0;
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f)
};
private Shape currentShape = null;
private ArrayList shapes = null;
public DrawBoard(){
shapes = new ArrayList();
addMouseListener(this);
addMouseMotionListener(this);
setDoubleBuffered(true);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); //设置鼠标样式
this.setPreferredSize(new Dimension(400,400)); //设置工作区域
this.setBorder(BorderFactory.createRaisedBevelBorder()); //设置边框
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int i = shapes.size();
for(int j=0; j<i; j++){
((Shape)shapes.get(j)).draw(g);
}
}
public void mousePressed(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(DrawBoard.buttonPressed == 0)
return;
else if(event.getButton() == 1){
switch(buttonPressed){
case 1:
break;
case 2:
currentShape = new Line(getForeground(), event.getX(), event.getY());
break;
case 3:
currentShape = new Rect(getForeground(), event.getX(), event.getY());
break;
case 4:
currentShape = new RoundRect(getForeground(), event.getX(), event.getY());
break;
case 5:
currentShape = new Oval(getForeground(), event.getX(), event.getY());
break;
case 6:
case 7:
case 8:
break;
default:
break;
}
shapes.add(currentShape);
}
else if(event.getButton() != 1)
return;
}
public void mouseDragged(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(currentShape != null){
currentShape.mouseDragged(event);
repaint();
}
else
return;
}
public void mouseReleased(MouseEvent event){
if (event.getButton() == 1 && currentShape != null){
currentShape.mouseDragged(event);
currentShape = null;
repaint();
}
else
return;
}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){
Drawer.statusBar.setText(" |"+" "+"|");
}
public void mouseMoved(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
}
}
来存放画过的图,repaint();的时候画的是数组里的图。加偶QQ讨论。
360151455
package cn.edu.xmu.LHand;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.util.ArrayList;
public class DrawBoard extends JPanel implements
MouseListener,MouseMotionListener{
public static int buttonPressed = 0;
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f)
};
private Shape currentShape = null;
private ArrayList shapes = null;
public DrawBoard(){
shapes = new ArrayList();
addMouseListener(this);
addMouseMotionListener(this);
setDoubleBuffered(true);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); //设置鼠标样式
this.setPreferredSize(new Dimension(400,400)); //设置工作区域
this.setBorder(BorderFactory.createRaisedBevelBorder()); //设置边框
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int i = shapes.size();
for(int j=0; j<i; j++){
((Shape)shapes.get(j)).draw(g);
}
}
public void mousePressed(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(DrawBoard.buttonPressed == 0)
return;
else if(event.getButton() == 1){
switch(buttonPressed){
case 1:
break;
case 2:
currentShape = new Line(getForeground(), event.getX(), event.getY());
break;
case 3:
currentShape = new Rect(getForeground(), event.getX(), event.getY());
break;
case 4:
currentShape = new RoundRect(getForeground(), event.getX(), event.getY());
break;
case 5:
currentShape = new Oval(getForeground(), event.getX(), event.getY());
break;
case 6:
case 7:
case 8:
break;
default:
break;
}
shapes.add(currentShape);
}
else if(event.getButton() != 1)
return;
}
public void mouseDragged(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(currentShape != null){
currentShape.mouseDragged(event);
repaint();
}
else
return;
}
public void mouseReleased(MouseEvent event){
if (event.getButton() == 1 && currentShape != null){
currentShape.mouseDragged(event);
currentShape = null;
repaint();
}
else
return;
}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){
Drawer.statusBar.setText(" |"+" "+"|");
}
public void mouseMoved(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
}
}
#9
用addComponentListener添加组件的监听器,
public void componentResized(ComponentEvent e)函数里响应重绘
public void componentResized(ComponentEvent e)函数里响应重绘
#1
贴吧
#2
/*装载两个面板的JFrame*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComponentTest extends JFrame implements ActionListener
{
private boolean b; //橡皮通知
private Color c; //颜色变量
private int clickFlag = 0; //橡皮按钮标记
JButton clearB = new JButton("橡皮"); //橡皮按钮
JButton colorB = new JButton("颜色"); //颜色按钮
JPanel buttonPane = new JPanel(); //按钮面板
MyPane pane = new MyPane();//绘图面班
public ComponentTest()
{
Container con = this.getContentPane(); //获取内容面班
//为按钮添加监视妻
buttonPane.add(clearB);
buttonPane.add(colorB); //在按钮面板中装置按钮
clearB.addActionListener(this);
colorB.addActionListener(this);
con.add(buttonPane,BorderLayout.NORTH);
con.add(pane,BorderLayout.CENTER); //安装两个面板
this.setSize(400,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == clearB)
{
if(clickFlag == 1)
{
pane.setClearOrDraw(false);
clickFlag = 0;
}
else
{
pane.setClearOrDraw(true);
clickFlag = 1;
}
}
if(e.getSource() == colorB)
{
pane.setColor(JColorChooser.showDialog(this,"颜色",Color.black));
}
}
public static void main(String[] args)
{
new ComponentTest();
}
}
/*画图的地方*/
public class MyPane extends JPanel
{
private int x; //鼠标坐标
private int y; //鼠标坐标
private boolean clear; //橡皮通知器
private Color color = Color.black; //默认颜色
public MyPane()
{
this.setSize(400,400);
this.setBackground(Color.white);
this.addMouseMotionListener(new MouseMotionAdapter() //鼠标移动适配器
{
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
//this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void setColor(Color c)
{
this.color = c;
}
public void setClearOrDraw(boolean b)
{
this.clear = b;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComponentTest extends JFrame implements ActionListener
{
private boolean b; //橡皮通知
private Color c; //颜色变量
private int clickFlag = 0; //橡皮按钮标记
JButton clearB = new JButton("橡皮"); //橡皮按钮
JButton colorB = new JButton("颜色"); //颜色按钮
JPanel buttonPane = new JPanel(); //按钮面板
MyPane pane = new MyPane();//绘图面班
public ComponentTest()
{
Container con = this.getContentPane(); //获取内容面班
//为按钮添加监视妻
buttonPane.add(clearB);
buttonPane.add(colorB); //在按钮面板中装置按钮
clearB.addActionListener(this);
colorB.addActionListener(this);
con.add(buttonPane,BorderLayout.NORTH);
con.add(pane,BorderLayout.CENTER); //安装两个面板
this.setSize(400,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == clearB)
{
if(clickFlag == 1)
{
pane.setClearOrDraw(false);
clickFlag = 0;
}
else
{
pane.setClearOrDraw(true);
clickFlag = 1;
}
}
if(e.getSource() == colorB)
{
pane.setColor(JColorChooser.showDialog(this,"颜色",Color.black));
}
}
public static void main(String[] args)
{
new ComponentTest();
}
}
/*画图的地方*/
public class MyPane extends JPanel
{
private int x; //鼠标坐标
private int y; //鼠标坐标
private boolean clear; //橡皮通知器
private Color color = Color.black; //默认颜色
public MyPane()
{
this.setSize(400,400);
this.setBackground(Color.white);
this.addMouseMotionListener(new MouseMotionAdapter() //鼠标移动适配器
{
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
//this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void setColor(Color c)
{
this.color = c;
}
public void setClearOrDraw(boolean b)
{
this.clear = b;
}
}
#3
public void paint(Graphics g)
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
这个里面x和y永远是最后一个点的位置, 如果你要panel被遮盖而不擦掉以前的东西, 需要把轨迹都记录下来.
{
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
这个里面x和y永远是最后一个点的位置, 如果你要panel被遮盖而不擦掉以前的东西, 需要把轨迹都记录下来.
#4
用Vector将你画的东西保存起来,然后在paint中实现重画
#5
添加update函数如下就搞定
public void update(Graphics g){
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
public void update(Graphics g){
super.paint(g);
if(clear)
{
g.clearRect(x-10/2,y-10/2,10,10); //擦除
}
else
{
g.setColor(color);
g.fillOval(x-10/2,y-10/2,10,10); //绘画
}
}
#6
在你paint()方法中不要super.paint(g)
#7
lxl888(晨晨) 正解
#8
我也在写这个程序,这是我的做法。已经实现勒,就是用一个ArrayList
来存放画过的图,repaint();的时候画的是数组里的图。加偶QQ讨论。
360151455
package cn.edu.xmu.LHand;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.util.ArrayList;
public class DrawBoard extends JPanel implements
MouseListener,MouseMotionListener{
public static int buttonPressed = 0;
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f)
};
private Shape currentShape = null;
private ArrayList shapes = null;
public DrawBoard(){
shapes = new ArrayList();
addMouseListener(this);
addMouseMotionListener(this);
setDoubleBuffered(true);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); //设置鼠标样式
this.setPreferredSize(new Dimension(400,400)); //设置工作区域
this.setBorder(BorderFactory.createRaisedBevelBorder()); //设置边框
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int i = shapes.size();
for(int j=0; j<i; j++){
((Shape)shapes.get(j)).draw(g);
}
}
public void mousePressed(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(DrawBoard.buttonPressed == 0)
return;
else if(event.getButton() == 1){
switch(buttonPressed){
case 1:
break;
case 2:
currentShape = new Line(getForeground(), event.getX(), event.getY());
break;
case 3:
currentShape = new Rect(getForeground(), event.getX(), event.getY());
break;
case 4:
currentShape = new RoundRect(getForeground(), event.getX(), event.getY());
break;
case 5:
currentShape = new Oval(getForeground(), event.getX(), event.getY());
break;
case 6:
case 7:
case 8:
break;
default:
break;
}
shapes.add(currentShape);
}
else if(event.getButton() != 1)
return;
}
public void mouseDragged(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(currentShape != null){
currentShape.mouseDragged(event);
repaint();
}
else
return;
}
public void mouseReleased(MouseEvent event){
if (event.getButton() == 1 && currentShape != null){
currentShape.mouseDragged(event);
currentShape = null;
repaint();
}
else
return;
}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){
Drawer.statusBar.setText(" |"+" "+"|");
}
public void mouseMoved(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
}
}
来存放画过的图,repaint();的时候画的是数组里的图。加偶QQ讨论。
360151455
package cn.edu.xmu.LHand;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.util.ArrayList;
public class DrawBoard extends JPanel implements
MouseListener,MouseMotionListener{
public static int buttonPressed = 0;
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f)
};
private Shape currentShape = null;
private ArrayList shapes = null;
public DrawBoard(){
shapes = new ArrayList();
addMouseListener(this);
addMouseMotionListener(this);
setDoubleBuffered(true);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); //设置鼠标样式
this.setPreferredSize(new Dimension(400,400)); //设置工作区域
this.setBorder(BorderFactory.createRaisedBevelBorder()); //设置边框
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int i = shapes.size();
for(int j=0; j<i; j++){
((Shape)shapes.get(j)).draw(g);
}
}
public void mousePressed(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(DrawBoard.buttonPressed == 0)
return;
else if(event.getButton() == 1){
switch(buttonPressed){
case 1:
break;
case 2:
currentShape = new Line(getForeground(), event.getX(), event.getY());
break;
case 3:
currentShape = new Rect(getForeground(), event.getX(), event.getY());
break;
case 4:
currentShape = new RoundRect(getForeground(), event.getX(), event.getY());
break;
case 5:
currentShape = new Oval(getForeground(), event.getX(), event.getY());
break;
case 6:
case 7:
case 8:
break;
default:
break;
}
shapes.add(currentShape);
}
else if(event.getButton() != 1)
return;
}
public void mouseDragged(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
if(currentShape != null){
currentShape.mouseDragged(event);
repaint();
}
else
return;
}
public void mouseReleased(MouseEvent event){
if (event.getButton() == 1 && currentShape != null){
currentShape.mouseDragged(event);
currentShape = null;
repaint();
}
else
return;
}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){
Drawer.statusBar.setText(" |"+" "+"|");
}
public void mouseMoved(MouseEvent event){
Drawer.statusBar.setText(" |"+event.getX()+","+event.getY()+"|");
}
}
#9
用addComponentListener添加组件的监听器,
public void componentResized(ComponentEvent e)函数里响应重绘
public void componentResized(ComponentEvent e)函数里响应重绘