我有以下最少的代码来绘制带有箭头的线:

package gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class StateBtn extends JPanel {

    private static final long serialVersionUID = -431114028667352251L;

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        // enable antialiasing
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);


        // draw the arrow
        Line2D.Double line = new Line2D.Double(0, getHeight()/2, 20, getHeight()/2);
        drawArrowHead(g2, line);
        g2.draw(line);

        // If I call repaint() here (like in my answer below), it works

    }

    private void drawArrowHead(Graphics2D g2d, Line2D.Double line) {
        AffineTransform tx = new AffineTransform();

        tx.setToIdentity();
        double angle = Math.atan2(line.y2-line.y1, line.x2-line.x1);
        tx.translate(line.x2, line.y2);
        tx.rotate((angle-Math.PI/2d));

        Polygon arrowHead = new Polygon();
        arrowHead.addPoint(0,5);
        arrowHead.addPoint(-5,-5);
        arrowHead.addPoint(5,-5);

        Graphics2D g = (Graphics2D) g2d.create();
        g.setTransform(tx);
        g.fill(arrowHead);
        g.dispose();
    }

}


它是这样创建的:

package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame {

    private static final long serialVersionUID = 4085389089535850911L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(500, 500);
        setLocation(0, 0);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        StateBtn stateBtn = new StateBtn();
        stateBtn.setBounds(200,200,35,35);
        contentPane.add(stateBtn);
    }
}


线条绘制正确,但是箭头不可见,直到我调用repaint()为止。问题在于该元素是可拖动元素,因此每次位置更改时,我都必须两次调用repaint()。这将使代码更加复杂,并且GUI会变得很落后。

为什么不能只将箭头与直线绘制在一起?真的没有人可以帮助我吗?

最佳答案

您尚未发布真实的MCVE,因此无法知道您可能做错了什么,但是您无需在答案中使用过的麻烦,您可以在paintComponent中重新调用repaint()。如果您仍然需要自己的代码帮助,请发布有效的MCVE代码,我们可以对其进行编译和运行,而无需进行修改。有关我所说的MCVE的示例,请阅读MCVE link并查看我在下面的答案中发布的MCVE示例。

如此说来,请理解,Swing图形通常是被动的,这意味着您将让程序根据事件更改其状态,然后调用repaint(),这建议Swing重绘管理器调用paint。不能保证会进行绘制,因为可能会忽略由于“堆叠”而又由于许多调用而备份的重新绘制请求。

因此,根据您的情况,我们可以使用您的代码并对其进行修改以查看其工作原理。假设我给我的JPanel一个MouseAdapter-一个既是MouseListener又是MouseMotionListener的类,并且在此适配器中,我只设置了两个Point实例字段,即p0(用于最初按下鼠标的位置)和p1(用于鼠标的位置)。拖动或释放。我可以设置这些字段,然后调用repaint,然后让我的绘画方法使用p0和p1绘制箭头。因此,鼠标调整器可能如下所示:

private class MyMouse extends MouseAdapter {
    private boolean settingMouse = false;

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }
        p0 = e.getPoint();
        p1 = null;
        settingMouse = true; // drawing a new arrow
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        setP1(e);
        settingMouse = false; // no longer drawing the new arrow
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        setP1(e);
    }

    private void setP1(MouseEvent e) {
        if (settingMouse) {
            p1 = e.getPoint();
            repaint();
        }
    }
}


然后在绘画代码中,我将使用您的代码,对其进行修改以使其使用我的p0和p1点:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    if (p0 != null && p1 != null) {
        Line2D.Double line = new Line2D.Double(p0.x, p0.y, p1.x, p1.y);

        drawArrowHead(g2, line);
        g2.draw(line);
    }

}


整个shebang看起来像这样:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class StateBtn extends JPanel {
    // constants to size the JPanel
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final int AH_SIZE = 5; // size of arrow head -- avoid "magic"
                                            // numbers!

    // our start and end Points for the arrow
    private Point p0 = null;
    private Point p1 = null;

    public StateBtn() {
        // create and add a label to tell the user what to do
        JLabel label = new JLabel("Click Mouse and Drag");
        label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 42));
        label.setForeground(new Color(0, 0, 0, 50));
        setLayout(new GridBagLayout());
        add(label); // add it to the center

        // create our MouseAdapater and use it as both MouseListener and
        // MouseMotionListener
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        // only do this if there are points to draw!
        if (p0 != null && p1 != null) {
            Line2D.Double line = new Line2D.Double(p0.x, p0.y, p1.x, p1.y);

            drawArrowHead(g2, line);
            g2.draw(line);
        }

    }

    private void drawArrowHead(Graphics2D g2d, Line2D.Double line) {
        AffineTransform tx = new AffineTransform();

        tx.setToIdentity();
        double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
        tx.translate(line.x2, line.y2);
        tx.rotate((angle - Math.PI / 2d));

        Polygon arrowHead = new Polygon();
        arrowHead.addPoint(0, AH_SIZE); // again avoid "magic" numbers
        arrowHead.addPoint(-AH_SIZE, -AH_SIZE);
        arrowHead.addPoint(AH_SIZE, -AH_SIZE);

        Graphics2D g = (Graphics2D) g2d.create();
        g.setTransform(tx);
        g.fill(arrowHead);
        g.dispose(); // we created this, so we can dispose of it
        // we should **NOT** dispose of g2d since the JVM gave us that
    }

    @Override
    public Dimension getPreferredSize() {
        // size our JPanel
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouse extends MouseAdapter {
        private boolean settingMouse = false;

        @Override
        public void mousePressed(MouseEvent e) {
            // if we press the wrong mouse button, exit
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            p0 = e.getPoint();  // set the start point
            p1 = null;  // clear the end point
            settingMouse = true; // tell mouse listener we're creating a new arrow
            repaint();  // suggest a repaint
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            setP1(e);
            settingMouse = false; // no longer drawing the new arrow
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            setP1(e);
        }

        private void setP1(MouseEvent e) {
            if (settingMouse) {
                p1 = e.getPoint(); // set the end point
                repaint();  // and paint!
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        StateBtn mainPanel = new StateBtn();
        JFrame frame = new JFrame("StateBtn");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}


这段代码就是我的MCVE示例的含义。实际上,对于像样的MCVE来说,它有点大,但是可以。请编译并运行代码以查看它是否有效。如果这对您没有帮助,如果您仍必须在重涂调用中使用kludge,那么我敦促您创建自己的MCVE并将其与您的问题一起发布,然后对我发表评论,以便我可以看到它。

顺便说一句,有人问您是否可以像在drawArrowHead(...)方法中一样创建一个新的Graphics对象,是的,不仅可以,而且在处理AffineTransforms时也是首选方法,因为这样您可以不必担心转换可能对可能共享原始Graphics对象的边框和子组件产生的下游影响。同样,只要您遵循处理您自己创建的Graphics对象并且不处理JVM给您的Graphics对象的规则,就可以了。

10-07 15:48