Adding a Canvas to JLayeredPane
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sidney_Kwakkel
Posted On:   Thursday, June 10, 2004 02:03 PM

The ultimate goal of this project is to have a map image that I can plot certain points on. I wish those points to have a popup menu so that I can see more information about the places that I have plotted. The application that I posted creates a red dot that you can click to get a popup menu. Clicking on "Details" moves the dot and turns the dot green. Eventually, clicking details will create a dialog window with the exrta information about the plotted point. The reason, I want the layered pane is so that I can hover the plotted points over the map image. If you uncomment the lines //JLayeredPane layers = new JLayeredPane(); //layers.add(canvas, 0); and comment the lines    More>>

The ultimate goal of this project is to have a map image that I can plot certain points on. I wish those points to have a popup menu so that I can see more information about the places that I have plotted.

The application that I posted creates a red dot that you can click to get a popup menu. Clicking on "Details" moves the dot and turns the dot green. Eventually, clicking details will create a dialog window with the exrta information about the plotted point.

The reason, I want the layered pane is so that I can hover the plotted points over the map image.

If you uncomment the lines

			
//JLayeredPane layers = new JLayeredPane();
//layers.add(canvas, 0);

and comment the lines
			
setLayout(new BorderLayout());
add("Center",canvas);

Why doesn't it work?

			
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Vector;


public class ShapeDrawWithMenu extends Applet {

public void init() {

ShapeCanvas canvas = new ShapeCanvas(); // create the canvas

//JLayeredPane layers = new JLayeredPane();
//layers.add(canvas, new Integer(0));

setLayout(new BorderLayout());
add("Center",canvas); // add canvas and controls to the applet
}

} // end class ShapeDraw


class ShapeCanvas extends Canvas implements ActionListener, MouseListener {

Image offScreenCanvas = null; // off-screen image used for double buffering
Graphics offScreenGraphics; // graphics context for drawing to offScreenCanvas
Vector shapes = new Vector(); // holds a list of the shapes that are displayed on the canvas
Color currentColor = Color.red; // current color; when a shape is created, this is its color

ShapeCanvas() {

addMouseListener(this);

popup = new PopupMenu();
popup.add("UOFC");
popup.addSeparator();
popup.add("Details"); // this menu item must end with "Details" to evoke the action
add(popup);
popup.addActionListener(this);

addShape(new RoundRectShape());

} // end construtor

synchronized public void paint(Graphics g) {
makeOffScreenCanvas();
g.drawImage(offScreenCanvas,0,0,this);
}

public void update(Graphics g) {
paint(g);
}

void makeOffScreenCanvas() {
if (offScreenCanvas == null) {
offScreenCanvas = createImage(getSize().width,getSize().height);
offScreenGraphics = offScreenCanvas.getGraphics();
}
offScreenGraphics.setColor(getBackground());
offScreenGraphics.fillRect(0,0,getSize().width,getSize().height);
int top = shapes.size();
for (int i = 0; i < top; i++) {
Shape s = (Shape)shapes.elementAt(i);
s.draw(offScreenGraphics);
}
}

public void actionPerformed(ActionEvent evt) {
doPopupMenuCommand( evt.getActionCommand() );
}

synchronized void addShape(Shape shape) {
shape.setColor(currentColor);
shape.reshape(3,3,15,15);
shapes.addElement(shape);
repaint();
}


PopupMenu popup;

Shape selectedShape = null; // This is null unless a menu has been popped up on this shape.

Shape clickedShape(int x, int y) {
for ( int i = shapes.size() - 1; i >= 0; i-- ) { // check shapes from front to back
Shape s = (Shape)shapes.elementAt(i);
if (s.containsPoint(x,y))
return s;
}
return null;
}


void doPopupMenuCommand(String command) {
// Handle a command from the pop-up menu.
if (selectedShape == null) // should be impossible
return;
if (command.endsWith("Details")) {
selectedShape.setColor(Color.green);
selectedShape.moveTo(50, 50);
}
repaint();
}


synchronized public void mousePressed(MouseEvent evt) {
int x = evt.getX(); // x-coordinate of point where mouse was clicked
int y = evt.getY(); // y-coordinate of point

if (evt.isPopupTrigger()) { // If this is a pop-up menu event that
selectedShape = clickedShape(x,y); // occurred over a shape, record which shape
if (selectedShape != null) // it is and show the menu.
popup.show(this,x,y);
}
}

synchronized public void mouseReleased(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();

if (evt.isPopupTrigger()) { // If this is a pop-up menu event that
selectedShape = clickedShape(x,y); // occurred over a shape, record the
if (selectedShape != null) // shape and show the menu.
popup.show(this,x,y);
}
}

public void mouseEntered(MouseEvent evt) { } // Other methods required for MouseListener and
public void mouseExited(MouseEvent evt) { } // MouseMotionListener interfaces.
public void mouseMoved(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseDragged(MouseEvent evt) { }


} // end class ShapeCanvas



abstract class Shape {

int left, top; // Position of top left corner of rectangle that bounds this shape.
int width, height; // Size of the bounding rectangle.
Color color = Color.white; // Color of this shape.

void reshape(int left, int top, int width, int height) {
// Set the position and size of this shape.
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}

void resize(int width,int height) {
// Set the size without changing the position
this.width = width;
this.height = height;
}

void moveTo(int x, int y) {
// Move upper left corner to the point (x,y)
this.left = x;
this.top = y;
}

void moveBy(int dx, int dy) {
// Move the shape by dx pixels horizontally and dy pixels veritcally
// (by changing the position of the top-left corner of the shape).
left += dx;
top += dy;
}

void setColor(Color color) {
// Set the color of this shape
this.color = color;
}

boolean containsPoint(int x, int y) {
if (x >= left && x < left+width && y >= top && y < top+height)
return true;
else
return false;
}

abstract void draw(Graphics g);
// Draw the shape in the graphics context g.
// This must be overriden in any concrete subclass.

} // end of class Shape



class RectShape extends Shape {
// This class represents rectangle shapes.
void draw(Graphics g) {
g.setColor(color);
g.fillRect(left,top,width,height);
g.setColor(Color.black);
g.drawRect(left,top,width,height);
}
}


class OvalShape extends Shape {
// This class represents oval shapes.
void draw(Graphics g) {
g.setColor(color);
g.fillOval(left,top,width,height);
g.setColor(Color.black);
g.drawOval(left,top,width,height);
}
boolean containsPoint(int x, int y) {
// Check whether (x,y) is inside this oval, using the
// mathematical equation of an ellipse.
double rx = width/2.0; // horizontal radius of ellipse
double ry = height/2.0; // vertical radius of ellipse
double cx = left + rx; // x-coord of center of ellipse
double cy = top + ry; // y-coord of center of ellipse
if ( (ry*(x-cx))*(ry*(x-cx)) + (rx*(y-cy))*(rx*(y-cy)) <= rx*rx*ry*ry )
return true;
else
return false;
}
}


class RoundRectShape extends Shape {
// This class represents rectangle shapes with rounded corners.
// (Note that it uses the inherited version of the
// containsPoint(x,y) method, even though that is not perfectly
// accurate when (x,y) is near one of the corners.)
void draw(Graphics g) {
g.setColor(color);
g.fillRoundRect(left,top,width,height,width/3,height/3);
g.setColor(Color.black);
g.drawRoundRect(left,top,width,height,width/3,height/3);
}
}

   <<Less
About | Sitemap | Contact