我一直在尝试使用以下代码在java中绘制圆弧:
g.fillArc(50, 50, 100, 100, 0, 180)
其中 g 是图形对象。
产生下面的蓝色对象:
我实际上想做的是制作如下所示的东西:
预先感谢您的帮助!
最佳答案
尝试使用 g.fillArc(50, 50, 100, 100, 180, 180)
代替。
基本上,第一个角度是从哪里开始,第二个角度是它应该弧过的度数(从开始处算起)。
因此,如果您只想要 5 度的饼滑,您可以使用类似 g.fillArc(50, 50, 100, 100, 0, 5)
看看Graphics#fillArc
和 Graphics2D了解更多信息
工作示例
public class PaintTest03 {
public static void main(String[] args) {
new PaintTest03();
}
public PaintTest03() {
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("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillArc(0, 0, 100, 100, 180, 180);
}
}
}
关于java - 尝试使用Java绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426313/