我需要做,看起來像Java的製作方法畫圓調用drawOval
public void drawCircle(int x, int y, int radius)
,吸引與圓心和半徑的圓圈畫圓一個方法。 drawCircle方法需要調用drawOval。我不知道如何從drawCircle方法調用drawOval而不將Graphics傳遞給它。這可能嗎?
繼承人我有:
import java.awt.*;
import javax.swing.*;
class test
{
public static void main(String[] args)
{
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
}
class MyPanel extends JPanel
{
MyPanel()
{
setBackground(Color.WHITE);
setPreferredSize(new Dimension(250,250));
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
drawCircle(50,50,20);
}
private void drawCircle(int x, int y, int radius)
{
drawOval(x - radius, y - radius, radius*2, radius*2);
}
}
爲什麼你必須通過傳遞圖形來做到這一點? – 2010-07-27 23:47:32
你不必在paintComponent中調用drawOval,比如g.drawOval(),其中g是Graphics? – Raptrex 2010-07-27 23:49:00
通過「將圖形傳遞給它」來引用Java庫對象嗎?發佈更完整的代碼。 – 2010-07-27 23:49:09