Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2008, 06:32 PM
Member
 
Join Date: May 2008
Posts: 22
Rep Power: 0
amith is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-16-2008, 07:34 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change Window Icon sharafat AWT / Swing 6 02-10-2008 06:13 AM
Changing icon of JOptionPane mew New To Java 3 12-21-2007 07:01 AM
How to set an Icon in a Label? Soda New To Java 2 12-07-2007 12:38 PM
how to remove an image icon cecily Advanced Java 1 08-05-2007 04:25 AM
To add an icon to my project Albert AWT / Swing 1 07-13-2007 03:14 PM


All times are GMT +2. The time now is 02:39 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org