Results 1 to 20 of 25
Thread: mouseListener problem
- 06-20-2011, 08:00 AM #1
mouseListener problem
I'm not sure if this is the right place to post this but I'm having issues using the mouseListener on a frame! I'm supposed to draw buttons on a frame and track the mouse to see if it's clicked while inside the buttons I drew in the frame, but what I have is not working! It's for a school assignment in a java course, I'll only post the class header for the class that extends the frame and implements the mouseListener since the assignment has me creating like 5 classes and the code is too much! I'm lost and need help! Please and thanks!
Java Code:import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class RandomShapes extends Frame implements MouseListener { }
- 06-20-2011, 01:56 PM #2
Does the code compile and execute? Are any of the mouse listener methods being called? Add printlns to show if they are called and print out the mouses location also.
I recommend that you write as small a program as possible that will detect and display mouse clicks. When you get the technique, you can copy it into the larger program. If you have problems with this smaller program, paste it here with your comments on what the problem is.
- 06-20-2011, 06:44 PM #3
The code does execute, and I have tried adding println, but the methods aren't being called! I'll try to write the smaller program now, thanks for the idea!
- 06-20-2011, 06:58 PM #4
Does the component with the listeners have focus?the methods aren't being called
- 06-20-2011, 07:05 PM #5
Here's my code for this whole class! The overall program is supposed to display a random amount of circles and squares when the encore button is pressed, so I'll eventually need a shape array!
The circle and square class both extend the shape class, and the main class creates an object of the RandomShapes class (This class) that's about it for the rest, I know that the shape circle and square classes do their job right, I've tested them!Java Code:import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class RandomShapes extends Frame implements MouseListener { public RandomShapes(){ //sets up frame setTitle("Random Shapes"); setLocation(220, 100); setSize(800, 600); setBackground(Color.white); setVisible(true); //instantiates buttons int x = BUTTON_WIDTH * 1/2; int y = BUTTON_HEIGHT * 3/2; encoreButton = new Abutton("Encore", Color.yellow.darker(), x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; removeButton = new Abutton("Remove", Color.pink, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; quitButton = new Abutton("Quit", Color.red, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); } public void paint(Graphics pane) { encoreButton.paint(pane); removeButton.paint(pane); quitButton.paint(pane); if(shape != null) shape.paint(pane); } public void mouseClicked(MouseEvent event) { check(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if(encoreButton.isInside(lastX, lastY)) encoreButton.flip(); else if(removeButton.isInside(lastX, lastY)) removeButton.flip(); else if(quitButton.isInside(lastX, lastY)) quitButton.flip(); } public void mouseReleased(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if(encoreButton.isInside(lastX, lastY)) encoreButton.flip(); else if(removeButton.isInside(lastX, lastY)) removeButton.flip(); else if(quitButton.isInside(lastX, lastY)) quitButton.flip(); System.out.print(lastX); } public void check() { if(encoreButton.isInside(lastX, lastY)) shape = new Square(); //I was using this to test if it worked, this isn't what the button is actually supposed to do else if(removeButton.isInside(lastX, lastY)) shape = new Circle(); } Abutton encoreButton, removeButton, quitButton; Shape shape; Shape[] shapes = new Shape[16]; private final int BUTTON_WIDTH = Abutton.BUTTON_WIDTH, BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT; private int lastX, lastY; }Last edited by Lrjavier; 06-20-2011 at 07:07 PM.
- 06-20-2011, 07:08 PM #6
Where do you add the mouse listener to the component? You've coded the listener methods but have not told the component to call any of them. That is done by adding a listener to the component telling it what code is the listener.
I don't see an printlns in any of the listener methods.
- 06-20-2011, 07:13 PM #7
I removed the printlns when they didn't work! How would I add a listener? My teacher never taught us that!
- 06-20-2011, 07:15 PM #8
Have you added any listeners like Action? I see you codeded: implements MouseListener
Look at the API doc for the components your are using for addMouse... methods and see what methods are available.
- 06-20-2011, 07:28 PM #9
I'm using eclipse and I get no option for viewing API documentation. Also, I'm really new to this, I don't know what component I should be adding the MouseListener to.
- 06-20-2011, 07:50 PM #10
Before going any further you must have access to the API documentation.
Here's the link:Java Platform SE 6
Also you should look at the Java Tutorial. Read the section about Mouse Listeners:
The Really Big Index
At this page Search for Mouse Listener
- 06-20-2011, 08:04 PM #11
Thanks a bunch, That worked wonders, I also noticed I was missing a repaint() method at the end of my check() method! Now my buttons are responding!
- 06-22-2011, 05:50 PM #12
I'm supposed to display up to 16 random shapes at the click of a button, when I first run my program and click the button it generates and fills an array with a random amount of squares and circles and paints them on the frame. My problem comes in when I click the button again! Where it's supposed to do this all again, for some reason it just erases all the shapes on the screen! I would really appreciate some help, maybe point me in the right direction? Thanks in advanced! Here's the code I have:
Java Code:import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class RandomShapes extends Frame implements MouseListener { //===================================================================================================// public RandomShapes() { // sets up frame setTitle("Random Shapes"); setLocation(220, 100); setSize(450, 400); setBackground(Color.white); setVisible(true); addWindowListener(myWindow); // to track window addMouseListener(this); // and mouse // instantiates buttons int x = BUTTON_WIDTH * 1 / 2; int y = BUTTON_HEIGHT * 3 / 2; encoreButton = new Abutton("Encore", Color.yellow.darker(), x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; removeButton = new Abutton("Remove", Color.pink, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; quitButton = new Abutton("Quit", Color.red, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); } //===================================================================================================// public void paint(Graphics pane) { encoreButton.paint(pane); removeButton.paint(pane); quitButton.paint(pane); if (shapes[j] != null) { while (j < i) { if (shapes[j] != null) shapes[j].paint(pane); j++; } } } //===================================================================================================// public void mouseClicked(MouseEvent event) { check(); } //===================================================================================================// public void mouseEntered(MouseEvent event) { } //===================================================================================================// public void mouseExited(MouseEvent event) { } //===================================================================================================// public void mousePressed(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if (encoreButton.isInside(lastX, lastY)) { encoreButton.flip(); repaint(); } else if (removeButton.isInside(lastX, lastY)) { removeButton.flip(); repaint(); } else if (quitButton.isInside(lastX, lastY)) { quitButton.flip(); repaint(); } } //===================================================================================================// public void mouseReleased(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if (encoreButton.isInside(lastX, lastY)) { encoreButton.flip(); } else if (removeButton.isInside(lastX, lastY)) { removeButton.flip(); repaint(); } else if (quitButton.isInside(lastX, lastY)) { quitButton.flip(); } } //===================================================================================================// public void check() { if (encoreButton.isInside(lastX, lastY)) { drawShapes(); } else if (removeButton.isInside(lastX, lastY)) { } else if (quitButton.isInside(lastX, lastY)) { System.exit(0); } else System.out.println("What?"); repaint(); } //===================================================================================================// public void drawShapes() { checkNum = (int) (100 * Math.random()); while (found == false) { if (checkNum >= 1 && checkNum <= 16) { newNum = checkNum; found = true; } else { checkNum = (int) (100 * Math.random()); } } while (i < newNum) { number = Math.random(); if (number >= 0 && number <= .5) { shapes[i] = new Square(); } else if (number > .5) { shapes[i] = new Circle(); } i++; } } //===================================================================================================// Abutton encoreButton, removeButton, quitButton; private MyWindow myWindow = new MyWindow(); private final int BUTTON_WIDTH = Abutton.BUTTON_WIDTH, BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT, LENGTH = 16; Shape[] shapes = new Shape[LENGTH]; private int lastX, lastY, i = 0, j = 0, checkNum, newNum; double number; boolean found = false; }
- 06-22-2011, 06:05 PM #13
Your code does not compile because of missing class definitions.
Abutton encoreButton, removeButton, quitButton;
private MyWindow
- 06-22-2011, 06:13 PM #14
Well I have all my classes, I just didn't paste the code because they work! The code compiles, the problem is that it erases all the shapes that it paints when I click the encore button a second time.
- 06-22-2011, 06:15 PM #15
For any one to test your code they need to compile it and execute it.
Other wise I can suggest you work on debugging your code.
Add print statements everywhere the shapes are painted and where they are erased. The output could show them being erased and not being painted again.
- 06-22-2011, 07:11 PM #16
I've since fixed that problem by changing my loop control variables to be local instead of instance variables, but a couple of new problems came up! I'll show you all of my classes this time:
The Abutton class creates and handles all the operations of a button
The RandomShapes class is the one that's giving me problems!Java Code:import java.awt.*; public class Abutton { public static final int BUTTON_WIDTH = 72, BUTTON_HEIGHT = BUTTON_WIDTH/2; public Abutton() { theLabel = "?????"; theColor = Color.black; x = y = 100; width = height = 10; up = true; } public Abutton(String someLabel, Color someColor, int someX, int someY, int someWidth, int someHeight) { theLabel = someLabel; theColor = someColor; x = someX; y = someY; width = someWidth; height = someHeight; up = true; } // // Initializes the components of a button. // public void setup(String someLabel, Color someColor, int someX, int someY, int someWidth, int someHeight) { theLabel = someLabel; theColor = someColor; x = someX; y = someY; width = someWidth; height = someHeight; up = true; } // // Flips the up/down state of a button (for 3-D effects) // public void flip() { up = ! up; } // // Determines if a given point is inside a button. // Note: The boundary is considered within (inside) the button. // public boolean isInside(int someX, int someY) { return ((someX >= x) && (someX <= x + width) && (someY >= y) && (someY <= y + height)); } // // Draws a button // public void paint(Graphics pane) { final int DELTA = 2; // To define the space between the // frame and the inside of a button pane.setColor(Color.black); // Drawing the button frame pane.drawRect(x, y, width, height); // in black, pane.setColor(theColor); // and the inside of the button pane.fill3DRect(x + DELTA, y + DELTA, // in the button color width - (2*DELTA - 1), height - (2*DELTA - 1), up); pane.setColor(Color.black); // Finally, we put the label in black // ... and nicely centered int labelWidth = pane.getFontMetrics().stringWidth(theLabel); int labelHeight = pane.getFontMetrics().getAscent(); pane.drawString(theLabel, x + (width - labelWidth)/2, y + (height + labelHeight)/2); } // // The instance variables // private String theLabel; // To hold the label, private Color theColor; // the color, private int x, y, // the location, and width, height; // the size of the button private boolean up; // To achieve 3-D effects }
Shape class randomizes the attributes of circles and squaresJava Code:import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class RandomShapes extends Frame implements MouseListener { //===================================================================================================// public RandomShapes() { // sets up frame setTitle("Random Shapes"); setLocation(220, 100); setSize(450, 400); setBackground(Color.white); setVisible(true); addWindowListener(myWindow); // to track window addMouseListener(this); // and mouse // instantiates buttons int x = BUTTON_WIDTH * 1 / 2; int y = BUTTON_HEIGHT * 3 / 2; encoreButton = new Abutton("Encore", Color.yellow.darker(), x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; removeButton = new Abutton("Remove", Color.pink, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); y += BUTTON_HEIGHT * 1.5; quitButton = new Abutton("Quit", Color.red, x, y, BUTTON_WIDTH, BUTTON_HEIGHT); } //===================================================================================================// public void paint(Graphics pane) { int j = 0; encoreButton.paint(pane); removeButton.paint(pane); quitButton.paint(pane); if (shapes[j] != null) { while (j < newNum) { if (shapes[j] != null) shapes[j].paint(pane); j++; } } } //===================================================================================================// public void mouseClicked(MouseEvent event) { check(); } //===================================================================================================// public void mouseEntered(MouseEvent event) { } //===================================================================================================// public void mouseExited(MouseEvent event) { } //===================================================================================================// public void mousePressed(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if (encoreButton.isInside(lastX, lastY)) { encoreButton.flip(); repaint(); } else if (removeButton.isInside(lastX, lastY)) { removeButton.flip(); repaint(); } else if (quitButton.isInside(lastX, lastY)) { quitButton.flip(); repaint(); } } //===================================================================================================// public void mouseReleased(MouseEvent event) { lastX = event.getX(); lastY = event.getY(); if (encoreButton.isInside(lastX, lastY)) { encoreButton.flip(); } else if (removeButton.isInside(lastX, lastY)) { removeButton.flip(); repaint(); } else if (quitButton.isInside(lastX, lastY)) { quitButton.flip(); } } //===================================================================================================// public void check() { if (encoreButton.isInside(lastX, lastY)) { drawShapes(); } else if (removeButton.isInside(lastX, lastY)) { int randomNumber = (int)(100 * Math.random()); while(randomNumber < 0 || randomNumber > newNum) { randomNumber = (int)(100 * Math.random()); } shapes[randomNumber] = null; } else if (quitButton.isInside(lastX, lastY)) { System.exit(0); } else System.out.println("What?"); repaint(); } //===================================================================================================// public int drawShapes() { int i = 0; checkNum = (int) (100 * Math.random()); while (found == false) { if (checkNum >= 1 && checkNum <= 16) { newNum = checkNum; found = true; } else { checkNum = (int) (100 * Math.random()); } } while (i < newNum) { number = Math.random(); if (number >= 0 && number <= .5) { shapes[i] = new Square(); } else if (number > .5) { shapes[i] = new Circle(); } i++; } return newNum; } //===================================================================================================// Abutton encoreButton, removeButton, quitButton; private MyWindow myWindow = new MyWindow(); private final int BUTTON_WIDTH = Abutton.BUTTON_WIDTH, BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT, LENGTH = 16; Shape[] shapes = new Shape[LENGTH]; private int lastX, lastY, checkNum, newNum; double number; boolean found = false; }
Square class uses Shape class to draw a random squareJava Code:import java.awt.*; public class Shape { public Shape(){} protected void randomize() { x = (int) (200 * Math.random()) + Abutton.BUTTON_WIDTH * 2; y = (int) (200 * Math.random()) + Abutton.BUTTON_HEIGHT * 2; size = (int) (100 * Math.random()); } public void paint(Graphics pane) {} protected int x, y, size; }
Circle class uses the Shape class to draw a random circle objectJava Code:import java.awt.*; public class Square extends Shape { public Square() { randomize(); } public void paint(Graphics pane) { pane.setColor(Color.green); // A green square pane.fillRect(x, y, size, size); // and pane.setColor(Color.black); // a black frame pane.drawRect(x, y, size, size); // around it } }
MyWindow class is used to extend a windowAdapter so that I can close the frameJava Code:import java.awt.*; public class Circle extends Shape { public Circle() { randomize(); } public void paint(Graphics pane) { pane.setColor(Color.cyan); // A circle pane.fillOval(x, y, size, size); // and pane.setColor(Color.black); // a black frame pane.drawOval(x, y, size, size); // around it } }
Java Code:import java.awt.*; import java.awt.event.*; public class MyWindow extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } }
The main class only instantiates an object of the RandomShapes class
Java Code:public class Program4 { public static void main(String[] args) { RandomShapes shape = new RandomShapes(); } }
I want the amount of shapes to be random each time I click the encore button, but since my newNum variable is an instance variable it doesn't change, but I can't make it local because then I have no way to test against it in my paint method when I go to paint the array after it has been filled. Secondly, the remove button is supposed to erase a random shape after each click, however, it tests positions in my array that have been made null, and as I keep clicking the button it eventually erases the rest of the shapes at once, and I can't figure out why!
- 06-22-2011, 07:34 PM #17
I want the amount of shapes to be random each time I click the encore buttonWhy aren't these descriptions of what you want your code to do in the program as comments. When I copy your code to compile it I have to come back and find your comments and copy them into your code so I know what that piece of code is supposed to do.The remove button is supposed to erase a random shape after each click,
- 06-22-2011, 07:34 PM #18
I want the amount of shapes to be random each time I click the encore buttonWhy aren't these descriptions of what you want your code to do in the program as comments? When I copy your code to compile it I have to come back and find your comments and copy them into your code so I know what that piece of code is supposed to do.The remove button is supposed to erase a random shape after each click,
- 06-22-2011, 07:48 PM #19
Oh, sorry, it's that since it's a school assignment I don't add in comments until I finish the program.
- 06-22-2011, 07:53 PM #20
When I'm coding a project, I first design it, then build a skeleton, then add comments describing what each method is supposed to do, then code the method to implement what the comments say the code is to do.
Without comments in your code, I can't tell what it is supposed to do. I can see what it does, but I don't know if that was what you intended the code to do or not. Being as lazy as the next guy, I'd rather not have to go back and forth between what you post here and the code that is in another place.
A suggestion for debugging your code: Add lots of printlns to show variables as they change value and to show execution flow.
Similar Threads
-
MouseListener problem
By s3ib in forum New To JavaReplies: 10Last Post: 01-03-2011, 01:20 PM -
Strategy Game: MouseListener problem
By Controloler in forum Java GamingReplies: 0Last Post: 12-10-2010, 03:11 AM -
Problem with mouseListener
By js91723 in forum AWT / SwingReplies: 12Last Post: 05-31-2010, 07:49 PM -
Problem with mouseListener (REPOST)
By js91723 in forum AWT / SwingReplies: 1Last Post: 05-31-2010, 05:15 PM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks