Solved

Need click event for Rectangel class

Posted on 2001-08-23
3
181 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
Comment Utility
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
Comment Utility
Thanks, sdussinger, works great, you saved me some upcoming verbal abuse.
0
 
LVL 4

Expert Comment

by:sdussinger
Comment Utility
Glad I could help...

--Steve
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

12 Experts available now in Live!

Get 1:1 Help Now