0

我正在编写一个java小程序。

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

我已经成功地实现了标记,作为 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);
    }
4

1 回答 1

0

这个方法解决!

我不得不完全改变解决问题的方法。

于 2012-09-26T00:48:14.920 回答