我希望有一个圆,可以用给定的x,y,颜色参数调用它的方法来重新创建。但我很难做到。我希望使用JComponent作为对象而不是组件。
public class OlympicRingsComponent extends JComponent {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(10, 10);
g2.setStroke(new BasicStroke(7));
Ellipse2D.Double circle = new Ellipse2D.Double(0,0,100,100);
g2.setPaint(Color.BLUE);
g2.draw(circle);
}}这个代码很好用。但是我希望能够调用一个方法来创建一个新的椭圆。
public class OlympicRingsComponent extends JComponent {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(10, 10);
g2.setStroke(new BasicStroke(7));
ring(10 , 20 , "Blue");
}
public void ring(int x , int y , String color) {
Ellipse2D.Double circle = new Ellipse2D.Double( x , y ,100,100);
g2.setPaint(Color.getColor(color));
g2.draw(circle);
}}发布于 2014-03-04 18:49:05
需要向graphics2D方法添加ring()参数,如下所示:
public void ring(int x , int y , String color, graphics2D g2) {
Ellipse2D.Double circle = new Ellipse2D.Double( x , y ,100,100);
g2.setPaint(Color.getColor(color));
g2.draw(circle);
}并使用ring()参数调用graphics2D:
ring(10 , 20 , "Blue", g2);我觉得这应该管用。
https://stackoverflow.com/questions/22180008
复制相似问题