public void start(){
sowSeed();
if(imageThread==null){
imageThread=new Thread(this);
imageThread.start();
//这儿少了一个}
}
sowSeed();
if(imageThread==null){
imageThread=new Thread(this);
imageThread.start();
//这儿少了一个}
}
import java.applet.*;
import java.util.*;
import java.io.*;
public class QixTest extends Applet implements Runnable{
Thread imageThread=null;
Random rnd=new Random();
Rectangle bounceRect;
Rectangle colorBounce;
BouncyPoint endPoint1,endPoint2;
BouncyPoint R_Bouncer,G_Bouncer,B_Bouncer;
int x1,x2,y1,y2;
public void init(){
bounceRect=getBounds();
bounceRect.x=0;
bounceRect.y=0;
}
public void start(){
sowSeed();
if(imageThread==null){
imageThread=new Thread(this);
imageThread.start();
}
}
public void stop()
{
if((imageThread!=null)&&imageThread.isAlive())
{
imageThread.stop();
}
imageThread=null;
}
public void run()
{
int i=0;
Thread me=Thread.currentThread();
me.sePriority(Thread.MIN_PRIORITY);
while(imageThread==me)
{
repaint();
try{
Thread.sleep(10);
}catch(InterruptedExpection e){}
i++;
if(i==1000){
sowSeed();
i=0;
}
}
}
public void update(Graphics g){
endPoint1.carryOnBouncing();
endPoint2.carryOnBouncing();
R_Bouncer.carryOnBouncing();
G_Bouncer.carryOnBouncing();
B_Bouncer.carryOnBouncing();
g.setColor(new Color(R_Bouncer.x,G_Bouncer.x,B_Bouncer.x));
g.drawLine(endPoint.x,endPoint.y,endPoint2.x,endPoint2.y);
}
public void sowSeed(){
x1=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
x2=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
y1=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
y2=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
endPoint1=new BouncyPoint(bounceRect,x1,y1,-1.0,1,5);
colorBounce=new Rectangle(0,0,255,255);
R_Bouncer=new BouncyPoint(colorBounce,200,0,-1.0,0.0);
B_Bouncer=new BouncyPoint(colorBounce,200,0,-2.0,0.0);
}
class BouncyPoint extends Point{
Rectangle boundingBox;
double x_direction;
double y_direction;
BouncyPoint(Rectangle limits,int startx,int starty,double dx,double dy)
{
super(startx,starty);
boundingBox=limits;
x_direction=dx;
y_direction=dy;
}
public void carryOnBouncing()
{
x+=(int)x_direction;
if(!boundingBox.contains(x,y)){
y-=(int)y_direction;
y_direction=-y_direction;
}
}
}
}
你看看现在可以了吗,我给你找出来两个错误
import java.applet.*;
import java.util.*;
import java.io.*;
public class QixTest extends Applet implements Runnable{
Thread imageThread=null;
Random rnd=new Random();
Rectangle bounceRect;
Rectangle colorBounce;
BouncyPoint endPoint;//加这1个变量
BouncyPoint endPoint1,endPoint2;
BouncyPoint R_Bouncer,G_Bouncer,B_Bouncer;
int x1,x2,y1,y2;
public void init(){
bounceRect=getBounds();
bounceRect.x=0;
bounceRect.y=0;
}
public void start(){
sowSeed();
if(imageThread==null){
imageThread=new Thread(this);
imageThread.start();
}
}
public void stop()
{
if((imageThread!=null)&&imageThread.isAlive())
{
imageThread.stop();
}
imageThread=null;
}
public void run()
{
int i=0;
Thread me=Thread.currentThread();
//me.sePriority(Thread.MIN_PRIORITY);
while(imageThread==me)
{
repaint();
try{
Thread.sleep(10);
}catch(InterruptedException e){}//Expection 写错了
i++;
if(i==1000){
sowSeed();
i=0;
}
}
}
public void update(Graphics g){
endPoint1.carryOnBouncing();
endPoint2.carryOnBouncing();
R_Bouncer.carryOnBouncing();
G_Bouncer.carryOnBouncing();
B_Bouncer.carryOnBouncing();
g.setColor(new Color(R_Bouncer.x,G_Bouncer.x,B_Bouncer.x));
g.drawLine(endPoint.x,endPoint.y,endPoint2.x,endPoint2.y);//
}
public void sowSeed(){
x1=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
x2=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
y1=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
y2=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
endPoint1=new BouncyPoint(bounceRect,x1,y1,-1.0,1,5);//还剩下一个错误了,你自己差一下这,好象是你的参数不对。
colorBounce=new Rectangle(0,0,255,255);
R_Bouncer=new BouncyPoint(colorBounce,200,0,-1.0,0.0);
B_Bouncer=new BouncyPoint(colorBounce,200,0,-2.0,0.0);
}
class BouncyPoint extends Point{
Rectangle boundingBox;
double x_direction;
double y_direction;
BouncyPoint(Rectangle limits,int startx,int starty,double dx,double dy)
{
super(startx,starty);
boundingBox=limits;
x_direction=dx;
y_direction=dy;
}
public void carryOnBouncing()
{
x+=(int)x_direction;
if(!boundingBox.contains(x,y)){
y-=(int)y_direction;
y_direction=-y_direction;
}
}
}
}//少一个}
你看看吧,就剩下一个错误了,我在程序里面注释了你看看吧,可能是参数不对。
import java.applet.*;
import java.util.*;
import java.io.*;
public class QixTest extends Applet implements Runnable{
Thread imageThread=null;
Random rnd=new Random();
Rectangle bounceRect;
Rectangle colorBounce;
BouncyPoint endPoint;//加这1个变量
BouncyPoint endPoint1,endPoint2;
BouncyPoint R_Bouncer,G_Bouncer,B_Bouncer;
int x1,x2,y1,y2;
public void init(){
bounceRect=getBounds();
bounceRect.x=0;
bounceRect.y=0;
}
public void start(){
sowSeed();
if(imageThread==null){
imageThread=new Thread(this);
imageThread.start();
}
}
public void stop()
{
if((imageThread!=null)&&imageThread.isAlive())
{
imageThread.stop();
}
imageThread=null;
}
public void run()
{
int i=0;
Thread me=Thread.currentThread();
//me.sePriority(Thread.MIN_PRIORITY);
while(imageThread==me)
{
repaint();
try{
Thread.sleep(10);
}catch(InterruptedException e){}//Expection 写错了
i++;
if(i==1000){
sowSeed();
i=0;
}
}
}
public void update(Graphics g){
endPoint1.carryOnBouncing();
endPoint2.carryOnBouncing();
R_Bouncer.carryOnBouncing();
G_Bouncer.carryOnBouncing();
B_Bouncer.carryOnBouncing();
g.setColor(new Color(R_Bouncer.x,G_Bouncer.x,B_Bouncer.x));
g.drawLine(endPoint.x,endPoint.y,endPoint2.x,endPoint2.y);//
}
public void sowSeed(){
x1=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
x2=4+((rnd.nextInt()&1023)%(bounceRect.width-8));
y1=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
y2=4+((rnd.nextInt()&1023)%(bounceRect.height-8));
endPoint1=new BouncyPoint(bounceRect,x1,y1,-1.0,1,5);//还剩下一个错误了,你自己差一下这,好象是你的参数不对。
colorBounce=new Rectangle(0,0,255,255);
R_Bouncer=new BouncyPoint(colorBounce,200,0,-1.0,0.0);
B_Bouncer=new BouncyPoint(colorBounce,200,0,-2.0,0.0);
}
class BouncyPoint extends Point{
Rectangle boundingBox;
double x_direction;
double y_direction;
BouncyPoint(Rectangle limits,int startx,int starty,double dx,double dy)
{
super(startx,starty);
boundingBox=limits;
x_direction=dx;
y_direction=dy;
}
public void carryOnBouncing()
{
x+=(int)x_direction;
if(!boundingBox.contains(x,y)){
y-=(int)y_direction;
y_direction=-y_direction;
}
}
}
}//少一个}
你看看吧,就剩下一个错误了,我在程序里面注释了你看看吧,可能是参数不对。
BouncyPoint(Rectangle limits,int startx,int starty,double dx,double dy)
你看看,你在程序里定义的构造器和你用的BouncyPoint(x1,y1,-1.0,1,5);
参数类型也不对啊,
问题终于找到了,累死我了。这下知道怎么做了吧。
如果你一定要用BouncyPoint(x1,y1,-1.0,1,5);这样的构造器初始化你的类,那就要在BouncyPoint类里面自己在加一个如你BouncyPoint(x1,y1,-1.0,1,5);这样形式的构造器
请大家再给几种解决方案﹐让我学习学习。我刚学java不久﹐现在学到了applet﹐可是总是有一些问题自己还不能解决。哪位能不能把自己觉得经典的applet程序发出来让我学习一下﹐能有注释就更好了。谢谢﹗
我在jcreator下运行过这个applet,成功了﹐可是上面说的那个却不行。为什幺啊﹖
我在jcreator下运行过这个applet,成功了﹐可是上面说的那个却不行。为什幺啊﹖
我在jcreator下运行过这个applet,成功了﹐可是上面说的那个却不行。为什幺啊﹖
著個卻可以啊
import java.applet.Applet;
import java.awt.Graphics;
public class Simple extends Applet
{
StringBuffer buffer=new StringBuffer();
public void init()
{
addItem("12345");
}
public void start()
{
addItem("67890");
}
public void stop()
{
addItem("Stopping...");
}
public void destory()
{
addItem("Preparing for unloading...");
}
void addItem(String newWord)
{
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
public void paint(Graphics gVar)
{
gVar.drawRect(0,0,getSize().width-1,getSize().height-1);
gVar.drawString(buffer.toString(),5,15);
}
}
結果 1234567890
著個卻可以啊
import java.applet.Applet;
import java.awt.Graphics;
public class Simple extends Applet
{
StringBuffer buffer=new StringBuffer();
public void init()
{
addItem("12345");
}
public void start()
{
addItem("67890");
}
public void stop()
{
addItem("Stopping...");
}
public void destory()
{
addItem("Preparing for unloading...");
}
void addItem(String newWord)
{
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
public void paint(Graphics gVar)
{
gVar.drawRect(0,0,getSize().width-1,getSize().height-1);
gVar.drawString(buffer.toString(),5,15);
}
}
結果 1234567890