Multiple methods in the same inner class?
Hey guys,
I have two programs. 1 which implements multiple mouse events:
Code :
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:
Code :
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.
Re: Multiple methods in the same inner class?
I am really confused as to what you're trying to do here. Why is there a paintComponent() method inside a MouseListener? This makes no sense. And why are you splitting each method up into its own MouseListener?
Re: Multiple methods in the same inner class?
This isn't a complete program, this is me just messing around. I just wanted to know if it was possible THIS way. The paintComponent() method's there for drawing a line when the mouse listener for drag is activated. I'd prefer to do it using paintComponent and graphics, so I don't really want an alternate, however better, program. Is this possible?
Thanks.
Re: Multiple methods in the same inner class?
Still not really sure why the paintComponent() method is inside a MouseListener.
You have the right idea with your first approach. Just don't move the paintComponent method into your anonymous inner class. Also, I still don't understand why each method has its own inner class instead of just one class that contains them all.
Re: Multiple methods in the same inner class?
So you're saying I can have all the methods just in the same inner class? Oh okay. I didn't know that.
Can I have all the inner classes like that, and then the paint component separate but not anonymous? Thanks! :)
Re: Multiple methods in the same inner class?
Okay. 1 more question. I have this:
Code :
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);
// Anonymouse 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());
}
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();
}
});
// Create a new window using the Swing class JFrame and add this panel
makeFrame();
}
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("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);
}
}
This works...-ish... When the mouse has been dragged there is a black line indicating the drag, but it doesn't say "mouse dragged" or show a gray line dragging. Any ideas?
Re: Multiple methods in the same inner class?
tommyf, when I provided you with the paintComponent() example, I had expected you to read further into it.
Now the downfall of that help is quite clear, as you haven't quite understood the theory behind it.
Yes you can have have them all in one and the paintComponent() doesn't belong to any mouse listeners, but to the JPanel (down from JComponent).
Re: Multiple methods in the same inner class?
In response to your subsequent post, this is because of the functionality of a MouseListener.
To understand why it doesn't detect mouse dragged, read the API for MouseListener (Specifically the main description / overview)
Re: Multiple methods in the same inner class?
Quote:
Originally Posted by
newbie
tommyf, when I provided you with the paintComponent() example, I had expected you to read further into it.
Now the downfall of that help is quite clear, as you haven't quite understood the theory behind it.
Yes you can have have them all in one and the paintComponent() doesn't belong to any mouse listeners, but to the JPanel (down from JComponent).
No, you're right. I don't understand it, which is why I'm turning to help. I have looked on the internet, and they do provide documentation, but with most of them I don't know where to begin, and I simply don't have time to read through all of them for 1 thing I need to do. Presumably you can see what I need to do to get the right result. Can you explain to me why I need to put paintComponent in the right place, and where that place is? Also one final question, the code doesn't report dragging. Presumably this should sort itself out when the paintComponent gets fixed?
Thanks.
Re: Multiple methods in the same inner class?
The paintComponent() method is in its rightful place right now.
I'm sorry, but the logic doesn't quite add up when you say you don't have time to read documentation, but do have the time to wait for replies on the forum.
The solution to the dragging is found in the API document I linked you, or at least the reasons are noted.
To clarify: the paintComponent method belongs to JComponent, which is a superclass of JPanel, so you shouldn't be saying it's a member of mouseAdapter() (as it was in the beginning of the post).
Re: Multiple methods in the same inner class?
The thing is, the fact that you don't understand basic inheritance and polymorphism tells us that you should definitely be reading the basic tutorials and the documentation. You're diving into the deep end before learning how to swim and then telling the lifeguard that you don't have time to wade around in the shallow end.
We're not trying to be mean, we're trying to save you a slew of headaches in the near future.
Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Re: Multiple methods in the same inner class?
"To track mouse moves and mouse drags, use the MouseMotionListener". *palm*
Thanks newbie for your help.
Of course it all works now.
Bisous X
Re: Multiple methods in the same inner class?
Quote:
Originally Posted by
KevinWorkman
The thing is, the fact that you don't understand basic inheritance and polymorphism tells us that you should definitely be reading the basic tutorials and the documentation. You're diving into the deep end before learning how to swim and then telling the lifeguard that you don't have time to wade around in the shallow end.
We're not trying to be mean, we're trying to save you a slew of headaches in the near future.
Recommended reading:
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
I appreciate that, and normally I would, but I have to do A LOT more work besides this program between now and its due date. Sorry for having to be spoon fed this stuff, but I was just in a hurry.
Thanks again.