[英]Java - Calling a paintComponent Method
我想有一個可以通過使用給定的x,y,color參數調用其方法來重新創建的圓。 但是我很難做到這一點。 我想將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);
}}
需要向ring()
方法添加graphics2D
參數,如下所示:
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);
}
並使用graphics2D
參數調用ring()
:
ring(10 , 20 , "Blue", g2);
我認為應該可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.