下面是我写的一个程序 为什么他画不出来啊, 帮我看一下 import java.applet.*;
import java.awt.*;
public class T1 extends Applet implements Runnable{
Thread t1,t2;
public void init(){
setBackground(Color.black);
}
public void start(){
if(t1==null)
t1=new Thread(this);
if(t2==null)
t2=new Thread(this);
t1.start();
t2.start();
}
public void paint(Graphics g){
if(Thread.currentThread()==t1){
g.setColor(Color.red);
g.drawRect(50,50,40,60);
}
else if(Thread.currentThread()==t2){
g.setColor(Color.green);
g.drawOval(100,100,50,50);
}
}
public void run(){
while(true){
repaint();
try{
Thread.sleep(100);
}
catch(InterruptedException e){
}
}
}
}
我把他改成下面的就可以了 为什么啊, 是不是那个线程的问题啊, 帮我解答一下啊, 谢谢了import java.applet.*;
import java.awt.*;
public class T2 extends Applet implements Runnable{
Thread t1,t2;
Graphics pen;
public void init(){
setBackground(Color.black);
}
public void start(){
pen=getGraphics();
if(t1==null)
t1=new Thread(this);
if(t2==null)
t2=new Thread(this);
t1.start();
t2.start();
}
public void run(){
while(true){
if(Thread.currentThread()==t1){
pen.setColor(Color.red);
pen.drawRect(100,100,40,60);
try{
Thread.sleep(500);
}
catch(InterruptedException e){
}
}
else if(Thread.currentThread()==t2){
pen.setColor(Color.green);
pen.drawOval(60,80,50,50);
}
try{
Thread.sleep(100);
}
catch(InterruptedException e){
}
repaint();
}
}
}
import java.awt.*;
public class T1 extends Applet implements Runnable{
Thread t1,t2;
public void init(){
setBackground(Color.black);
}
public void start(){
if(t1==null)
t1=new Thread(this);
if(t2==null)
t2=new Thread(this);
t1.start();
t2.start();
}
public void paint(Graphics g){
if(Thread.currentThread()==t1){
g.setColor(Color.red);
g.drawRect(50,50,40,60);
}
else if(Thread.currentThread()==t2){
g.setColor(Color.green);
g.drawOval(100,100,50,50);
}
}
public void run(){
while(true){
repaint();
try{
Thread.sleep(100);
}
catch(InterruptedException e){
}
}
}
}
我把他改成下面的就可以了 为什么啊, 是不是那个线程的问题啊, 帮我解答一下啊, 谢谢了import java.applet.*;
import java.awt.*;
public class T2 extends Applet implements Runnable{
Thread t1,t2;
Graphics pen;
public void init(){
setBackground(Color.black);
}
public void start(){
pen=getGraphics();
if(t1==null)
t1=new Thread(this);
if(t2==null)
t2=new Thread(this);
t1.start();
t2.start();
}
public void run(){
while(true){
if(Thread.currentThread()==t1){
pen.setColor(Color.red);
pen.drawRect(100,100,40,60);
try{
Thread.sleep(500);
}
catch(InterruptedException e){
}
}
else if(Thread.currentThread()==t2){
pen.setColor(Color.green);
pen.drawOval(60,80,50,50);
}
try{
Thread.sleep(100);
}
catch(InterruptedException e){
}
repaint();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货