Results 1 to 13 of 13

Thread: program problem

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

    Default program problem

    i just want to load two images in the frame but iam getting only one image at a time can u give me logic for writing multiple images in frame at same time and also give the logic for generating random images for this multiple images
    import java.awt.*;
    import javax.swing.*;
    class a extends JComponent implements Runnable
    {
    Image q[]=new Image[2];
    int f=0;
    int x=200,y=200;

    int c;
    public void paint(Graphics g)
    {
    Image r=q[f];
    g.drawImage(r,x,y,this);
    }

    public void run()
    {
    q[0]=new ImageIcon("red.png").getImage();
    q[1]=new ImageIcon("blue.png").getImage();

    try
    {
    while(true)
    {
    f=(f+1)%q.length;

    repaint();

    Thread.sleep(1000);
    }}
    catch(InterruptedException m)
    {
    }
    }
    }

    class b
    {
    public static void main(String args[])
    {
    a x=new a();
    JFrame s= new JFrame(" GAME ");
    s.getContentPane().add(x);
    s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    s.setVisible(true);
    s.setSize(800,800);
    s.setResizable(false);
    (new Thread(x)).start();
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

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

    Default random

    generating both the images randomly i a frame example i hav red and blue
    randomly generate possible combinations in frame red red ,red blue, blue red, blue blue

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    According to your application do it as follows.

    Java Code:
        public void run() {
            q[0] = new ImageIcon("images\\logo.gif").getImage();
            q[1] = new ImageIcon("images\\RADAR.png").getImage();
    
            try {
                while(true) {
                    //f = ( f + 1 ) % q.length;
                    f = randomIndex();
                    repaint();
    
                    Thread.sleep(1000);
                }
            }
            catch(InterruptedException m) {
            
            }
        }
        
        public int randomIndex() {
            Random rr = new Random();
            return rr.nextInt(2); // number of images you used
        }

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

    Default paint

    in paint method when i give repaint then it draws only one image i,e q[0]
    after f ==1 then it only draws q[1] i just want to draw both q[0] and q[1] at the same time can u give me the logic i will make x=x+200 for q[1] so that the images will be side by side

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by amith View Post
    in paint method when i give repaint then it draws only one image i,e q[0]
    after f ==1 then it only draws q[1] i just want to draw both q[0] and q[1] at the same time can u give me the logic i will make x=x+200 for q[1] so that the images will be side by side
    What you really want to do? Display two images at the same time? So what you say at your post #2? Randomly change???

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

    Default program

    see if there is a mistake in telling i apolozise but iam telling u frankly my idea is all about
    i jst want to display both the different images at the same time but when iam trying to do that it is displaying any one of the images i just want to display both the images at same time and i also want to generate two random images at same time for i will just give u the picture
    Attached Thumbnails Attached Thumbnails program problem-.png  

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, you can try something different like this. Use two labels in two locations you want. Then add images there randomly selected. I don't think it is difficult.

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.swing.*;
    
    public class RandomImagesTest extends JPanel {
        BufferedImage[] images;
        Random seed = new Random();
        int leftIndex  = 0;
        int rightIndex = 1;
    
        RandomImagesTest() {
            makeImages();
            new Thread(runner).start();
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x1 = 50;
            int y1 = 50;
            g.drawImage(images[leftIndex], x1, y1, this);
            int x2 = x1 + images[leftIndex].getWidth() + 20;
            int y2 = y1;
            g.drawImage(images[rightIndex], x2, y2, this);
        }
    
        private Runnable runner = new Runnable() {
            long delay = 1000;
            boolean animate = true;
    
            public void run() {
                while(animate) {
                    try {
                        Thread.sleep(delay);
                    } catch(InterruptedException e) {
                        animate = false;
                    }
                    int n = images.length;
                    leftIndex  = seed.nextInt(n);
                    rightIndex = seed.nextInt(n);
                    //System.out.printf("leftIndex = %d  rightIndex = %d%n",
                    //                   leftIndex, rightIndex);
                    repaint();
                }
            }
        };
    
        private void makeImages() {
            Color[] colors = { Color.red, Color.blue };
            int w = 40, h = 40;
            int type = BufferedImage.TYPE_INT_RGB;
            images = new BufferedImage[colors.length];
            for(int i = 0; i < images.length; i++) {
                images[i] = new BufferedImage(w, h, type);
                Graphics2D g2 = images[i].createGraphics();
                g2.setBackground(colors[i]);
                g2.clearRect(0,0,w,h);
                g2.dispose();
            }
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new RandomImagesTest());
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

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

    Default assigning

    how can i assign image object to g.drawimage for example
    Image q=new Image;
    g.drawImage(q,x,y,this);
    Image m=new Image();
    m=g.drawImage(q,x,y,this);

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

    Default

    Java Code:
        protected void paintComponent(...
            ...
            Image q=images[seed.nextInt(images.length)];
            g.drawImage(q,x,y,this);
            Image m=images[seed.nextInt(images.length)];
            g.drawImage(m,x,y,this);
        }
    The main problem with doing it this way is that the images may change with every system-initiated repaint event. It is better to assign the images in your event code, viz, in your Runnable implementation, as shown above.

Similar Threads

  1. Problem with my first Struts program....please help me
    By sireesha in forum Web Frameworks
    Replies: 5
    Last Post: 10-16-2011, 05:19 PM
  2. Problem with First Hibernate program
    By sireesha in forum JDBC
    Replies: 11
    Last Post: 05-05-2008, 05:19 PM
  3. Two problem for my rssi calculation program
    By iamchoilan in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2008, 04:49 PM
  4. Problem with my program HelloWorld
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:32 PM
  5. getting problem in compiling java program?
    By sathish04021984 in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:26 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
  •