本来想让鼠标控制,点击一次鼠标就初始化SimpleApplet3类生成一个移动的小球,
但是为什么显示的只有一个,
有兴趣的话调试看看!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
class SimpleApplet3 extends Applet implements Runnable{
int x1=0,y1=0,w=30,h=30;
int red=0,green=0,blue=0;
public Thread timer;
boolean horizontal=true,verticalct=true;
public void init(){
this.start();
}
public void start(){
timer=new Thread(this);
red=(int)(Math.random()*1000%256);
green=(int)(Math.random()*1000%256);
blue=(int)(Math.random()*1000%256);
timer.start();
}
public void paint(Graphics g){
g.setColor(new Color(red,green,blue));
g.fillOval(x1,y1,w,h);
}
public void run(){
while(true){
if(horizontal==true) {
x1=x1+1;
if (x1>=770){
horizontal=false;
}
}else{
x1=x1-3;
if (x1<=0){
horizontal=true;
}
}
if(verticalct==true) {
y1=y1+2;
if (y1>=570){
verticalct=false;
}
}else{
y1=y1-4;
if (y1<=0){
verticalct=true;
}
}
try{
Thread.currentThread().sleep(10);
}catch(Exception e1){
return;
}
repaint(x1-5,y1-5,40,40); }
}
}
--------------------------------
public class SimpleApplet2 extends SimpleApplet3{
public void init(){
this.setSize(800,600);
this.setBackground(Color.darkGray);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
new SimpleApplet3().start();
System.out.print(new SimpleApplet3().x1+"-"+new SimpleApplet3().y1+";");
}
});
}
}
------------------------
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "SimpleApplet2.class"
width = "500"
height = "300"
>
</APPLET></CENTER>
</BODY>
</HTML>
但是为什么显示的只有一个,
有兴趣的话调试看看!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
class SimpleApplet3 extends Applet implements Runnable{
int x1=0,y1=0,w=30,h=30;
int red=0,green=0,blue=0;
public Thread timer;
boolean horizontal=true,verticalct=true;
public void init(){
this.start();
}
public void start(){
timer=new Thread(this);
red=(int)(Math.random()*1000%256);
green=(int)(Math.random()*1000%256);
blue=(int)(Math.random()*1000%256);
timer.start();
}
public void paint(Graphics g){
g.setColor(new Color(red,green,blue));
g.fillOval(x1,y1,w,h);
}
public void run(){
while(true){
if(horizontal==true) {
x1=x1+1;
if (x1>=770){
horizontal=false;
}
}else{
x1=x1-3;
if (x1<=0){
horizontal=true;
}
}
if(verticalct==true) {
y1=y1+2;
if (y1>=570){
verticalct=false;
}
}else{
y1=y1-4;
if (y1<=0){
verticalct=true;
}
}
try{
Thread.currentThread().sleep(10);
}catch(Exception e1){
return;
}
repaint(x1-5,y1-5,40,40); }
}
}
--------------------------------
public class SimpleApplet2 extends SimpleApplet3{
public void init(){
this.setSize(800,600);
this.setBackground(Color.darkGray);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
new SimpleApplet3().start();
System.out.print(new SimpleApplet3().x1+"-"+new SimpleApplet3().y1+";");
}
});
}
}
------------------------
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "SimpleApplet2.class"
width = "500"
height = "300"
>
</APPLET></CENTER>
</BODY>
</HTML>
int x1[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int y1[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
static int i=-1;
int w=0,h=0;
boolean horizontal[22];
boolean verticalct[22];
public void init(){
this.setSize(800,600);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
aa();
}
});
}
public void aa(){
if (i<=20){
i++;
w=50;h=50;
new Thread(this).start();
System.out.print(i+" ");
}else{
System.out.print(" OK"+i);
}
}
public void paint(Graphics g){
for(int j=0;j<=i;j++){
g.setColor(Color.blue);
g.fillOval(x1[j],y1[j],w,h);
}
}
public void run(){
while(true){
for(int j=0;j<=i;j++){
if(horizontal[j]==true) {
x1[j]=x1[j]+1;
if (x1[j]>=800-w){
horizontal[j]=false;
}
}else{
x1[j]=x1[j]-3;
if (x1[j]<=0){
horizontal[j]=true;
}
}
if(verticalct[j]==true) {
y1[j]=y1[j]+2;
if (y1[j]>=600-h){
verticalct[j]=false;
}
}else{
y1[j]=y1[j]-4;
if (y1[j]<=0){
verticalct[j]=true;
}
}
repaint(x1[j]-5,y1[j]-5,w+10,h+10);
try{
Thread.currentThread().sleep(20);
}catch(Exception e1){
return;
}
}
}
}
}