Thread: repaint problem
View Single Post
  #3 (permalink)  
Old 07-01-2008, 02:10 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Now we can slide up and down.
Code:
import java.awt.*; import javax.swing.*; public class SlidingDown extends JPanel implements Runnable { Image[] images; int[] yLocs; public SlidingDown() { String prefix = "images/geek/geek"; String[] ids = { "-c---", "--g--", "---h-", "----t" }; images = new Image[ids.length]; for(int i = 0; i < images.length; i++) { String path = prefix + ids[i] + ".gif"; images[i] = new ImageIcon(path).getImage(); } yLocs = new int[images.length]; for(int i = 0; i < yLocs.length; i++) { yLocs[i] = -images[i].getHeight(this); } } protected void paintComponent(Graphics g) { super.paintComponent(g); for(int i = 0; i < images.length; i++) { int x = (getWidth() - images[i].getWidth(this))/2; g.drawImage(images[i], x, yLocs[i], this); } } public Dimension getPreferredSize() { return new Dimension(300,400); } public void run() { int height = getPreferredSize().height; int index = 0; do { yLocs[index]++; repaint(); if(yLocs[index] > height) { index++; } try { Thread.sleep(100); } catch(InterruptedException e) { break; } } while(index < images.length); System.out.println("all done"); } private void start() { Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { SlidingDown test = new SlidingDown(); JFrame f = new JFrame(); f.add(test); f.pack(); f.setLocation(200,200); f.setVisible(true); test.start(); } }
Reply With Quote