我正在编程一个小的绘画应用程序,问题是,当我在菜单上点击,然后将鼠标移出并在面板上点击以继续绘画时,面板会擦除我画的所有内容,并更改背景。。。这是我的代码:
帧的代码:
public class Ardoise extends JFrame {
private JMenuBar menu = new JMenuBar();
private JToolBar toolbar = new JToolBar();
private JMenu file = new JMenu("Fichier");
private JMenu edit = new JMenu("Edition");
private JMenu about = new JMenu("About");
private JMenu shape = new JMenu("Forme du curseur");
private JMenu color = new JMenu("Couleur du curseur");
private JMenuItem clear = new JMenuItem("Effacer");
private JMenuItem quit = new JMenuItem("Quitter");
private JMenuItem rond = new JMenuItem("Rond");
private JMenuItem carre = new JMenuItem("Carre");
private JMenuItem rouge = new JMenuItem("Rouge");
private JMenuItem bleu = new JMenuItem("Bleu");
private JMenuItem noir = new JMenuItem("Noir");
private JButton rougeButton = new JButton(new ImageIcon("rouge.jpg"));
private JButton bleuButton = new JButton(new ImageIcon("bleu.jpg"));
private JButton noirButton = new JButton(new ImageIcon("noir.jpg"));
private JButton formecarreeButton = new JButton(new ImageIcon("formecarree.png"));
private JButton formerondeButton = new JButton(new ImageIcon("formeronde.png"));
private JPanel container = new JPanel();
private Panneau pan = new Panneau();
private ColorListener cListener = new ColorListener();
public Ardoise(){
this.setTitle("Paint");
this.setSize(700,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
initComposants();
this.setVisible(true);
}
private void initComposants(){
file.add(clear);
file.addSeparator();
file.add(quit);
file.setMnemonic('F');
clear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK));
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_DOWN_MASK));
shape.add(rond);
shape.add(carre);
color.add(rouge);
color.add(bleu);
color.add(noir);
edit.add(shape);
edit.add(color);
edit.setMnemonic('E');
menu.add(file);
menu.add(edit);
toolbar.add(formecarreeButton);
toolbar.add(formerondeButton);
toolbar.addSeparator();
toolbar.add(noirButton);
toolbar.add(rougeButton);
toolbar.add(bleuButton);
pan.addMouseMotionListener(new PaintListener1());
pan.addMouseListener(new PaintListener2());
clear.addActionListener(new ClearListener());
rougeButton.addActionListener(cListener);
bleuButton.addActionListener(cListener);
noirButton.addActionListener(cListener);
container.setLayout(new BorderLayout());
container.add(toolbar,BorderLayout.NORTH);
container.add(pan,BorderLayout.CENTER);
this.setContentPane(container);
this.setJMenuBar(menu);
}
class PaintListener1 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
pan.setx(e.getX());
pan.sety(e.getY());
pan.repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
class PaintListener2 implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
pan.setx(e.getX());
pan.sety(e.getY());
pan.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class ClearListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
pan.setClean(true);
pan.repaint();
}
}
class ColorListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource() == rougeButton)
pan.setColor(Color.red);
if(e.getSource() == bleuButton)
pan.setColor(Color.blue);
if(e.getSource() == noirButton)
pan.setColor(Color.black);
}
}
}
面板代码:
public class Panneau extends JPanel{
private int mousex=0,mousey=0;
private boolean clean=true;;
private Color color= Color.black;
public void paintComponent(Graphics g){
g.setColor(color);
draw(g);
if(clean){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
clean=false;
}
}
private void draw(Graphics g){
g.fillOval(mousex, mousey, 10, 10);
}
public void setx(int x){
this.mousex=x;
}
public void sety(int y){
this.mousey=y;
}
public void setClean(boolean c){
this.clean = c;
}
public void setColor(Color c){
this.color=c;
}
}
面板擦除了我画的所有东西,还改变了背景。。。这是我的代码:
你需要跟踪所有已绘制的内容,然后重新绘制所有内容。
请参阅自定义绘画方法,了解两种常见的方法:
- 使用ArrayList跟踪绘制的对象
- 使用缓冲图像