JMenuBar重新粉刷了我的JPanel

时间:2013-12-04 01:43:31

标签: java swing jpanel paintcomponent jmenubar

我正在编写一个小的绘图应用程序,问题是,当我在菜单上单击然后将鼠标移出并在面板上移动以继续绘画时,面板会擦除我绘制的所有内容并更改背景...这是我的代码:

框架代码:

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;
}
}

1 个答案:

答案 0 :(得分:2)

  

面板会删除我画的所有内容并改变背景......这是我的代码:

你需要跟踪已绘制的所有内容,然后再重新绘制所有内容。

有关实现此目的的两种常用方法,请参阅Custom Painting Approaches

  1. 使用ArrayList跟踪绘制的对象
  2. 使用BufferedImage