谁能帮我看下 这段代码 就是只有一个星星 但要实现好多星星啊

apirlapple 2015-07-29 09:58:05

import java.awt.*;
public class Mysnow{

public static void main(String[] args){
Frame w=new Frame();
w.setSize(500,500);
w.setBackground(Color.BLACK);
MyPanel mp=new MyPanel();
w.add(mp);
Thread t=new Thread(mp);
t.start();
w.show();



}
static class MyPanel extends Panel implements Runnable{
int x[]=new int[300];
int y[]=new int[300];
public void Mypanel(){
for(int i=0;i<300;i++){
x[i]=(int)(Math.random()*500);
y[i]=(int)(Math.random()*500);
}
}
public void paint(Graphics g){
g.setColor(Color.RED);
for(int i=0;i<300;i++) {
g.drawString("*",x[i],y[i]);
}
}

public void run(){
while(true){

for(int i=0;i<300;i++){
y[i]++;
if(y[i]>500){
y[i]=0;
}
}
try{
Thread.sleep(20);
} catch(Exception e){}
repaint();
}


}
}
}
...全文
107 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoxin4415 2015-07-29
  • 打赏
  • 举报
回复
楼主的想法应该是用public void Mypanel做为构造函数来初始化xy坐标数组吧?
但是楼主你的方法声明的方式有误:1.构造函数无返回值;2.你的函数名写错了,应该和类名一致;
改成如下这样实现了楼主的想法:

public MyPanel(){
for(int i=0;i<300;i++){
x[i]=(int)(Math.random()*500);
y[i]=(int)(Math.random()*500);
}
}

运行结果:
cricel 2015-07-29
  • 打赏
  • 举报
回复
你没有调用你定义的public void Mypanel() 方法,所以不能使用
import java.awt.*;

public class tests1 {

	public static void main(String[] args) {
		Frame w = new Frame();
		w.setSize(500, 500);
		w.setBackground(Color.BLACK);
		MyPanel mp = new MyPanel();
		//////////////////////////////////////////////
		mp.Mypanel();/////////////////////////////////
		/////////////////////////////////////////////
		w.add(mp);
		Thread t = new Thread(mp);
		t.start();
		w.show();

	}

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧