Solved

Need click event for Rectangel class

Posted on 2001-08-23
3
184 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 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now