开发者

Java: Adding JComponents to a JPanel with static sizing and positioning

开发者 https://www.devze.com 2023-02-26 05:24 出处:网络
I have a program that reads and draws an SVG file in a java application without the use of third party libraries. I have gotten to the point where I can replicate the file by drawing shape\'s onto a g

I have a program that reads and draws an SVG file in a java application without the use of third party libraries. I have gotten to the point where I can replicate the file by drawing shape's onto a graphics object, however I would like to make each element (Rect/Circle/Line etc) selectable by applying 开发者_如何学JAVAlisteners to each Object.

To do so I am under the impression I need to create a Class that extends JComponent, draws the Object within the component and adds a listener to it for each element that is to be displayed. So I would have a group of container components(if you can call them that) with listeners attached, each corresponding to a particular element in the file.

I then need to draw these components onto a JPanel or suitable Container, however to do so i would need to use a null layout and set the positions and size of each component within the JPanel/Container manually.

So in conclusion although this is exactly what I want to do, I am wondering if there is a more standardized approach to adding listeners to Graphics Objects that I am not aware of?

Here is a sample of the code in question that I am looking to extend by using the above approach, I've simplified it alot so I hope it still makes sense

public class View extends JComponent implements SVGViewport {

    // Model of the SVG document
    private SVGDocument document;

    /** Paint method */
    @Override
    protected void paintComponent(Graphics g) {
        paint2D((Graphics2D) g);
    }

    /** Paints the entire view */
    private void paint2D(Graphics2D g) {
        // Paint Document properties
        ....

        // Paint document Elements
        for (SVGElement elem : document) {  
            paintElement(g, elem);
        }
    }

    /** Paints a single element on the graphics context */
    public void paintElement(Graphics2D g, SVGElement elem) {

        //Get a drawable shape object for this element
        Shape shape = elem.createShape();

        //Set Fill, stroke, etc attributes for this shape
        ....
        // Fill the interior of the shape
        ....
        // Stroke the outline of the shape
        ....
        g.draw(shape);
    }

    /** Gets the document currently being displayed by the view. */
    public SVGDocument getDocument() {
        return document;
    }

    /** set and re-paint the document */
    public void setDocument(SVGDocument document) {
        this.document = document;
        repaint();
    }
}


A JLayeredPane is the traditional approach, as seen in this example. An alternative approach using paintComponent() may be seen in GraphPanel.

Addendum: Regarding JLayeredPane, see also this tutorial, this related example, and this variation.


OOdium,

What you describe certainly sounds reasonable and, to my knowledge, there is not native support for events in the 2D API. You will have to be careful if there is overlap between the visual elements. Another way to go about it would be to have a JComponent on top of the canvas (Z-order or Glass Pane) that is responsible for determining which item was clicked and then firing the appropriate event. This might be necessary if there are overlapping visual entities. Here is the docs from Sun for the layering: http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

I am not a graphics guy though and I expect there is a standard solution to your requirements given how often that a programmer would want to do what you want to do. You might want to try googling for 2D games built in Java and see if you can learn from what they did.

Regards,

Guido

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号