Drawing (mouse events)

I've created six ellipses labelled p1, p2...up to p6. I'm trying to make it so that on a mouse click (if a p object is selected), an unfilled rectangle will be drawn around it. As a way of letting someone know the object was selected. I haven't had much luck. I don't want the rectangle to be drawn until the mouse event, and i've even tried drawing it first and then having it moved when a p object is clicked...still no luck. Any help would be great.

[CODE]
private class ENDMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
// Determine which object (if any) was clicked in, //"contains" is inside an abstract class, overidden in ellipse class, and rectangle class. (returns the coordinates).

Shape s = null;
if (p1 != null && p1.contains(e.getX(), e.getY())){
s = p1;
}else if (p2 != null && p2.contains(e.getX(), e.getY())){ s = p2;
}else if (p3 != null && p3.contains(e.getX(), e.getY())){ s = p3;
}else if (p4 != null && p4.contains(e.getX(), e.getY())){ s = p4;
}else if (p5 != null && p5.contains(e.getX(), e.getY())){ s = p5;
}else if (p6 != null && p6.contains(e.getX(), e.getY())){ s = p6;
}

// was a shape selected?
if (s != null) { //Draw a rectangle around the circle player = s;

point_x = e.getX(); //x-point clicked
point_y = e.getY(); //y-point clicked
Selector r1 = new Selector(Shape.MEDIUM, Color.black, false, point_x, point_y); //rectangle around ellipse
//r1.draw(g2); //can't call draw??? (not sure how to draw it)
}

repaint(); //tried drawing it with all the other shapes then repainting it over the ellipse chosen (didn't work). }

public void mouseReleased(MouseEvent e) {//no player is currently selected
player = null;
}
}

[/CODE]

Comments

  • [b][red]This message was edited by zibadian at 2007-2-2 9:59:36[/red][/b][hr]
    : I've created six ellipses labelled p1, p2...up to p6. I'm trying to make it so that on a mouse click (if a p object is selected), an unfilled rectangle will be drawn around it. As a way of letting someone know the object was selected. I haven't had much luck. I don't want the rectangle to be drawn until the mouse event, and i've even tried drawing it first and then having it moved when a p object is clicked...still no luck. Any help would be great.
    :
    : [CODE]
    : private class ENDMouseListener extends MouseAdapter {
    : public void mousePressed(MouseEvent e) {
    : // Determine which object (if any) was clicked in, //"contains" is inside an abstract class, overidden in ellipse class, and rectangle class. (returns the coordinates).
    :
    : Shape s = null;
    : if (p1 != null && p1.contains(e.getX(), e.getY())){
    : s = p1;
    : }else if (p2 != null && p2.contains(e.getX(), e.getY())){ s = p2;
    : }else if (p3 != null && p3.contains(e.getX(), e.getY())){ s = p3;
    : }else if (p4 != null && p4.contains(e.getX(), e.getY())){ s = p4;
    : }else if (p5 != null && p5.contains(e.getX(), e.getY())){ s = p5;
    : }else if (p6 != null && p6.contains(e.getX(), e.getY())){ s = p6;
    : }
    :
    : // was a shape selected?
    : if (s != null) { //Draw a rectangle around the circle player = s;
    :
    : point_x = e.getX(); //x-point clicked
    : point_y = e.getY(); //y-point clicked
    : Selector r1 = new Selector(Shape.MEDIUM, Color.black, false, point_x, point_y); //rectangle around ellipse
    : //r1.draw(g2); //can't call draw??? (not sure how to draw it)
    : }
    :
    : repaint(); //tried drawing it with all the other shapes then repainting it over the ellipse chosen (didn't work). }
    :
    : public void mouseReleased(MouseEvent e) {//no player is currently selected
    : player = null;
    : }
    : }
    :
    : [/CODE]
    :
    Drawing can only be done inside the paint() method. You should create a method draw(), which draws the rectangle if it is visible. This is very simple to do:
    [code]
    void public draw(Graphics gr) {
    if isVisible {
    // Perfrom drawing with selected color
    }
    }
    [/code]
    You should also make use of lists to hold your elements. This makes coding it much easier and the code can be expanded without needing to change a lot of methods. Here is an example:
    [code]
    ArrayList shapes = new ArrayList();
    ArrayList<Selector) selectors = new ArrayList();

    void addEllipse(Ellipse e)
    {
    ArrayList.add(e);
    }

    private class ENDMouseListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    Shape s = null;
    for (Shape p: shapes) {
    // Loop through all shapes and check if it contains the mouseevent
    if (p.contains(e.getX(), e.getY()) {
    s = p;
    }
    }
    if s != null {
    selectors.add(new Selector(s));
    // Add a selector to the list of selectors and make it enclose s
    }
    }

    void public paint(Graphics gr) {
    // Draw all shapes
    for (Shape s: shapes) {
    s.draw(gr);
    }
    // Draw all selectors
    for (Selector s: selectors) {
    s.draw(gr);
    }
    }
    [/code]


  • I'm new to all of this so i'll probably keep the code the same until i'm done the program (easier for me). Also the program is very long, i don't want to make any major changes just yet. The code you wrote also seemed similar to mine (except better written) and for now I can't even get it working. I don't think my mouse event method is even working properly. Using the code below, shouldn't something at least happen on a mouse click? Could you look over this and tell me if it's right, or what I should change the x, y coordinate for the rectangle to. The code does absoulutely nothing upon clicking.

    [CODE]
    private class ENDMouseListener extends MouseAdapter {
    public Shape mouseClick(MouseEvent e) { //returns shape
    // Determine which object (if any) was clicked in
    Shape s = null;

    if (p1 != null && p1.contains(e.getX(), e.getY())){
    s = p1;
    }else if (p2 != null && p2.contains(e.getX(), e.getY())){
    s = p2;
    }else if (p3 != null && p3.contains(e.getX(), e.getY())){
    s = p3;
    }else if (p4 != null && p4.contains(e.getX(), e.getY())){
    s = p4;
    }else if (p5 != null && p5.contains(e.getX(), e.getY())){
    s = p5;
    }else if (p6 != null && p6.contains(e.getX(), e.getY())){
    s = p6;
    }
    // Shape clicked?
    if (s != null) { //Draw a rectangle around the circle
    player = s; //player clicked
    point_x = e.getX(); //point x (player selected)
    point_y = e.getY(); //point y (player selected)

    Selector r1 = new Selector(Shape.MEDIUM, Color.black, false, point_x, point_y); //new rectangle created (using points x and y)

    }
    return s; //changed to return so i could draw
    }

    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    //Draw other shapes (code ommitted)

    //Selector (rectangle)
    if(s != null){
    r1.draw(g2);
    }
    }

    [/CODE]
  • : I'm new to all of this so i'll probably keep the code the same until i'm done the program (easier for me). Also the program is very long, i don't want to make any major changes just yet. The code you wrote also seemed similar to mine (except better written) and for now I can't even get it working. I don't think my mouse event method is even working properly. Using the code below, shouldn't something at least happen on a mouse click? Could you look over this and tell me if it's right, or what I should change the x, y coordinate for the rectangle to. The code does absoulutely nothing upon clicking.
    :
    : [CODE]
    : private class ENDMouseListener extends MouseAdapter {
    : public Shape mouseClick(MouseEvent e) { //returns shape
    : // Determine which object (if any) was clicked in
    : Shape s = null;
    :
    : if (p1 != null && p1.contains(e.getX(), e.getY())){
    : s = p1;
    : }else if (p2 != null && p2.contains(e.getX(), e.getY())){
    : s = p2;
    : }else if (p3 != null && p3.contains(e.getX(), e.getY())){
    : s = p3;
    : }else if (p4 != null && p4.contains(e.getX(), e.getY())){
    : s = p4;
    : }else if (p5 != null && p5.contains(e.getX(), e.getY())){
    : s = p5;
    : }else if (p6 != null && p6.contains(e.getX(), e.getY())){
    : s = p6;
    : }
    : // Shape clicked?
    : if (s != null) { //Draw a rectangle around the circle
    : player = s; //player clicked
    : point_x = e.getX(); //point x (player selected)
    : point_y = e.getY(); //point y (player selected)
    :
    : Selector r1 = new Selector(Shape.MEDIUM, Color.black, false, point_x, point_y); //new rectangle created (using points x and y)
    :
    : }
    : return s; //changed to return so i could draw
    : }
    :
    : }
    :
    : public void paintComponent(Graphics g) {
    : super.paintComponent(g);
    : Graphics2D g2 = (Graphics2D)g;
    :
    : //Draw other shapes (code ommitted)
    :
    : //Selector (rectangle)
    : if(s != null){
    : r1.draw(g2);
    : }
    : }
    :
    : [/CODE]
    :
    It is probably because the mouseClick() isn't linked to the applet/window. You need to add the ENDMouseListener to the window/applet by using the correct method. Also the mouseClick() should not return anything, since that is the way it is defined in the MouseListener interface.
  • It's put together like this: Frame is broken up into two parts Top: Drawings, Bottom : Controls. I Imported: java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;. Below is the code for the Top panel. Is there anything i'm missing?
    [CODE]
    end_panel = new HockeyLineUpPanel(); //Panel holds drawings
    content_pane.add(end_panel, "Center"); //Center, Controls South
    ENDMouseListener end_mouse_listener = new ENDMouseListener(); //Mouse Listener
    end_panel.addMouseListener(end_mouse_listener);//Add Mouse Listener
    [/CODE]

  • : It's put together like this: Frame is broken up into two parts Top: Drawings, Bottom : Controls. I Imported: java.awt.event.MouseAdapter;
    : import java.awt.event.MouseEvent;. Below is the code for the Top panel. Is there anything i'm missing?
    : [CODE]
    : end_panel = new HockeyLineUpPanel(); //Panel holds drawings
    : content_pane.add(end_panel, "Center"); //Center, Controls South
    : ENDMouseListener end_mouse_listener = new ENDMouseListener(); //Mouse Listener
    : end_panel.addMouseListener(end_mouse_listener);//Add Mouse Listener
    : [/CODE]
    :
    :
    With the mouseClick() event returning a Shape object, the MouseAdapter.mouseClick() doesn't get overridden, but you've created a new method in your ENDMouseListener. Thus the JVM still calls MouseAdapter.mouseClick(), which does nothing.
  • I have it all working now, i realised what I was doing wrong. When calling the constructor to create a new object I did this. Rectangle rect1 = new Rectangle(parameters), where "Rectangle" was the constructor. After removing the constructor name before the object name It all worked nicely. It was suprising and very annoying, took me a while to find that. I't's my first time doing things like this.

    Also, now that I have a rectanlge being draw everytime a click happens inside a circle. How can I undraw it when a click happens outside a circle? Can I destroy the rectanle or do I have to redraw it changing the color to the background color?
  • : I have it all working now, i realised what I was doing wrong. When calling the constructor to create a new object I did this. Rectangle rect1 = new Rectangle(parameters), where "Rectangle" was the constructor. After removing the constructor name before the object name It all worked nicely. It was suprising and very annoying, took me a while to find that. I't's my first time doing things like this.
    :
    : Also, now that I have a rectanlge being draw everytime a click happens inside a circle. How can I undraw it when a click happens outside a circle? Can I destroy the rectanle or do I have to redraw it changing the color to the background color?
    :
    Both ways are acceptable. I would use the destroy way, but you need to to check if the rectangle exists in the paint() method.
  • I'm also having problems with JLabels. I create a JLabel and add it to a frame, and try to setText() the JLabel upon mouse event. But the console just prints out alot of errors when I click on the position of P1. I made the label public if that matters. How can I edit the label upon a mouse event (click)?

    [CODE]
    JLabel player_position_label = new JLabel("Click a player!", SwingConstants.RIGHT);
    player_position_panel.add(player_position_label);
    [/CODE]

    Then in my mouseListener I have

    [CODE]
    if (p1 != null && p1.contains(e.getX(), e.getY())){
    player_position_label.setText("Goalie"); //when P1 clicked, errors display on console.
    }
    [/CODE]
  • : I'm also having problems with JLabels. I create a JLabel and add it to a frame, and try to setText() the JLabel upon mouse event. But the console just prints out alot of errors when I click on the position of P1. I made the label public if that matters. How can I edit the label upon a mouse event (click)?
    :
    : [CODE]
    : JLabel player_position_label = new JLabel("Click a player!", SwingConstants.RIGHT);
    : player_position_panel.add(player_position_label);
    : [/CODE]
    :
    : Then in my mouseListener I have
    :
    : [CODE]
    : if (p1 != null && p1.contains(e.getX(), e.getY())){
    : player_position_label.setText("Goalie"); //when P1 clicked, errors display on console.
    : }
    : [/CODE]
    :
    Which error do you get?
  • I get a null-pointer exception at the line where I try to setText() the JLabel. Along with alot of other error messages which I guess are related to the nullpointer exception. I create the label with text "Click a player". Then declare it as a private instance variable inside the class. Then try to setText() inside the mouseListener. How should I be coding it?


    [CODE]
    public HockeyLineUpFrame(int width, int length){
    JLabel player_position_label = new JLabel("Click a player!", SwingConstants.RIGHT);
    player_position_panel.add(player_position_label);
    }
    private JLabel player_position_label;
    [/CODE]

    Then in my mouseListener I have

    [CODE]
    if (p1 != null && p1.contains(e.getX(), e.getY())){
    player_position_label.setText("Goalie"); //nullpointer exception
    }
    [/CODE]

    Is that because its trying to change the instance variable which isn't set to anything? If so, how can I have a already created label inside my frame and change it upon a mouse click?


  • I get a null-pointer exception at the line where I try to setText() the JLabel. Along with alot of other error messages which I guess are related to the nullpointer exception. I create the label with text "Click a player". Then declare it as a private instance variable inside the class. Then try to setText() inside the mouseListener. How should I be coding it?



    public HockeyLineUpFrame(int width, int length){
    JLabel player_position_label = new JLabel("Click a player!", SwingConstants.RIGHT);
    player_position_panel.add(player_position_label);
    }
    private JLabel player_position_label;



    Then in my mouseListener I have


    if (p1 != null && p1.contains(e.getX(), e.getY())){
    player_position_label.setText("Goalie"); //nullpointer exception
    }



    Is that because its trying to change the instance variable which isn't set to anything? If so, how can I have a already created label inside my frame and change it upon a mouse click?

    =====================================================================

    Appearantly the mouseClicked() is called before the HockeyLineUpFrame(). This causes the player_position_label to be uncreated by the time you set the text. You should create the player_position_label in the constructor of the panel and leave it blank. That way the player won't see it until he clicks the mouse.
  • I got it working, it was the exact same problem I had when I was trying to get the rectangle to appear around the selected circle shape. In HockeyLineUpFrame I simply changed JLabel player_position_label = new JLabel("Click a player") to player_position_label = new JLabel("Click a player"). I really don't understand what the diffrence is, but it works now. The diffrence seems to be that the first will display the label, but the second way of writing it will allow you to modify it. Could you please explain this to me?
  • : I got it working, it was the exact same problem I had when I was trying to get the rectangle to appear around the selected circle shape. In HockeyLineUpFrame I simply changed JLabel player_position_label = new JLabel("Click a player") to player_position_label = new JLabel("Click a player"). I really don't understand what the diffrence is, but it works now. The diffrence seems to be that the first will display the label, but the second way of writing it will allow you to modify it. Could you please explain this to me?
    :
    The difference is that the first way declares a new variable instead of accessing an existing variable. Every time you declare a type before a variable name, Java will define a new variable.
  • That makes sense, thanks. Now comes the final step, storing JTextField entries into a player object thats selected upon clicking a JButton called Set. Then accessing that data upon clicking the player's shape and displaying it to the JTextFields. Also using a Reset JButton to clear the information for the selected player. Do you think I should create a class which extends the Player class (which sets the paramteres for the ellipse and draws it)? Or should I just create a method? I'm thinking arrays would be useful as well.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories