Java Line2D始终从JFrame的左上角开始

时间:2013-12-06 07:11:29

标签: java swing

这是较大任务的一部分,用户必须能够绘制,移动和调整方块,圆和线的大小。此外,这些流程必须可见。

我对方块和圆圈没有任何问题,但是当我尝试绘制一条线时,就会出现问题。

这是绘制线条的代码,但仍然会产生这个错误:

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;

import javax.swing.JFrame;

public class test extends JFrame implements MouseListener,
MouseMotionListener {
Point startDrag, endDrag;

/**
 * @param args
 */
public test() {

    this.addMouseListener(this);
    this.addMouseMotionListener(this);
}
public static void main(String[] args) {
    test frame = new test();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
Shape r = null;
public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(1));
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            0.50f));

    if(startDrag != null && endDrag != null)
    {
        r = makeLine(startDrag.x, startDrag.y, endDrag.x,
            endDrag.y);
    }


    if(r != null)
        g2.draw(r);
}

@Override
public void mouseDragged(MouseEvent e) {
    endDrag = new Point(e.getX(), e.getY());
    repaint();

}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent me) {
    startDrag = new Point(me.getX(), me.getY());
    repaint();
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent me) {
    startDrag = new Point(me.getX(), me.getY());
    endDrag = startDrag;
    repaint();

}

private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
    return new Line2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1- x2), Math.abs(y1 - y2));
}
@Override
public void mouseReleased(MouseEvent me) {

    r = makeLine(startDrag.x, startDrag.y, me.getX(), me.getY());

    startDrag = null;
    endDrag = null;
    repaint();

}

}

我刚刚开始学习如何使用awt和swing,所以任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:0)

主要问题在于......

private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
    return new Line2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1- x2), Math.abs(y1 - y2));
}

如果第二个x / y小于或大于第一个(或任何一个的组合),则行不关心,它只会从一个点到下一个点画一条线。

因为您已经有一个代表各个鼠标事件的起点和终点,所以您可以按原样使用它们

private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
    return new Line2D.Float(x1, y1, x2, y2);
}

我还删除了startPoint事件中mouseClicked的更改,因为它无法执行您想要的操作;)

我发布了用于测试它的代码,我在评论中提出了一些建议的更改......

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawLine extends JPanel implements MouseListener,
                MouseMotionListener {

    Point startDrag, endDrag;

    public DrawLine() {

        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DrawLine());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    Shape r = null;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(1));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                        0.50f));

        if (startDrag != null && endDrag != null) {
            System.out.println(startDrag + " - " + endDrag);
            r = makeLine(startDrag.x, startDrag.y, endDrag.x,
                            endDrag.y);
        }

        if (r != null) {
            g2.draw(r);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        endDrag = new Point(e.getPoint());
        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent me) {
//        startDrag = new Point(me.getX(), me.getY());
//        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent me) {
        startDrag = new Point(me.getPoint());
        repaint();

    }

    private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
        return new Line2D.Float(x1, y1, x2, y2);
    }

    @Override
    public void mouseReleased(MouseEvent me) {

        r = makeLine(startDrag.x, startDrag.y, me.getX(), me.getY());

        startDrag = null;
        endDrag = null;
        repaint();

    }
}