【发布时间】:2017-07-20 18:40:37
【问题描述】:
我目前正计划编写一些用于碰撞检测的代码。但是,我遇到了一个问题。我想在 JFrame 窗口上绘制多个球体,但以下代码不起作用...请帮帮我... 这是我的代码:-
import javax.swing.*;
import java.awt.*;
class Draw extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<20;i++)
drawing(g,new Sphere());
}
public void drawing(Graphics g,Sphere s)
{
g.setColor(s.color);
g.fillOval(s.x,s.y,s.radius*2,s.radius*2);
}
public static void main(String args[])
{
JFrame jf = new JFrame("Renderer");
jf.getContentPane().add(new Draw(),BorderLayout.CENTER);
jf.setBounds(100,100,400,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class Sphere
{
int x;
int y;
int radius;
Color color;
public Sphere()
{
this.x = (int)Math.random()*100;
this.y = (int)Math.random()*100;
this.radius = (int)Math.random()*20;
this.color = new Color((int)(Math.random()*255),
(int)(Math.random()*255),(int)(Math.random()*255));
}
}
【问题讨论】:
-
“不工作”是什么意思?你期望什么,你的程序做什么?
-
好吧,我的意思是球体根本没有在屏幕上绘制。只出现一个空白窗口。