Results 1 to 5 of 5

Thread: Using ImageIcon

  1. #1
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Using ImageIcon

    Hi all,

    I'm in the process of making a 2D game with a JCheckBox grid. I'm having trouble setting icons for each individual checkbox (for both selected and un-selected states) from a master image but I'm not sure why. Would anyone be able to tell me where I'm going wrong? This code does compile and produces the default style JCheckBox with spacing as if it had the defined image texture (in my code below, any references to target [x] [y] are extensions of the JCheckBox class).

    Many thanks :)

    Java Code:
    package targetgame;
    
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.LinkedList;
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    public class TargetFrame extends JPanel {
    
        private TitledBorder border;
        private Target [] [] target; // class Target extends JCheckBox
        private static LinkedList<Target> targetList;
        private static ImageIcon [] targetImage = new ImageIcon [400];
        
        private static int $targets = TargetGameFrame.targets;
        public static int targetsLeft = $targets;
        
        public TargetFrame(int numTargets) {
            
            super(new GridLayout(20, 20, 2, 2));
            border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Bermuda");
            border.setTitleJustification(TitledBorder.DEFAULT_JUSTIFICATION);
            setBorder(border);
            setVisible(true);
            
            // GET IMAGES FOR TARGETS
    
            try {
    
                File file = new File("C:\\Documents and Settings\\David\\My Documents\\NetBeansProjects\\TargetGame\\map.png");
                FileInputStream inputStream = new FileInputStream(file);
                BufferedImage rawImage = ImageIO.read(inputStream);
    
                int chunkWidth = rawImage.getWidth() / 20; 
                int chunkHeight = rawImage.getHeight() / 20;
                int count = 0;
                BufferedImage [] buffImage = new BufferedImage [400];
                Image [] image = new Image [400];
    
                for (int x = 0; x < 20; x++) {
                    for (int y = 0; y < 20; y++) {
    
                        buffImage[count] = new BufferedImage(chunkWidth, chunkHeight, rawImage.getType());
                        image [count] = (Image) buffImage[count];
                        targetImage[count] = new ImageIcon(image[count]);
    
                    }
                }   
    
            } catch (IOException ex) {
    
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
    
            }
            
            // MAKE TARGETS
            
            int [] targetIndices = new int [numTargets];
            targetIndices = ranIntArray(numTargets);
            
            target = new Target [20] [20];
            targetList = new LinkedList<>();
            int i = 0;
    
            for (int a = 0; a < 20; a++) {
                for (int b = 0; b < 20; b++) {
                    
                    
                    target [a] [b] = new Target(false); // create new targets
                    target [a] [b].setIcon(targetImage[i]); // set image texture icon
                    targetList.add(target [a] [b]);
                    i++;
                    
                }
            }
            
            for (int c = 0; c < targetIndices.length; c++) {
                
                int index = targetIndices[c];
                Target newActiveTarget = targetList.get(index); // set selected targets
                newActiveTarget.setTarget();
                targetList.add(index, newActiveTarget);
                
            }
            
            for (int d = 0; d < targetList.size(); d++) {
                
                Target targetToAdd = targetList.get(d); // add all targets to list and the grid
                add(targetToAdd);
                
            }
            
           
        } // end of TargetFrame constructor
    This code does compile and produces the default style JCheckBox with spacing as if it has the defined image texture.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Using ImageIcon

    Quote Originally Posted by David1234204 View Post
    I'm having trouble setting icons for each individual checkbox (for both selected and un-selected states) from a master image but I'm not sure why.
    Without knowing what customizations your extension of JCheckBBox does, who knows? But I do see you constructing ImageIcons from blank, newly constructed BufferedImages that haven't had anything drawn to them.

    Also note that there's no need to cast from BufferedImage to Image. BufferedImage extends Image, so an Image reference can always point to a BufferedImage instance; and a BufferedImage reference can be passed to a method or constructor that expects an argument of type Image.

    Moving to AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Using ImageIcon

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    David1234204 is offline Member
    Join Date
    Mar 2012
    Location
    London
    Posts
    7
    Rep Power
    0

    Default Re: Using ImageIcon

    Quote Originally Posted by DarrylBurke View Post
    But I do see you constructing ImageIcons from blank, newly constructed BufferedImages that haven't had anything drawn to them.

    Also note that there's no need to cast from BufferedImage to Image. BufferedImage extends Image, so an Image reference can always point to a BufferedImage instance; and a BufferedImage reference can be passed to a method or constructor that expects an argument of type Image.
    Thanks a lot, I didn't realise that the BufferedImages didn't have anything drawn to them - I had taken a section of code from a website and removed it thinking it wasn't necessary.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Using ImageIcon

    Trying to learn by copying blocks of code is an inefficient process, to say the least. If you use publicly available code for inspiration, you need to first analyze and understand the code, then write your own version of it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ImageIcon problem
    By rvd1ofakind in forum New To Java
    Replies: 5
    Last Post: 02-24-2012, 06:37 PM
  2. How to add an ImageIcon on a JPanel???
    By Angelo in forum AWT / Swing
    Replies: 22
    Last Post: 12-19-2011, 02:27 PM
  3. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 10:10 PM
  4. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 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
  •