我需要做,看起来像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