JMenuBar repaints my JPanel

403 views Asked by At

I am programming a small paint application, the problem is, when I clic on my Menu and then move the mouse out and clic on the panel to continiue painting, the panel erases everything I painted and changes the background too ... here's my code :

Frame's Code :

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

}

Panel's Code :

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

There are 1 answers

0
camickr On

the panel erases everything I painted and changes the background too ... here's my code :

You need to keep track of everything that has been painted and then repaint everything again.

See Custom Painting Approaches for the two common ways to do this:

  1. Use a ArrayList to keep track of objects painted
  2. Use a BufferedImage