import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Responding extends JPanel {
Rectangle target = new Rectangle(125,100,75,75);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(target);
}
public static void main(String[] args) {
Responding test = new Responding();
test.addMouseListener(test.ml);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
private MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(target.contains(e.getPoint())) {
// Here you can:
// change to another card in a CardLayout
// load another JPanel into the content pane
// show content in a JDialog or a JOptionPane
System.out.println("target selected");
}
}
};
}
Sorry it seams that Ididn't ask thecorrect question: so here I go again
Iwant to create jpeg file by users request, the jpeg will show a number of rectangles(nodes) and arcs(relations) connecting two rectangles. I wanted to know how can i add actionlisteners to each rectangle,so if users wants more information about that node they can click on the rectangle and say a new jsp page opens with additional information.
thanks for the relay and hopefully more
Ehsan.
Iwant to create jpeg file by users request,
A little confusing...
the jpeg will show a number of rectangles(nodes) and arcs(relations) connecting two
rectangles.
Okay.
I wanted to know how can i add actionlisteners to each rectangle,
Technically, you cannot add an ActionListener but we'll try to get the effect of one.
so if users wants more information about that node they can click on the rectangle and
say a new jsp page opens with additional information.
Can't help on the jsp page but I'll try to advise you on options for the graphics part.
So let's say you have the jpeg image containing rectangles connected by arcs. To get this
to respond to user mouse clicks you will need to store the location/size of each
rectangle (as a Rectangle) and use this information to determine which rectangle was
selected by the user, just like above. You can keep an array of Rectangles to use for
determining the selections and another (parallel) array with the URLs or data to use for
your jsp pages. Or you could make a Map with the Rectangles (or array index) as keys and
info objects (such as URL or a data store class) as the value. The Rectangle location
will have to be relative to the image in case it is shown in a larger component - another
detail. You add a MouseListener to the component showing the image, identify the selected
Rectangle in the mousePressed method and use the index of the Rectangle in its array to
access the info in the parallel array or use the Rectangle (or its array index) to get
the info from the map.