如何用Java制作四色圆?
我的方式一直持续到这一步。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class CircleDraw extends Frame {
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
Shape square = new Rectangle2D.Double(100, 100,100, 100);
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
ga.setPaint(Color.green);
ga.fill(circle);
ga.setPaint(Color.red);
ga.draw(square);
}
public static void main(String args[]) {
Frame frame = new CircleDraw();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(300, 250);
frame.setVisible(true);
}
}
答案 0 :(得分:2)
您可以使用java.awt.geom.Arc2D
绘制圆形切片
见下面的代码,
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class CircleDraw extends Frame {
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D) g;
ga.setPaint(Color.YELLOW);
Arc2D arc2D4 = new Arc2D.Double(100.0f, 100.0f, 100.0f, 100.0f, 90, 270, Arc2D.PIE);
ga.fill(arc2D4);
ga.draw(arc2D4);
ga.setPaint(Color.GREEN);
Arc2D arc2D1 = new Arc2D.Double(100.0f, 100.0f, 100.0f, 100.0f, 90, 90d, Arc2D.PIE);
ga.fill(arc2D1);
ga.draw(arc2D1);
ga.setPaint(Color.RED);
Arc2D arc2D2 = new Arc2D.Double(100.0f, 100.0f, 100.0f, 100.0f, 360, 90d, Arc2D.PIE);
ga.fill(arc2D2);
ga.draw(arc2D2);
ga.setPaint(Color.BLUE);
Arc2D arc2D3 = new Arc2D.Double(100.0f, 100.0f, 100.0f, 100.0f, 180, 90d, Arc2D.PIE);
ga.fill(arc2D3);
ga.draw(arc2D3);
}
public static void main(String args[]) {
Frame frame = new CircleDraw();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(300, 250);
frame.setVisible(true);
}
}
out put会是这样的!