Question

I programmed a little paint application, with a JMenu, JToolBar and a JPanel, the problem is, when I begin to draw on the panel, the JMenu and JToolBar are drawn on the same panel, and sometimes the background of the panel becomes gray instead of white , here is what it looks like :java error

here is my code : JFrame code :

public class ArdoiseF 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 PanneauF pan = new PanneauF();
private ColorListener cListener = new ColorListener();
private ShapeListener shapeListener = new ShapeListener();


public ArdoiseF(){

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

    clear.addActionListener(new ClearListener());
    rougeButton.addActionListener(cListener);
    bleuButton.addActionListener(cListener);
    noirButton.addActionListener(cListener);
    rouge.addActionListener(cListener);
    bleu.addActionListener(cListener);
    noir.addActionListener(cListener);
    formecarreeButton.addActionListener(shapeListener);
    formerondeButton.addActionListener(shapeListener);
    carre.addActionListener(shapeListener);
    rond.addActionListener(shapeListener);

    container.setLayout(new BorderLayout());
    container.add(toolbar,BorderLayout.NORTH);
    container.add(pan,BorderLayout.CENTER);
    this.setContentPane(container);

    this.setJMenuBar(menu);

}


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 || e.getSource() == rouge)
            pan.setColor(Color.red);
        if(e.getSource() == bleuButton || e.getSource() == bleu)
            pan.setColor(Color.blue);
        if(e.getSource() == noirButton || e.getSource() == noir)
            pan.setColor(Color.black);
    }
}
class ShapeListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == formecarreeButton || e.getSource()== carre)
            pan.setShape("carre");
        if(e.getSource() == formerondeButton || e.getSource()== rond)
            pan.setShape("rond");
    }

}

}

JPanel code :

public class PanneauF extends JPanel{
private int i=0;
private int mousex=0,mousey=0;
private boolean clean=true;;
private Color color= Color.black;
private String shape = "rond";
ArrayList<Point> points = new ArrayList<Point>();


public PanneauF(){
    this.addMouseMotionListener(new MouseMotionListener(){

        public void mouseDragged(MouseEvent e) {
            points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape));
            repaint();

        }

        public void mouseMoved(MouseEvent arg0) {

        }       
    });
    this.addMouseListener(new MouseListener(){

        public void mouseClicked(MouseEvent e) {

        }

        public void mouseEntered(MouseEvent e) {

        }

        public void mouseExited(MouseEvent e) {

        }

        public void mousePressed(MouseEvent e) {
            points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape));
            repaint();

        }

        public void mouseReleased(MouseEvent e) {

        }
    });
}
public void paintComponent(Graphics g){
    draw(g);
    if(clean){
        g.setColor(Color.white);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        clean=false;
        points = new ArrayList<Point>();
    }
}

private void draw(Graphics g){
    for (Point p: this.points){
        g.setColor(p.getColor());
        if(p.getType()=="rond")
            g.fillOval(p.getPosx(), p.getPosy(), 10, 10);

        else{
                g.fillRect(p.getPosx(), p.getPosy(), 10, 10);
        }
    }
}
public void setClean(boolean c){
    this.clean = c;
}
public void setColor(Color c){
    this.color = c;
}
public void setShape(String S){
    this.shape=S;
}


}
Was it helpful?

Solution 2

When redrawing your panel you really should redraw the whole thing. Start by clearing it and then go through and add all your points back in.

Right now it seems odd since you draw all the points then afterwards you sometimes but only sometimes clear it?

Just always clear it then draw the points - otherwise you could have stray stuff from previous paints of the window that never gets written over. Swing components are only allowed to not draw to every pixel if they mark themselves as not being opaque.

OTHER TIPS

Just add super.paintComponent(g); at the start of your public void paintComponent(Graphics g) method in your PanneauF.

Read about customPaintings.

I had the same problem. Put the JMenuBar in your JFrame. Use the JPanel only for draw. Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top