1

I wrote a simple drawing program in Java that allows the user to select between different shapes, sizes, and colors (in the JMenuBar) and draw them with the mouse. After overcoming many issues with the program (I originally wrote it in AWT but was convinced to switch to Swing, so excuse any poor Swing coding, because it's the first time I've ever used it), I have one problem left to solve. Whenever the user draws something on the screen (by default a red rectangle with a black border), the JMenuBar duplicates right under the original JMenuBar. I figure it's something weird with the way paintComponent buffers the screen, but since I've never used it before, I don't have a clue how it works.

Any ideas how I can make it stop duplicating the JMenuBar?

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;

public class E3G04 extends JFrame implements WindowListener, ActionListener, MouseListener, MouseMotionListener
{
    //Variables are declared as volatile to ensure that they're always called from system RAM
    static volatile String type = "rectangle";
    static volatile Boolean fill = true;
    static Color lineColor = Color.BLACK;
    static Color fillColor = Color.RED;
    static int size = 1;

    CanvasEX cx = new CanvasEX();

    static boolean running = true;

    JMenuBar mb = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
            JMenuItem newItem = new JMenuItem("New");
            JMenuItem quitItem = new JMenuItem ("Quit");


    protected E3G04()
    {

        mb.add(fileMenu);
        fileMenu.add(newItem);
        fileMenu.add(quitItem);             
        newItem.setMnemonic('N');
        quitItem.setMnemonic('Q');
        newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));          
        newItem.addActionListener(this);
        quitItem.addActionListener(this);
        cx.setSize(800,600);
        cx.addMouseListener(this);
        cx.addMouseMotionListener(this);
        setJMenuBar(mb);
        setBounds(100,100,800,600);
        setLayout(new BorderLayout());
        add("Center",cx);
        addWindowListener(this);
        setResizable(true);
        setVisible(true);
    }

    public static void main(String [] args)
    {
        new E3G04();
    }


    public void stop()
    {
        newItem.removeActionListener(this);
        quitItem.removeActionListener(this);    
        dispose();
        System.exit(0);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object o = e.getSource();

        if (o == newItem)
        {
            cx.erase = true;
            cx.repaint();
        }

        if (o == quitItem)
        {   
            running = false;
            stop();
        }

    }      

    public void mousePressed(MouseEvent m)
    {
        cx.start = m.getPoint();
        cx.end = cx.start;
        cx.cur = cx.start;
    }

    public void mouseDragged(MouseEvent m)
    {
        cx.cur = m.getPoint();
    }   

    public void mouseReleased(MouseEvent m)
    {
        cx.end = cx.cur;
        cx.repaint();
    }   
    public void itemStateChanged(ItemEvent e)
    {
        Object o = e.getSource();

    }
    public void windowClosing(WindowEvent e)
    {
        running = false;
        stop();
    }
    public void mouseClicked(MouseEvent m){}    
    public void mouseExited(MouseEvent m){}
    public void mouseEntered(MouseEvent m){}    
    public void mouseMoved(MouseEvent m){}
    public void windowClosed(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
}


class CanvasEX extends JPanel
{
    Point start = new Point(100,100);
    Point cur = new Point(100,100);
    Point end = new Point(100,100);
    boolean erase = false;



    public void paintComponent(Graphics g)
    {

        Graphics buffer = g;
        if (erase)
        {
            g.dispose();
            erase = false;
        }   
        g.setColor(E3G04.lineColor);
        if (end.x > start.x && end.y > start.y)
            g.fillRect(start.x,start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
        if (end.x > start.x && end.y < start.y)
            g.fillRect(start.x,end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));  
        if (end.x < start.x && end.y > start.y)
            g.fillRect(end.x, start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
        if (end.x < start.x && end.y < start.y)
            g.fillRect(end.x, end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
        g.setColor(E3G04.fillColor);
        if (end.x > start.x && end.y > start.y)
            g.fillRect(start.x + E3G04.size,start.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
        if (end.x > start.x && end.y < start.y)
            g.fillRect(start.x + E3G04.size,end.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);  
        if (end.x < start.x && end.y > start.y)
            g.fillRect(end.x + E3G04.size, start.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
        if (end.x < start.x && end.y < start.y)
            g.fillRect(end.x + E3G04.size, end.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);          
    }
}
weberc2
  • 7,423
  • 4
  • 41
  • 57
Alex Kibler
  • 4,674
  • 9
  • 44
  • 74

1 Answers1

4

You do not honor the paint chain (which you should), by calling super.paintComponent(Graphics g) in overriden paintComponent(..) method:

@Override
public void paintComponent(Graphics g)
{
     super.paintComponent(g);

    //do rest of painting etc
}  
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Okay, well, that fixed the problem with the menubar being there, but now my drawings aren't persistant. How do I fix that? – Alex Kibler Dec 10 '12 at 17:30
  • You need something like `ArrayList` which will hold all `Rectanlges` to be drawn, and in `paintComponent` you would iterate through the array list and redraw each `Rectangle`, thus the drawings will persist – David Kroukamp Dec 10 '12 at 17:34
  • It's not all rectangles, though. Isn't there a way that I can create an image of the screen (minus the JMenubar) and then paint that image back on the screen? – Alex Kibler Dec 10 '12 at 17:37
  • @AlexKibler Im sure you could but this would not be the right way, you just have to have more array lists to hold all objects or use generics (each class will extend an Entity class which has the basics like x,y position width height etc) so different objects may be added to the same list. here is a small example for persistent rectangles: http://stackoverflow.com/questions/13358658/paintcomponent-draws-other-components-on-top-of-my-drawing/13359279#13359279 – David Kroukamp Dec 10 '12 at 17:40
  • 1
    Oh, okay, I'll do that then. Thanks! – Alex Kibler Dec 10 '12 at 17:46