import java.awt.*;
import java.applet.*;
import java.util.*;
public class FontTest extends Applet implements Runnable{
Thread fontrun;
int count;
int locx;
int locy;
byte state;
final static int REFRESH_TIME = 100;
public void paint(Graphics g){
Font f = new Font("Serif", Font.PLAIN, 12);
g.setColor(Color.blue);
g.setFont(f);
g.drawString("Serif - PLAIN", locx, locy);
}
public void init(){
System.out.printlin("Draw words");
setBackground(Color.black);
locx = 0;
locy = 0;
state = DOWN;
}public void start(){
System.out.println("Starting !");
fontrun = new Thread(this);
if (fontrun = null)
stop();
}public void updateFont(){
switch(state){
case DOWN:
locx +=2;
locy += 2;
if(locx > 199)
state= UP;
break;
case UP:
locx -=2;
locy -=2;
if(locx < 1 )
state= DOWN;
break;
}
}public void run(){
while(true)
{
repaint();
updateFont();
try{
Thread.sleep(REFRESH_TIME);
}
catch(Exception e){
}
}
}public void stop(){
System.out.pritln("Program is terminated !");
}
}
//<applet code = FontTest width =199 height= 199></applet>
//无法访问Thread
import java.applet.*;
import java.util.*;
public class FontTest extends Applet implements Runnable{
Thread fontrun;
int count;
int locx;
int locy;
byte state;
final static int REFRESH_TIME = 100;
public void paint(Graphics g){
Font f = new Font("Serif", Font.PLAIN, 12);
g.setColor(Color.blue);
g.setFont(f);
g.drawString("Serif - PLAIN", locx, locy);
}
public void init(){
System.out.printlin("Draw words");
setBackground(Color.black);
locx = 0;
locy = 0;
state = DOWN;
}public void start(){
System.out.println("Starting !");
fontrun = new Thread(this);
if (fontrun = null)
stop();
}public void updateFont(){
switch(state){
case DOWN:
locx +=2;
locy += 2;
if(locx > 199)
state= UP;
break;
case UP:
locx -=2;
locy -=2;
if(locx < 1 )
state= DOWN;
break;
}
}public void run(){
while(true)
{
repaint();
updateFont();
try{
Thread.sleep(REFRESH_TIME);
}
catch(Exception e){
}
}
}public void stop(){
System.out.pritln("Program is terminated !");
}
}
//<applet code = FontTest width =199 height= 199></applet>
//无法访问Thread
import java.applet.*;
import java.util.*;public class TestFont extends Applet implements Runnable {
Thread fontrun; int count; int locx ; int locy ; int state; final static int REFRESH_TIME = 100; public void paint(Graphics g) {
Font f = new Font("Serif", Font.PLAIN, 12);
g.setColor(Color.RED);
g.setFont(f);
g.drawString("2", locx, locy);
} public void init() {
System.out.println("Draw words");
//setBackground(Color.black);
locx = 10;
locy = 20;
state = 1;
} public void start() {
System.out.println("Starting !");
try{
new Thread(this).start();
} catch(Exception ex ) {
System.out.println(ex);
}
} public void updateFont() {
System.out.println(state);
switch (state) {
case 1:
locx += 2;
locy += 2;
if (locx > 199)
state = 2;
break; case 2:
locx -= 2;
locy -= 2;
if (locx < 1)
state = 1;
break; } } public void run() {
while (true) {
repaint();
updateFont();
try {
Thread.sleep(REFRESH_TIME);
} catch (Exception e) {
System.out.println(e);
}
}
} public void stop() {
System.out.println("Program is terminated !");
}
}
帮你改了一下
有问题交流
import java.applet.*;
import java.util.*;
public class FontTest extends Applet implements Runnable{
Thread fontrun;
int count;
int locx;
int locy;
byte state;
final static int REFRESH_TIME = 100;
public void paint(Graphics g){
Font f = new Font("Serif", Font.PLAIN, 12);
g.setColor(Color.white);
g.setFont(f);
g.drawString("Serif - PLAIN", locx, locy);
}
public void init(){
System.out.println("Draw words");
setBackground(Color.black);
locx = 0;
locy = 0;
state = 1;
}public void start(){
System.out.println("开始 !");
fontrun = new Thread(this);
fontrun.start();
}public void updateFont(){
switch(state){
case 1:
locx +=2;
locy += 2;
if(locx > 199)
state= 2;
break;
case 2:
locx -=2;
locy -=2;
if(locx < 1 )
state= 1;
break;
}
}public void run(){
while(true)
{
repaint();
updateFont();
try{
Thread.sleep(REFRESH_TIME);
}
catch(Exception e){
}
}
}public void stop(){
System.out.println("Program is terminated !");
}
}
//<applet code = FontTest width =199 height= 199></applet>
我是不是jdk没有设置好?Thread的东西编译失败 可是可以appletviewer观看 ..不知为什么?
奇怪?
程序本身没有错误!