Results 1 to 4 of 4

Thread: adding thread

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

    Default adding thread

    i sincerely apologize for asking this question again and i am stuck by this program for past 3 days this time i am sending the .gif file so that u can easily understand what my idea is
    here is the code
    as u see in the picture the red image is fixed and yellow is sliding from top to bottom i can't make it out when i run the only one image is visible either red is fixed visible or yellow sliding from top is visible i just want two image as shown in picture
    Java Code:
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    class load extends JFrame
    {
    
    Image r;
    Image ye;
    
    int y=329;
    int floor=329;
    int x=3;
    load()
    {
    
    super("some name");
    r=new ImageIcon("red.png").getImage();
    ye=new ImageIcon("yellow.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 yellow 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();
    }
    }
    Attached Thumbnails Attached Thumbnails adding thread-.gif  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Please repost the code for this question using the CODE tags to preserve indentation - to make it easier to read your code.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please at least delete the text in your other posts.

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

    Default

    Java Code:
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    
    class LoadRx extends JFrame
    {
        Image red;
        Image yellow;
    
        int y=329;
        int floor=329;
        int x=3;
        int yLoc = 0;
    
        LoadRx()
        {
            super("some name");
            red=new ImageIcon("red.png").getImage();
                              //"images/geek/geek--g--.gif").getImage();
            yellow=new ImageIcon("yellow.png").getImage();
                              //"images/geek/geek---h-.gif").getImage();
    
            getContentPane().add(new DisplayPanel());
    //        setVisible(true);
            setSize(175,400);
    //        setResizable(false);
            // This should always be the last call.
            setVisible(true);
        }
    
        class DisplayPanel extends JPanel implements Runnable
        {
            DisplayPanel()
            {
                Thread t=new Thread(this);
                // To keep your gui responsive to user input.
                t.setPriority(Thread.NORM_PRIORITY);
                t.start();
            }
    
            // Java wil be easier to learn if you will copy
            // these method signatures exactly as shown in
            // the javadocs. There is still plenty of room
            // to do things your way, eg, giving meaningful,
            // descriptive, verbose names to your variables.
            protected void paintComponent(Graphics g)
            {
                // Eliminate artifacts:
                super.paintComponent(g);
                // This cast not necessary here, but okay.
                //Graphics2D g2=(Graphics2D)g;
                // Draw fixed-location image:
                g.drawImage(red, x, y, this);
                // Draw moving image:
                // "yLoc" is controlled from within your
                // event code, here, the [i]run[/i] method
                // of your Runnable.
                g.drawImage(yellow, x, yLoc, this);
            }
    
            // i just want to place the red image fixed at the bottom
            // and start new yellow image thread i,e
            // This is the implementation of the Runnable interface.
            // In java, when you implement an interface you must
            // provide an implementation of every method defined
            // in the interface. Runnable has a single method: [i]run{/i].
            public void run()
            {
                // This resets your member variable "y" to zero.
                // You want it to be left alone so it will show
                // "red" at the bottom of the component. You could
                // reset the member variable "yLoc" here if you are
                // going to do multiple runs through this method.
    //            y=0;
                while(yLoc != floor)
                {
                    // The variable "y" was/is used to position "red".
                    // If you change it here you will reposition "red".
    //                y++;
                    // Solution: use a different variable to locate "yellow".
                    yLoc++;
                    // Show the new location of "yellow".
                    repaint();
                    try
                    {
                        // Draw only in [i]paintComponent[/i].
                        // Adjust variables here in event code.
    //                    q.drawImage(b,x,y,this);
                        Thread.sleep(50);
                    }
                    catch(InterruptedException e)
                    {
                        break;  // get out of this [i]while[/i] loop
                    }
                }
            }
        }
    
        public static void main(String[] args)
        {
            new LoadRx();
        }
    }

Similar Threads

  1. adding thread
    By amith in forum Java 2D
    Replies: 0
    Last Post: 07-05-2008, 04:44 PM
  2. adding thread
    By amith in forum Java 2D
    Replies: 0
    Last Post: 07-05-2008, 04:33 PM
  3. adding thread
    By amith in forum Java 2D
    Replies: 0
    Last Post: 07-05-2008, 04:31 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

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