java

[알림판목록 I] [알림판목록 II] [글목록][이 전][다 음]
[ java ] in KIDS
글 쓴 이(By): jimsh (바람향기)
날 짜 (Date): 1999년 8월 23일 월요일 오후 08시 11분 11초
제 목(Title): 플리커링 좀 없애 주세요.



21일만에 자바 끝내기 책으로 공부하다가 내용데로 되지 않아서

여기에 묻습니다.

플리커링이 책 내용데로 없어지지 않습니다.

더블 버퍼링을 이용해도 되지 않는데, 무엇이 문제인지 좀 봐주세요.

미리 감사드립니다.

프로그램은 간단합니다.

글자 색만 바꾸는 애플릿입니다. 

/////////////////////////////////////////////
// Test.java 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Test extends Applet implements Runnable{

Thread runner;
Font f = new Font("TimesRoman", Font.BOLD, 48);
Color colors[] = new Color[200];

public Test(){ super(); }

public void init(){
float c=0;
for (int i=0; i < colors.length; i++){
colors[i]=Color.getHSBColor(c,(float)1.0,(float)1.0);
c +=0.005;
}
}

public void start(){
if(runner == null){
runner = new Thread(this);
runner.start();
}
}

public void run(){
int i=0;
while(true){
setForeground(colors[i]);
repaint();
i++;
try{ Thread.sleep(50); }
catch(InterruptedException e) {  }
if (i == (colors.length)) i=0;
}
}

public void paint(Graphics g){
g.setFont(f);
g.drawString("All the Test Colors", 15, 50);
}

public void update(Graphics g){
paint(g);
}

public static void main(String args[]){
Frame frame = new Frame("Jimsh");
Test myApplet = new Test();
myApplet.init();
myApplet.start();
frame.add(myApplet);
frame.setSize(450,100);
frame.setVisible(true);
}
}
///////////////////////////////////////////소스 Test.java끝

위의 것은 update 메쏘드만 오버라이드한 것입니다.

다음은 더블 버퍼링을 이용한 예제 입니다.

/////////////////////////////////////////////////
// Test1.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Test1 extends Applet implements Runnable{

Thread runner;
Image mImage;
Graphics offG;
Font f = new Font("TimesRoman", Font.BOLD, 48);
Color colors[] = new Color[50];

public Test1(){ super(); }

public void init(){
float c=0;
for (int i=0; i < colors.length; i++){
colors[i]=Color.getHSBColor(c,(float)1.0,(float)1.0);
c +=0.05;
}
}

public void start(){
if(runner == null){
runner = new Thread(this);
runner.start();
}
}

public void run(){
int i=0;
while(true){
setForeground(colors[i]);
repaint();
i++;
try{ Thread.sleep(50); }
catch(InterruptedException e) {  }
if (i == (colors.length)) i=0;
}
}

public void paint(Graphics g){
Dimension d = getSize();
checkOffscreenImage();
offG = mImage.getGraphics();
offG.setFont(f);
offG.drawString("All the Swirly Colors", 15, 50);

g.drawImage(mImage,0,0,this);
}

public void update(Graphics g){
paint(g);
}

private void checkOffscreenImage() {
    Dimension d = getSize();
    if (mImage == null ||
        mImage.getWidth(null) != d.width ||
        mImage.getHeight(null) != d.height) {
      mImage = createImage(d.width, d.height);
    }
  }

public static void main(String args[]){
Frame frame = new Frame("Jimsh");
Test1 myApplet = new Test1();
myApplet.init();
myApplet.start();
frame.add(myApplet);
frame.setSize(450,100);
frame.setVisible(true);
}
}
///////////////////////////////////Test1.java 쏘스 끝

걍 카피엔 페이스트하면 됩니다.

간단한 예제이니 좀 봐주시면 고맙겠습니다.


[알림판목록 I] [알림판목록 II] [글 목록][이 전][다 음]
키 즈 는 열 린 사 람 들 의 모 임 입 니 다.