Step 7 : ちらつきの制御
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Font;
import java.awt.Color;
import java.awt.Event;
import java.awt.FontMetrics;
import java.awt.Image;
public class HelloWorld7 extends Applet implements Runnable{
String input_text;
int xpos=400;
int realLength;
int realHeight;
int width=400;
int height=40;
boolean suspended = false;
Font font= new Font("Helvetica",1,24);
Color color = new Color(0,0,255);
Thread killme=null;
Image im;
Graphics osGraphics;
public void init(){
input_text=getParameter("text");
im=createImage(size().width,size().height);
osGraphics=im.getGraphics();
}
public void paint(Graphics g){
paintText(osGraphics);
g.drawImage(im,0,0,null);
}
public void paintText(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,width,height);
g.clipRect(0,0,width,height);
g.setFont(font);
g.setColor(color);
FontMetrics fmetrics=g.getFontMetrics();
realLength=fmetrics.stringWidth(input_text);
realHeight=fmetrics.getHeight();
g.drawString(input_text,xpos,(height+realHeight)/2);
}
public void start(){
if(killme==null){
killme=new Thread(this);
killme.start();
}
}
public void setcoord(){
xpos =xpos-5;
if(xpos<-realLength){
xpos=width;
}
}
public void run(){
while(killme != null){
try {Thread.sleep(10);}catch(InterruptedException e){}
setcoord();
repaint();
}
}
public void update(Graphics g){
paint(g);
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.MOUSE_DOWN) {
if (suspended) {
killme.resume();
} else {
killme.suspend();
}
suspended = !suspended;
}
return true;
}
public void stop(){
if(killme != null)
killme.stop();
killme=null;
}
}