[ java ] in KIDS 글 쓴 이(By): char (★자유★) 날 짜 (Date): 1997년11월09일(일) 10시40분16초 ROK 제 목(Title): [프로젝트 2 소스] import java.applet. *; import java.awt. *; public class JReverseScrollText extends Applet implements Runnable { Canvas drawingarea; String txt[] = new String[100], count; Dimension dm; Thread animation = null; Image offscreen; int txt_width[] = new int[100], gap = 0, font_height, start; boolean stop = false, startb = false, first = false, cycle = false; public void init() { int j; first = true; resize(500, 500); dm = this.size(); // this.setBackground(Color.white); count = getParameter("NUMBER"); FontMetrics fm = this.getFontMetrics(this.getFont()); font_height = fm.getHeight(); if (count == null) { count = "1"; } for (j = 1; j <= Integer.valueOf(count).intValue(); j++) { txt[j] = getParameter("TXT" + j); if (txt[j] == null) { txt[j] = "This Applet is ReverseScrollText."; } txt_width[j] = fm.stringWidth(txt[j]); } } public void start() { animation = new Thread(this); animation.start(); } public void stop() { if (animation != null) animation.stop(); animation = null; } public boolean mouseDown(Event event, int x, int y) { if (animation != null) { stop = true; } else { stop = false; this.start(); } return true; } public void drawback(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, dm.width, dm.height); } public void paint(Graphics g) { int i; drawback(g); g.setColor(Color.black); for (i = 1; i <= Integer.valueOf(count).intValue(); i++) { if ((gap == start) && (startb == true)) { cycle = true; // gap 이 start와 같고, startb 가 true라면 // cycle이 1번 끝났다는 뜻이므로 cycle을 true로 변환 } else if (cycle == true) { if (i+1 > Integer.valueOf(count).intValue()) { ; } else { g.drawString(txt[i+1], (dm.width - txt_width[i+1]) / 2, font _height * (i+1) - gap); } g.drawString(txt[i], (dm.width - txt_width[i]) / 2, dm.height - gap + font_height * i); } else { g.drawString(txt[i], (dm.width - txt_width[i]) / 2, dm.height - gap + font_height * i); } } /* if ((i == Integer.valueOf(count).intValue()) && (cycle == true)) { cycle = false; }*/ } public void run() { offscreen = createImage(dm.width, dm.height); //double-buffering //을 위한 offscreenimage생성 while (!stop) { Rectangle oldrect = new Rectangle(0, 0, dm.width, dm.height); gap = ((gap + 2) % dm.height); if (first == true) { // first는 실행이 되었는지 알아보려는 start = gap; // boolean변수. start는 gap의 처음값을 first = false; // 기억하는 변수. } if (gap != start) { startb = true; } /* else { startb = false;} */ Rectangle newrect = new Rectangle(0, 0, dm.width, dm.height); Rectangle r = newrect.union (oldrect); Graphics g = offscreen.getGraphics(); g.clipRect(r.x, r.y, r.width, r.height); paint(g); g = this.getGraphics(); g.clipRect(r.x, r.y, r.width, r.height); g.drawImage(offscreen, 0, 0, this); try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println("Can't run this applet because of Thread."); System.exit(0); } } animation = null; } } ------------------------------------------------------------------------------ 이것은 이번 프로젝트(?) 소스입니다. 음... 약간의 버그가 있습니다. 화면의 최상위에 스크롤 될때 약간 껌벅일겁니다. (0,0) 좌표에서의 그리기를 생각하지 않았기 때문이죠. (좌표계산에 %연산자를 사용했기 때문에...) duble-buffring에서의 rectangle생성은 canvas전체를 대상으로 했습니다. 소스가 지저분하더라도 양해바랍니다. 확인해보려면 --------------------example.html----------------------------------- <applet code=JReverseScrollText.class width=500 height=500> <param name="number" value="4"> <param name="txt1" value="This is a test applet"> <param name="txt2" value="Welcome to My"> <param name="txt3" value="Welcome to My HomePage"> <param name="txt4" value="Welcome to My HomePage!!"> </applet> ------------------------------------------------------------------ 이렇게 하시면 됩니다. number는 string의 갯수, string의 이름들은 txt1, txt2, ..., txt100으로 지어 주면 됩니다. 버그를 발견하시면 포스팅이나 메일 부탁드립니다. 그럼 이만.. |