Java,创建扩展JComponent的行对象

时间:2012-09-25 15:54:34

标签: java draw extend jcomponent

我正在编写一个java applet。

在这个小程序中,我需要在图像上绘制一些标记(如红色圆圈)和一些线条。

我已经成功地实现了标记,作为JComponent的扩展,我还把这些标记放在了一些鼠标上。

我遇到了线对象的大问题。我创建了另一个扩展JComponent的对象,除了我在坐标系统上遇到一些问题之外,setDimension还会产生麻烦。例如,它拦截所有标记的点击。

这不是一种让对象“尺寸”更紧密的方法,因为我不能只绘制线条或水平线......

感谢大家。

修改

public class Path extends JComponent {
...
    // stroke of the line
    private Stroke spessore =  new BasicStroke(SPESSORE);

    // coordinates
    private double x, y, x_2, y_2;

// ZoomManager is an object. In this project I can zoom in and zoom out the
    // image, so this object convert coordinates get on the superior JPanel in 
    // coordinates on the image real-sized.
    public Path(double x, double y, ZoomManager zoom) {//, double x_2, double y_2, ZoomManager zoom) {
            super();

            // this function return the coordinates on the real-sized image
            Point a = DrawableObjects.getScaledCoordinates(x, y, zoom);
            this.x = a.x;
            this.y = a.y;

            this.x_2 = a.x;
            this.y_2 = a.y;

            updateBoundsAndSize(zoom);

            // this was only for test...
            this.addMouseListener(new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.out.println("CLICK!");
                    }
                    ...
            });
    }

    // this function is called during the mouse dragging for drow the line.
    // it gets the coordinates, convert them, save them and update the bounds and 
    // size of the object
    public void setArrivePoint(Point a, ZoomManager zoom) {
            Point p = DrawableObjects.getScaledCoordinates(a.x, a.y, zoom);
            this.x_2 = p.x;
            this.y_2 = p.y;
            updateBoundsAndSize(zoom);
    }

    // update the bounds of the object, the origin point of the rectangle is the
    // top-left coordinate build with the original coordinates. The width and height of the rectangle are obtained by subtraction. 
    private void updateBoundsAndSize(ZoomManager zoom) {

            Point p = DrawableObjects.getPanelCoordinates(x, y, zoom);
            Point a = DrawableObjects.getPanelCoordinates(x_2, y_2, zoom);

            int min_x = (int)Math.min(p.x, a.x) - SPESSORE;
            int min_y = (int)Math.min(p.y, a.y) - SPESSORE;

            if (min_x < 0)
                    min_x =0;

            if (min_y < 0)
                    min_y = 0;

            int w = (int) (Math.max(a.x, p.x) - min_x) + SPESSORE;
            int h = (int) (Math.max(a.y, p.y) - min_y) + SPESSORE;

            setBounds(new Rectangle(min_x, min_y, w, h));
            repaint();
    }

    // drawing function   
    @Override
    protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D antiAlias = (Graphics2D) g;
            antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // get ZoomManager from the superior object
            ZoomManager zoom = ((JPanelImmagine)this.getParent()).zoom;

            antiAlias.setColor(DEFAULT_COLOR);
            antiAlias.setStroke(spessore);

            Point[] coordinates = updateCoordinates(zoom);

            Line2D line = new Line2D.Double(coordinates[0], coordinates[1]);

            antiAlias.draw(line);

    }

    // translate coordinates from superior jpanel to this object
    private Point[] updateCoordinates(ZoomManager zoom) {

            Point[] output = new Point[2];

            Point p = DrawableObjects.getScaledCoordinates(x, y, zoom);
            Point a = DrawableObjects.getScaledCoordinates(x_2, y_2, zoom);

            double o_x = this.getBounds().getCenterX();
            double o_y = this.getBounds().getCenterY();
            Point origin = new Point ((int)o_x, (int)o_y);

            output[0] = calculateCoordinates(p, origin);
            output[1] = calculateCoordinates(a, origin);

            return output;
    }

    private Point calculateCoordinates(Point p, Point origin) {

            double new_x = p.x - origin.x;
            double new_y = p.y - origin.y;

            return new Point((int)new_x, (int)new_y);
    }

1 个答案:

答案 0 :(得分:0)

使用this method解决了!

我必须彻底改变问题。