Paint program - layers

Hi i am in the process of making a paint program and have decided to go through the (hopefully not) long process of using layers.

at the moment all shapes, when drawn, are stored in a vector and then painted onto the graphics of a class extending JPanel.

what i want to do is store them in separate images, all the size of the JPanel canvas (so they can be positioned at (0, 0)) and anyt area of the image that isnt part of the shape should be transparent.

i have tried creating a new graphics object for each shape and then painting them onto an ARGB buffered image then storing all the images in a new vector and finally painting them onto the canvas however this isnt working.

is there an easier way to do this, or does anybody know what has happened?

thanks

[798 byte] By [boblettoj99a] at [2007-11-27 7:27:16]
# 1

If they're in a vector, they're basically already in a kind of layered system, of course.

If what you want is to have all the shapes live in their own coordinate system, and then be able to assign relative positions to draw their system in a larger coordinate system...well, I thought that Java2D already handled that. But if it doesn't, then rather than drawing in images, another thing you can do is create a class that represents a coordinate system. Basically, all it would have to do is have two state elements: a reference to a shape, and a reference to an (x,y) pair where you want to anchor that shape for drawing. Then your vector could hold these objects.

It wouldn't have to be much more than:

class Layer {

private Shape shape;

private Point location;

// constructor that sets these; shape could be final, but location probably shouldn't be

public void paint(Graphics g) {

// maybe use AffineTransform to move the shape to the given location, then

// paint it?

}

}

You should probably check on a forum that does more with GUIs though.

But, if you want to, you can also make each layer just be an image. You'd gain some of the operations of images (like Graphics knowing how to scale them) but might lose image quality. There are a bunch of tutorials about double buffering that will tell you how to create an intermediate image; you could apply that to make multiple images, if you wanted to.

paulcwa at 2007-7-12 19:07:39 > top of Java-index,Java Essentials,New To Java...