Results 1 to 3 of 3

Thread: repaint problem

  1. #1
    amith is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default repaint problem

    i hav five images iam loading it with repaint after that i just want one image to fall from top to bottom using threads when iam using this only the image falling from top to bottom is visible but the five images loaded previously are not visible only the new image falling from top to bottom is visible can u suggest some paint method to this thing

  2. #2
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    Add the bigger image which covers from top to bottom then add the rest 5 small images...
    To finish sooner, take your own time....
    Nivedithaaaa

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Now we can slide up and down.
    Java 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();
        }
    }

Similar Threads

  1. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  2. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  3. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM
  4. repaint validate doLayout
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-27-2007, 06:53 PM
  5. Repaint fails when using threads
    By rjevans2000 in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-21-2007, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •