Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need click event for Rectangel class

Posted on 2001-08-23
3
Medium Priority
?
208 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
  • 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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

581 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