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

    Default where to add thread in the program

    in the above program, the main motive of the program is to keep one image fixed at the bottom (the fixed one is red) and start a thread for the image(the blue one) which u can see the code in the comment lines of the program given below .i just want to keep the fixed image and the other image(blue) in thread so that the two images should be visible to me , my program should look like one image(red) is fixed at the bottom and other image (blue) coming from top to bottom and both should be visible to me but they are not if one is fixed then i can see only that image i cant see the other image could u help out with this code and how to use thread in this program.


    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    class load extends JFrame
    {

    Image r;
    Image b;

    int y=329;
    int floor=329;
    int x=3;
    load()
    {

    super("some name");
    r=new ImageIcon("red.png").getImage();
    b=new ImageIcon("blue.png").getImage();

    getContentPane().add(new display());
    setVisible(true);
    setSize(175,400);
    setResizable(false);
    }

    class display extends JPanel //implements Runnable
    {
    //Thread t=new Thread(this);
    display()
    {
    repaint();
    }

    public void paintComponent(Graphics q)
    {
    Graphics2D n=(Graphics2D)q;
    q.drawImage(r,x,y,this);
    }
    /*
    i just want to place the red image fixed at the bottom
    and start new blue image thread i,e
    try
    {
    y=0;
    while(y!=floor)
    {
    y++;
    q.drawImage(b,x,y,this);
    Thread.sleep(10);
    }
    }
    catch(Exception e)
    {
    }
    */

    }
    }
    class f
    {
    public static void main(String args[])
    {
    new load();
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Suggestions:
    Use CODE tags around code that you post to make for easier reading.
    Use words for variable names instead of single letters
    Naming conventions for Java classes is Uppercase first letter.

    Re your logic - Following probably wrong because it is hard to read your code without the indentation showing nesting of {}.
    Move the loop out of the paintComponent() method. You should only stay in this method long enough to do some drawing and then exit. Use repaint() to cause the system to call the paintComponent() method. Put the repaint() call following where you compute what/where to draw, probably in a loop inside a thread .

    There are several examples of how to use Threads. Use a Search to find some and then put some code in your progam and post it using CODE tags.

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

    Default

    Didn't we just do this.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class SlidingDownRevisited extends JPanel implements Runnable {
        Image[] images;
        int yLoc;
    
        public SlidingDownRevisited() {
            String prefix = "images/geek/geek";
            String[] ids = { "-c---", "--g--" };
            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();
            }
            yLoc = -images[1].getHeight(this);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - images[0].getWidth(this))/2;
            int y = getHeight() - images[0].getHeight(this);
            g.drawImage(images[0], x, y, this);
            g.drawImage(images[1], x, yLoc, this);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(300,400);
        }
    
        public void run() {
            int height = getPreferredSize().height -
                         images[0].getHeight(this);
            do {
                yLoc++;
                repaint();
                try {
                    Thread.sleep(100);
                } catch(InterruptedException e) {
                    break;
                }
            } while(yLoc + images[1].getHeight(this) < height);
            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) {
            SlidingDownRevisited test = new SlidingDownRevisited();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •