Drawing multiple graphic objects using Java2D onto the screen and allowing the user to move, add, delete those objects.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Vowell
Posted On:   Thursday, September 26, 2002 11:18 AM

I need to be able to draw custom graphics using Java2D onto the user's screen and allow the user to manipulate those graphical objects. The user needs to be able to move, add, delete, etc. these graphical objects. My question is should I implement the Shape interface for each custom graphic object to be displayed, or rather should I extend JComponents and build the custom graphics using the JDK class that already implement Shape. The later sounds much better, but my problem is how to position these graphics on the users screen what kind of Swing container should I use to display multiple 2D graphics that would allow the user to reposition the objects. I don't think it is possible to add JComponents in this manner. Thank You,   More>>

I need to be able to draw custom graphics using Java2D onto the user's screen and allow the user to manipulate those graphical objects. The user needs to be able to move, add, delete, etc. these graphical objects.

My question is should I implement the Shape interface for each custom graphic object to be displayed, or rather should I extend JComponents and build the custom graphics using the JDK class that already implement Shape.

The later sounds much better, but my problem is how to position these graphics on the users screen what kind of Swing container should I use to display multiple 2D graphics that would allow the user to reposition the objects. I don't think it is possible to add JComponents in this manner.

Thank You,
Jason Vowell

   <<Less

Re: Drawing multiple graphic objects using Java2D onto the screen and allowing the user to move, add, delete those objects.

Posted By:   Rick_Gniewek  
Posted On:   Thursday, September 26, 2002 03:48 PM

You could accomplish this by using a single Panel or JPanel with a single Canvas. If you listen for each mouse click and track each shape object in an array structure you can iterate through and see which object within your panel the user clicked on. This also allows you to easily add layering capability.

Here's a quick example of how you can handle this problem:

// in the constructor...
ArrayList list = new ArrayList();

// a method to add shapes to the panel / canvas
public void addShape(CustomShape shape){
list.add(new CustomShape());
}

Override your paint() method in the panel or canvas..

public void paint(Graphics g){
for(int i = 0; i CustomShape shape = (CustomShape) list.get(i);
shape.render(g);
}
}

// create a method to select a shape from the list
public CustomShape selectShape(MouseEvent e){

// start from the bottom of the list since it will
// be from the top layer of your shapes
for(int i = list.size() -1; i > -1; i--){
CustomShape shape = (CustomShape) list.get(i);
if(shape.inside(e.getX(), e.getY()){
return shape;
}
}
return null;
}

// in the CustomShape class...

class CustomShape{
Color color;

public Color getColor(){ return color; }

public void render(Graphics g){
// here is where you would paint this object.
// since you are using the graphics from the parent,
// the x,y coordinates will origniate from the upper
// left corner of the panel or canvas.
// use g.drawLine, g.drawRect, g.drawOval, etc.
// to get whatever shape is required.
}
}


After you implement the above, you can now select a shape object. Once it is selected, you can drag it around on the screen by changing the x,y coordinates of the shape and then call repaint() on the canvas or panel. A DND listener can be used to detect drag gestures.
About | Sitemap | Contact