Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need click event for Rectangel class

Posted on 2001-08-23
3
Medium Priority
?
203 Views
Last Modified: 2012-05-04
Anybody have any idea how to add a click event to a Rectangle class?  I'm basically pulling x,y,h,w coordinates from a SQL dbase and drawing rectangles in the applet.  I then need to be able to click any one particular rectangle and get a piece of information specific to that rectangle.  I don't think a label will work, that's why I'm using the rectangle class.  I've also put a panic button on the applet for testing purposes, it shows the PopUp window just fine, but the rectangles to not.  Here is the code for my Java applet...

import java.applet.Applet;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;

public class BX extends java.applet.Applet implements ActionListener {
  Frame f = new PopUp();
  Font f1=new Font("TimesRoman",Font.BOLD,18);
  Font f2=new Font("TimesRoman",Font.BOLD,12);
  String[] seat=new String[871];
  String[] leftpos=new String[871];
  String[] toppos=new String[871];
  String[] whatcolor=new String[871];
  String[] whatseat=new String[871];
  Rectangle[] whatr=new Rectangle[871];
  String caption;
  Color colororange=new Color(255,216,111);
  Color colorblue=new Color(132,191,252);
  Color colorgreen=new Color(164,254,164);
  Color colorbox=new Color(253,254,164);
  Color colormaroon=new Color(132,33,66);
 
  public void paint(Graphics screen) {

    for (int i=0;i<870;i++)  {  
      if (Integer.parseInt(seat[i])==1) {
        if (Integer.parseInt(whatcolor[i])==1)
          screen.setColor(colororange);
        else if (Integer.parseInt(whatcolor[i])==2)
          screen.setColor(colorblue);
        else if (Integer.parseInt(whatcolor[i])==3)
          screen.setColor(colorgreen);
        else if (Integer.parseInt(whatcolor[i])==4)
          screen.setColor(colorbox);
        else if (Integer.parseInt(whatcolor[i])==5)
          screen.setColor(Color.white);
        else
          screen.setColor(Color.black);
      }
      else if (Integer.parseInt(seat[i])==2) {
        screen.setColor(Color.black);
        }

      whatr[i]=new Rectangle(Integer.valueOf(leftpos[i]).intValue(),Integer.valueOf(toppos[i]).intValue(),13,16);
      whatr[i].addActionListener(this);
      screen.fillRect(Integer.parseInt(leftpos[i]),Integer.parseInt(toppos[i]),13,16);
    }
  }


public void actionPerformed(ActionEvent evt){

       showSeat();
     
  }

  public void init() {
    setBackground(colormaroon);

    Button panic=new Button("Panic!");
    add(panic);
    panic.addActionListener(this);

    for (int i=0;i<870;i++) {
      seat[i]=getParameter("seat["+i+"]");
      leftpos[i]=getParameter("leftpos["+i+"]");
      toppos[i]=getParameter("toppos["+i+"]");
      whatcolor[i]=getParameter("whatcolor["+i+"]");

      if (seat[i]==null)
        seat[i]="C";
    }
  }

void showSeat() {
  Object anchorpoint=getParent();
  while (! (anchorpoint instanceof Frame))
    anchorpoint = ( (Component) anchorpoint ).getParent();

    f.resize(150,150);
    f.show();
  }

}


That's it.  The line

whatr[i].addActionListener(this);

fails on compile in the java.awt.Rectangle class, leading me to believe that a rectangle just doesn't implement click events.  Any ideas would be greatly appreciated.
0
Comment
Question by:michaelwarden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
sdussinger earned 600 total points
ID: 6420681
You are correct: REctangle contains no click event handling ability. A Rectangle is simply a bounded region expressed by x, y, width, and height.  To do what you want, you need to get the position of a mouse event and loop through the array of whatr[] looking for an intersection.

First, your applet needs to implement MouseListener rather than ActionListener and provide the following functions:

public void mouseEntered (MouseEvent e)
{
  // Not interested in enter events
}

public void mouseExited (MouseEvent e)
{
  // Not interested in exit events
}

public void mousePressed (MouseEvent e)
{
  // Not interested in press events
}

public void mouseReleased (MouseEvent e)
{
  // Not interested in release events
}

public void mouseClicked (MouseEvent e)
{
  for (int i = 0; i < 870; i++)
    if (whatr[i].contains (e.getPoint ()))
    {
      .
      .
      .
      // Here's where you do whatever you want with the
      // rectangle.
      .
      .
      .
    }
}

Next, remove the actionPerformed () function form your applet and remove the addActionListener () call from your paint method.

Finally, in your init method call addMouseListener (this).

This will get mouse events in your applet directed to the mouseClicked method, and from there you can find the rectangle which was clicked.

HTH

--Steve
0
 

Author Comment

by:michaelwarden
ID: 6423642
Thanks, sdussinger, works great, you saved me some upcoming verbal abuse.
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6423842
Glad I could help...

--Steve
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question