public static void draw (Graphics g, int x, int y, int width, int height) {
if (height == 0) return;
g.drawOval (x, y, width, height);
draw (g, x, y, width/2, height/2);
draw (g, x+width/2, y, width/2, height/2);
}
所以在上面的代码中,我试图运行测试,但它一直给我错误。这显然是一个硬件问题,我认为我有代码部分,但不管你信不信,我们还没有真正涵盖如何正确测试小说。所以我在努力:
public static void main (String[] args) {
draw (g, 1, 2, 3, 4);
//draw (null, 1, 2, 3, 4);
}
任何帮助都将被挪用。谢谢。
答案 0 :(得分:2)
您使用的是swing
吗?
你需要一个图形对象来绘制!
怎么样
public static void main(String[] args){
JFrame f=new JFrame(); // create a window object in which to place a component
f.add(new JLabel(){ // create a component within the window
public void paint(Graphics g){ // override paint on this component to get a drawing area
super.paint(g); // call the default drawing command to draw background
draw(g, 1,2,3,4); // call your drawing routine
}
});
f.pack(); // make frame big enough for the label
f.setVisible(true); // show the frame, with its contents.
// Only after this is your routine actually called to do the drawing.
} // the main program now exits, but Java's event thread continues to run in the background,
// drawing your object until the window is closed.
答案 1 :(得分:0)
根据您要做的事情,首先制作JFrame并向其添加面板可能很有用,然后覆盖JPanel的绘制功能以进行绘制。
JFrame创建一个框架来保存信息。然后添加面板,以便绘制一个曲面。
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new JPanel()
{
public void paint(Graphics g)
{
super.paint(g);
drawLine(g, 10,10,100,100);
}
});
frame.setVisible(true);
}
所以你有效地做了什么:
我希望这可以解决一些问题:)