这里是代码行,我宣布曲线:如何使用QuadCurve2D.Double绘制曲线段?
现在我可以用什么代码绘制该曲线QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);
?我试过类似的东西:
g.draw(curve);
但显然没有工作。有什么建议么?
这里是代码行,我宣布曲线:如何使用QuadCurve2D.Double绘制曲线段?
现在我可以用什么代码绘制该曲线QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);
?我试过类似的东西:
g.draw(curve);
但显然没有工作。有什么建议么?
我已经做了一个我认为你在这里描述的最低测试案例。 这个程序的工作原理,但我不能真正帮助你,除非我能看到你正在使用的代码。
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);
}
}
+1。 [请参阅文档](http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#draw(java.awt.Shape))。 QuadCurve2D实现Shape。 – ignis
请不要直接绘制顶层容器,请调用'super.paint(g)'。虽然我很欣赏这只是一个测试案例,但它是适当编码实践的一个不好的例子,我们处理的问题已经足够多了,因为它们(对不起,我很挑剔,但它可以为您赢得一票否决权) – MadProgrammer
工作正常,我...
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);
}
}
}
两件事情浮现在脑海中。
正如Tiger指出的那样,您不是在Graphics上绘制它,而是在Graphics2D上绘制它。 Swing的paintComponent(Graphics g)---> g实际上是一个Graphics2D。 – ignis