Hey guys,
I have two programs. 1 which implements multiple mouse events:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MousePanel extends JPanel implements MouseInputListener { private int startX, startY, endX, endY; private Color lineColor = Color.black; private JLabel mouseStatus; public MousePanel() { setLayout(new BorderLayout()); setBackground(Color.white); mouseStatus = new JLabel(); add(mouseStatus,BorderLayout.SOUTH); this.addMouseListener(this); this.addMouseMotionListener(this); // Create a new window using the Swing class JFrame and add this panel makeFrame(); } public void mouseEntered(MouseEvent me) { mouseStatus.setText("Object entered X: " + me.getX() + " Y: " + me.getY()); } public void mouseExited(MouseEvent me) { mouseStatus.setText("Object exited X: " + me.getX() + " Y: " + me.getY()); } public void mousePressed(MouseEvent me) { mouseStatus.setText("Button pressed X: " + me.getX() + " Y: " + me.getY()); startX = me.getX(); startY = me.getY(); } public void mouseReleased(MouseEvent me) { mouseStatus.setText("Button released X: " + me.getX() + " Y: " + me.getY()); lineColor = Color.black; endX = me.getX(); endY = me.getY(); repaint(); } public void mouseClicked(MouseEvent me) { mouseStatus.setText("Button clicked X: " + me.getX() + " Y: " + me.getY()); } public void mouseMoved(MouseEvent me) { mouseStatus.setText("Mouse moved X: " + me.getX() + " Y: " + me.getY()); } public void mouseDragged(MouseEvent me) { mouseStatus.setText("Button dragged X: " + me.getX() + " Y: " + me.getY()); lineColor = Color.lightGray; endX = me.getX(); endY = me.getY(); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(lineColor); g.drawLine(startX, startY, endX, endY); } // Create a window frame public void makeFrame() { // Instantiate a window frame using Swing class JFrame JFrame frame = new JFrame("MousePanel"); // When the window is closed terminate the application frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // set initial size of window frame.setSize(800, 600); // add the current object to the centre of the frame and make visible frame.getContentPane().add(this, BorderLayout.CENTER); frame.setVisible(true); } }
However, if I were to want all these methods as anonymous inner classes, say for a larger JPanel, I can't seem to get it to work for the graphics method. As far as I know (from what I've tried), I can't get the graphics method to implement. Maybe I don't know the right code, so I was hoping someone could help.
Thanks.
Here's what I've got so far:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MousePanel3 extends JPanel { private JLabel mouseStatus; private int startX, startY, endX, endY; private Color lineColor = Color.black; public MousePanel3() { setLayout(new BorderLayout()); setBackground(Color.white); mouseStatus = new JLabel(); add(mouseStatus,BorderLayout.SOUTH); // Anonymous inner class that overides method mouseClicked this.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { mouseStatus.setText("Object entered X: " + me.getX() + " Y: " + me.getY()); } }); this.addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent me) { mouseStatus.setText("Object exited X: " + me.getX() + " Y: " + me.getY()); } }); this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { mouseStatus.setText("Button pressed X: " + me.getX() + " Y: " + me.getY()); startX = me.getX(); startY = me.getY(); } }); this.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent me) { mouseStatus.setText("Button released X: " + me.getX() + " Y: " + me.getY()); lineColor = Color.black; endX = me.getX(); endY = me.getY(); repaint(); } }); this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { mouseStatus.setText("Button clicked X: " + me.getX() + " Y: " + me.getY()); } }); this.addMouseListener(new MouseAdapter() { public void mouseMoved(MouseEvent me) { mouseStatus.setText("Mouse moved X: " + me.getX() + " Y: " + me.getY()); } }); this.addMouseListener(new MouseAdapter() { public void mouseDragged(MouseEvent me) { mouseStatus.setText("Button dragged X: " + me.getX() + " Y: " + me.getY()); lineColor = Color.lightGray; endX = me.getX(); endY = me.getY(); repaint(); } }); this.addMouseListener(new MouseAdapter() { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(lineColor); g.drawLine(startX, startY, endX, endY); } }); // Create a new window using the Swing class JFrame and add this panel makeFrame(); } // Create a window frame public void makeFrame() { // Instantiate a window frame using Swing class JFrame JFrame frame = new JFrame("MousePanel3"); // When the window is closed terminate the application frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // set initial size of window frame.setSize(800, 600); // add the current object to the centre of the frame and make visible frame.getContentPane().add(this, BorderLayout.CENTER); frame.setVisible(true); } }
I'm fairly sure it's the this.addMouseListener for the graphics method line is the wrong one, but I don't know what it should be. If anyone could let me know, that'd be great.
Thanks very much.