【问题标题】:Java (Graphics2D): triangle drawn by created Graphics2D not visible until second repaintJava(Graphics2D):由创建的 Graphics2D 绘制的三角形在第二次重绘之前不可见
【发布时间】:2018-01-28 22:24:26
【问题描述】:

我有以下最少的代码来画一条带箭头的线:

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 会滞后。

为什么箭头不能和线一起画?真的没有人可以帮助我吗?

【问题讨论】:

  • 更多上下文可能会有所帮助。让我们看看您在哪里使用此代码的minimal reproducible example 代码。侧不(与手头的问题无关):永远不要从绘画方法中调用setBackground(...)。这属于其他地方,可能在构造函数或事件侦听器中,但从不在绘画方法中。
  • 好的,我把代码扩展了一下。
  • 我想,是的。方法 drawArrowHead 来自 Stack Overflow 上的另一个线程。

标签: java swing graphics awt graphics2d


【解决方案1】:

您还没有发布真正的MCVE,因此不可能知道您可能做错了什么,但是您不需要在回答中使用的拼凑,您可以在其中重新调用repaint()油漆组件。如果您在您自己的代码方面仍需要帮助,请发布有效的 MCVE,我们可以在不修改的情况下编译和运行代码。有关 MCVE 的示例,请阅读 MCVE link 并查看我在下面的答案中发布的示例 MCVE。

话虽如此,请理解 Swing 图形通常是被动的,这意味着您将让程序根据事件更改其状态,然后调用 repaint()建议 Swing 重绘管理器调用油漆。不能保证会发生绘制,因为“堆叠”的重绘请求可能会被忽略,这些请求由于在短时间内调用了许多而正在备份。

因此,在您的情况下,我们可以使用您的代码并对其进行修改,看看它是如何工作的。假设我给我的 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 对象

【讨论】:

  • 好的,我明白了。我已经更新了我的问题。你能帮帮我吗?
  • 抱歉,我仍然无法让我的代码正常工作。你有什么想法吗?
【解决方案2】:

好吧,看来,除了再次调用 repaint 之外别无他法。我在paintComponent-method结束时确实喜欢这样:

if (repaint == false) {
    repaint = true;
} else {
    repaint = false;
    repaint();
}

因此,它只重绘一次。但是没有更清洁的解决方案吗?

【讨论】:

  • 这不是必需的,事实上repaint() 不应该在paintComponent 方法中被调用。
猜你喜欢
  • 2016-08-30
  • 2014-02-21
  • 2021-11-24
  • 2013-08-21
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多