How can I draw a curved line segment using QuadCurve2D.Double?

前端 未结 2 1018
小鲜肉
小鲜肉 2021-01-24 23:41

Here is the line of code where I declare the curve:

QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);

Now what code ca

相关标签:
2条回答
  • 2021-01-25 00:18

    I've made a minimum test case of what I think your describing here. This program works but I can't really help you unless I can see the code you are working with.

    import java.awt.geom.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class CurveDraw extends JFrame {
            public static void main(String[] args) {
                    CurveDraw frame = new CurveDraw();
                    frame.setVisible(true);
            }
            public CurveDraw() {
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setSize(400,400);
            }
            public void paint(Graphics g) {
                    QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);
                    ((Graphics2D)g).draw(curve);
            }
    }
    
    0 讨论(0)
  • 2021-01-25 00:30

    Works fine for me...

    enter image description here

    public class PaintQuad {
    
        public static void main(String[] args) {
            new PaintQuad();
        }
    
        public PaintQuad() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new PaintMyQuad());
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class PaintMyQuad extends JPanel {
    
            @Override
            protected void paintComponent(Graphics g) {
    
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
    
                QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);
    
                g2d.setColor(Color.RED);
                g2d.draw(curve);
    
            }
    
        }
    
    }
    

    Two things come to mind.

    1. Make sure you've set the color of the graphics, the default is the back ground color of the pane
    2. Make sure that the size of your container is large enough (and is layout correctly) to show the graphics.
    0 讨论(0)
提交回复
热议问题