Results 1 to 2 of 2

Thread: icon

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

    Default icon

    JFrame q=new JFrame( " Game");
    ImageIcon c=new ImageIcon("red.png");
    ImageIcon d=new ImageIcon("blue.png");
    JLabel l= new JLabel(c);
    JLabel j=new JLabel(d);
    l.setBounds(50,50,90,50);
    j.setBounds(100,50,90,50);
    q.getContentPane().add(l);
    q.getContentPane().add(j);
    q.setVisible(true);
    i just place in main it is executing correctly but i just want to get "red.png" (x,y,width,height) does there is any method AND also i just want to generate these icons in random order but how there are named as c,d how can i generate them

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.swing.*;
    
    public class RandomImagesTest2 {
        BufferedImage[] images;
        Random seed = new Random();
        JLabel left;
        JLabel right;
    
        RandomImagesTest2() {
            makeImages();
            new Thread(runner).start();
        }
    
        private Runnable runner = new Runnable() {
            long delay = 1000;
            boolean animate = true;
            boolean firstTime = true;
    
            public void run() {
                while(animate) {
                    try {
                        Thread.sleep(delay);
                    } catch(InterruptedException e) {
                        animate = false;
                    }
                    left.setIcon(getIcon());
                    right.setIcon(getIcon());
                    if(firstTime && left.isVisible()) {
                        Rectangle rL = left.getBounds();
                        Rectangle rR = right.getBounds();
                        System.out.printf("leftBounds = [%d, %d, %d, %d]  " +
                                          "rightBounds = [%d, %d, %d, %d]%n",
                                           rL.x, rL.y, rL.width, rL.height,
                                           rR.x, rR.y, rR.width, rR.height);
                        firstTime = false;
                    }
                    left.repaint();
                    right.repaint();
                }
            }
        };
    
        private void makeImages() {
            int alpha = 140;
            Color[] colors = {
                new Color(255, 0, 0, alpha), new Color(0, 0, 255, alpha)
            };
            int w = 90, h = 50;
            int type = BufferedImage.TYPE_INT_ARGB;
            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();
            }
        }
    
        private JPanel getContent() {
            left  = new JLabel(getIcon(), JLabel.CENTER);
            right = new JLabel(getIcon(), JLabel.CENTER);
            JPanel panel = new JPanel(null);
            panel.add(left);
            panel.add(right);
            left.setBounds(50,50,90,50);
            right.setBounds(100,50,90,50);
            return panel;
        }
    
        private ImageIcon getIcon() {
            int n = seed.nextInt(images.length);
            return new ImageIcon(images[n]);
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new RandomImagesTest2().getContent());
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM
  2. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 07:01 AM
  3. How to set an Icon in a Label?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:38 PM
  4. how to remove an image icon
    By cecily in forum Advanced Java
    Replies: 1
    Last Post: 08-05-2007, 04:25 AM
  5. To add an icon to my project
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 03:14 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
  •