Results 1 to 5 of 5
Thread: Using ImageIcon
- 03-20-2012, 09:05 PM #1
Member
- Join Date
- Mar 2012
- Location
- London
- Posts
- 7
- Rep Power
- 0
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 :)
This code does compile and produces the default style JCheckBox with spacing as if it has the defined image texture.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
- 03-21-2012, 12:14 AM #2
Re: Using ImageIcon
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-21-2012, 12:14 AM #3
Re: Using ImageIcon
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-21-2012, 06:30 PM #4
Member
- Join Date
- Mar 2012
- Location
- London
- Posts
- 7
- Rep Power
- 0
- 03-21-2012, 08:14 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
ImageIcon problem
By rvd1ofakind in forum New To JavaReplies: 5Last Post: 02-24-2012, 06:37 PM -
How to add an ImageIcon on a JPanel???
By Angelo in forum AWT / SwingReplies: 22Last Post: 12-19-2011, 02:27 PM -
Need help with ImageIcon and int
By proceeded in forum New To JavaReplies: 9Last Post: 01-23-2011, 10:10 PM -
ImageIcon HELP!
By ben1989 in forum New To JavaReplies: 1Last Post: 05-05-2010, 08:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks