Note that there are some explanatory texts on larger screens.

plurals
  1. POJMenuItem only triggers from keyboard input and not click
    primarykey
    data
    text
    <p>I made a simple drawing program in Java using a JFrame (my first time with such). The user and click and drag to draw shapes, but that's not important. I have a <code>JMenuBar</code> with a bunch of options, like type of shape, New, and Quit. When the user clicks the <code>new</code> button, it's supposed to clear the screen. When the user hits Ctrl+N, this works just fine. However, when the button is clicked, it doesn't work at all. </p> <p>I put a debugging <code>System.out.println</code> inside of the actionEvent for newItem and it prints out just fine when the item is clicked, but it doesn't actually erase the screen. Any idea what would cause this?</p> <p>I cut out the majority of the program and left as much as was necessary to see the problem. You can still press and drag to draw a shape (the shape doesn't appear until you release the mouse button), and hit Ctrl+N to clear the screen, but clicking New doesn't do it.</p> <pre><code>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 Canvas { Point start = new Point(100,100); Point cur = new Point(100,100); Point end = new Point(100,100); Image offscreen; boolean erase = false; public void update(Graphics g) { //This is adds the new stuff to the screen or erases the screen if erase is true Graphics buffer; if (offscreen == null) { offscreen = createImage(getWidth(), getHeight()); } buffer = offscreen.getGraphics(); if (erase) { buffer.setColor(getBackground()); buffer.fillRect(0,0,800, 600); buffer.dispose(); erase = false; } paint(buffer); g.drawImage(offscreen, 0, 0, this); } public void paint(Graphics g) { Graphics buffer = g; if (erase) { g.dispose(); erase = false; } g.setColor(E3G04.lineColor); if (end.x &gt; start.x &amp;&amp; end.y &gt; start.y) g.fillRect(start.x,start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)); if (end.x &gt; start.x &amp;&amp; end.y &lt; start.y) g.fillRect(start.x,end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)); if (end.x &lt; start.x &amp;&amp; end.y &gt; start.y) g.fillRect(end.x, start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y)); if (end.x &lt; start.x &amp;&amp; end.y &lt; 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 &gt; start.x &amp;&amp; end.y &gt; 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 &gt; start.x &amp;&amp; end.y &lt; 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 &lt; start.x &amp;&amp; end.y &gt; 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 &lt; start.x &amp;&amp; end.y &lt; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload