Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need click event for Rectangel class

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

971 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