Update all icons of a JLabel ArrayList efficiently? - java

So, I want to update every icon of a JLabel ArrayList, to its own individual icon based off of an outside ArrayList. The outside ArrayList is called board, and the JLabel ArrayList is called tiles. I want to update every icon to its corresponding item in board, how can I do that efficiently? I need it to update every icon of a huge list, we're talking like 100 - 200 items long. Right now, at a hundred items long, it takes about 4 seconds before displaying all of the icons at the same time:
for (int i = 0; i < board.size(); i++) {
//this is all one line:
tiles.get(i).setIcon(resizeIcon(displayTile(board.get(i),
tiles.get(i).getHeight(), tiles.get(i).getHeight()));
}
//this resizes the ImageIcon and returns the risized icon:
public static ImageIcon resizeIcon(ImageIcon i, int x, int y) {
Image image = i.getImage();
Image newimg = image.getScaledInstance(x, y, java.awt.Image.SCALE_SMOOTH);
i = new ImageIcon(newimg);
return i;
}
//this returns an ImageIcon based off of the number that is put in:
public static ImageIcon displayTile(int num) {
if (num != 0) {
ImageIcon i = new ImageIcon("src/resources/" + num + ".png");
return i;
}
ImageIcon i = new ImageIcon("src/resources/0.png");
return i;
}
How do I make it more effecient?

Related

How to make a copy of an ImageIcon JButton on a current JPanel each time it is clicked?

I am creating a Swing Game GUI, which contains multiple panels that are part of a frame. Each panel contains a number of ImageIcon JButtons. When a JButton is clicked it will randomly position itself on the screen. For this particular panel i am not using a certain LayoutManager(understand the criticism here, but also the frame is not resizable as it fits the entire screen) and I use the randomness of setBounds(x, y, 100, 100) for this image JButton to appear on a random spot inside that panel. My only trouble is to figure it out, how to save the previous position of a certain JButton, so if the user repeatedly clicks it, it will duplicate and will appear on the screen multiple times on a random position, leaving all others displayed.
class ButtonListener implements ActionListener
{
Random randomCoord = new Random();
//random location in on the screen 30-150 x 200-400
int x = randomCoord.nextInt(150-30) + 30;
int y = randomCoord.nextInt(400-200) + 200;
int butnCOUNT = 0;
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == button1)
{
butnCOUNT ++;
JButton[] addButtons = new JButton[butnCount];
for (int i = 0; i < addButtons.length; i++)
{
addButtons[i] = butnCloud; // ImageIcon JButton
addButtons[i].addActionListener(new ButtonListener ());
panel5.add(addButtons[i]);
panel5.setVisible(true);
panel5.setOpaque(false);
panel5.setBounds(x, y, 20, 20);
}
}
}

Splitting an Image Object into a 2D Array in Java

The aim of this little project is to break down an image (in this case a flag) into pieces like a jigsaw and store each piece in part of a 2D array. I then want to be able to view each piece individually so that I know it has worked.
I have created an object class which loads and stores the image. I created the object in my main class and then pass it to my splitImage method which divides the image into chunks and stores in the array. I would like to be able to view a section of the array to check that the splitImage method has worked correctly. Long term however I do need to view the array as I will be determining the colour of the pixel in each image piece and counting how many of each colour is in the image. When I run the current code I get the following in the console,
Exception in thread "main" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at sample.ImageFrame.splitImage(ImageFrame.java:28)
at sample.ImageFrame.main(ImageFrame.java:59)
28 is the line - BufferedImage image = (BufferedImage)((Image) icon.getImage());
59 is - ImageFrame.splitImage(imageSwing.label);
I have played around with these for some time, changing the position trying other options and have been unsuccessful. Help on this is much appreciated.
Code is below and thanks in advance.
public class ImageSwing extends JFrame
{
public JLabel label;
public ImageSwing()
{
super("Test Image Array");
setLayout(new FlowLayout());
Icon flag = new ImageIcon(getClass().getResource("Italy_flag.jpg"));
label = new JLabel(flag);
label.setToolTipText("Italian Flag");
add(label);
}//main
}//class
public class ImageFrame
{
//public ImageSwing imageSwing = new ImageSwing();
//ImageSwing imageSwing = new ImageSwing();
public static BufferedImage splitImage(JLabel i) throws IOException
{
//Holds the dimensions of image
int rows = 4;
int cols = 4;
ImageIcon icon = (ImageIcon)i.getIcon();
BufferedImage image = (BufferedImage)((Image) icon.getImage());
int chunks = rows * cols; //Total amount of image pieces
int partWidth = i.getWidth() / cols; // determines the piece width
int partHeight = i.getHeight() / rows; //determines the piece height
int count = 0;
BufferedImage[][] flagArray = new BufferedImage[rows][cols]; //2D Array to hold each image piece
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
//Initialize the image array with image chunks
flagArray[x][y] = new BufferedImage(partWidth, partHeight, image.getType());
// draws the image chunk
Graphics2D gr = flagArray[x][y].createGraphics();
gr.drawImage(image, 0, 0, partWidth, partHeight, partWidth * y, partHeight * x, partWidth * y + partWidth, partHeight * x + partHeight, null);
gr.dispose();
}
}
return flagArray[rows][cols];
}
public static void main(String[] args)
{
ImageSwing imageSwing = new ImageSwing();
try
{
ImageFrame.splitImage(imageSwing.label);
} catch (IOException e)
{
e.printStackTrace();
}
imageSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageSwing.setSize(260,180);
imageSwing.setVisible(true);
}//main
}//class
Take a look at Java converting Image to BufferedImage
It provides a way to convert from Image to BufferedImage, which seems to be the problem.

Changing the position of images on the JFrame

So I am making this ABC learning game , What I want to do is if I click on the A button more than once then , the three Images will change their position, What I want to do is this
![enter image description here][1]
When I click on A button, the three image will appear on the screen, the first is apple as I set it that way in the loop, but the second two images will appear randomly, though sometimes one o them is apple again, I could fix that.
My Question is, how can I change that position of the Apple to the second and second image to the first and third image to the second position if the "A" button is clicked more than once.
SO, the result will be the apple will change position based on the click "A" button and other two picture changes their position and chosed randomly from the array.
So, here is my code for the JPanel, where everything takes place.Most of the code is explained in the comments
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import java.text.AttributedCharacterIterator;
import java.util.Random;
import javax.swing.ImageIcon;
/**
*
* #author Dip
*/
public class AbcGeniusPanel extends JPanel implements ActionListener {
//Declare the necessary Variables here
private JButton[] buttons; //create an array for buttons
private BorderLayout layout; //Declare object of BorderLayout
private Image image = null;
private boolean showImage = false;
//Initialize all the variables here
static int index = 0;
int randNumber = 0, id = 0;
int q = 0, w = 0;
int buttonClick = 0;
//Store all the imahges that will appear on the screen into an String type array
private static String[] imageList = {"src/Images/1.png", "src/Images/2.png", "src/Images/3.png", "src/Images/4.png", "src/Images/5.png", "src/Images/6.png", "src/Images/7.png", "src/Images/8.png", "src/Images/9.png", "src/Images /10.png",
"src/Images/11.png", "src/Images/12.png", "src/Images/13.png", "src/Images /14.png", "src/Images/15.png",
"src/Images/16.png", "src/Images/17.png", "src/Images/18.png", "src/Images /19.png", "src/Images/20.png",
"src/Images/21.png", "src/Images/22.png", "src/Images/23.png", "src/Images /24.png", "src/Images/25.png",
"src/Images/26.png"
};
//Define the constructor here
public AbcGeniusPanel() {
ImageIcon[] alphabets = new ImageIcon[26];
setBackground(Color.yellow);
//Load the images for alphabet images into the alphabets array using a for loop
for (int i = 0; i < alphabets.length; i++) {
alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\" + (i + 1) + ".png");
}
//Create a JPnael object
JPanel panel = new JPanel();
//Set a layoutManager on the panel
//panel.setLayout(new FlowLayout(FlowLayout.CENTER)); //This is not workling good
panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now
//Create an array for holdoing the buttons
buttons = new JButton[26];
//This Loop will Store the buttons in the buttons array attatching each image for each button
//Try passing Images inside the JButton parameter later.
for (int i = 0; i < 26; i++) {
buttons[i] = new JButton(alphabets[i]);
}
// Now Setting up a new Borderlayout so that we can set the whole gridLayout at the botton of the panel
setLayout(new BorderLayout(2, 0));
//add the panel to the Border layout
add(panel, BorderLayout.SOUTH);
//Add evenHandling mechanism to all the buttons
for (int k = 0; k < 26; k++) {
buttons[k].addActionListener(this);
}
for (int count1 = 0; count1 < 26; count1++) {
panel.add(buttons[count1]);
}
}
//This Method will generate a random Number and return it
public int random_number() {
int rand_num;
Random generator = new Random(System.currentTimeMillis());
rand_num = generator.nextInt(26);
return rand_num;
}
//This method will draw the font on the Panel
public void paintComponent(Graphics g) {
Font font; //Declare Font object here
font = new Font("Wide Latin", Font.BOLD, 22); //Set font
super.paintComponent(g); //Ensure the drawing in super class
g.setFont(font); //Set the font
g.setColor(Color.RED);
String text = "CLICK ON THE RIGHT IMAGE!"; //Display the text
g.drawString(text, 255, 20);
}
//To draw the picture on the screen we need to override the paint Method
#Override
public void paint(Graphics g) {
super.paint(g);
//Here, x and y will determine the x and y position os each image
int x = 0, y = 0;
// the varibale q is declared above
for (q = 0; q < 3; q++) //This loop will generate three images on the screen
{
if (showImage) {
x = x + 265; //X-Position of the image
y = 90; //Y-Position of the image
//q is declared as q=0, so this will always be true
if (w == 1 || q == 0) {
g.drawImage(image, x, y, image.getWidth(null), image.getHeight(null), null); //This method will put the image on the screen
showImage = true;
w = 0;
}
while (true) //this loop will run anyway
{
//go inside this loop only when the generated random
//doesn't match with the index of the button that was pressed
while ((randNumber = random_number()) != index) {
index = randNumber; //Now put the randomVlaue in the index
this.image = new ImageIcon(imageList[randNumber]).getImage();
showImage = true;
//make w=1 so that we can break from the outer loop
w = 1;
//break from the inner loop
break;
}
//Since we have made the w=1, so we are breaking out of the outer loop
if (w == 1) {
break;
}
}
}
}
}
#Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
id = 0;
while (true) {
//id is set to zero, for example if the button A (buttons[0])is not pressed then it will go below
//to increase id until it matches the index of the button that we pressed
if (source == buttons[id]) {
//get the image of that same index of the buttons and then set the showImage true
//SO the the paint function above can draw the image
this.image = new ImageIcon(imageList[id]).getImage();
showImage = true;
//save the index of the button that is presed in another variable
//then break from the while loop
index = id;
break;
} else {
id++;
//This is necessary to make sure that id will cross 26
//becasue we have only 26 letters or the array index is 26
//so highest value can be 26 only
id = id % 26;
}
}
repaint();
}
}
Add 3 JLabels or JButtons (whatever will be displaying the images) into a JPanel container. The JPanel will likely use a GridLayout(1, 3, horizontal_gap, 0) layout.
Place all images as ImageIcons into an ArrayList.
Shuffle the ArrayList when needed
After shuffling place the Icons into the JLabels/JButtons in a for loop using the setIcon(...) method.
Note that
your JPanel should override paintComponent, not paint. The paint method is responsible for painting a component's children and borders, and it does not use double buffering by default, making a more dangerous method to override.
Putting in a while (true) loop into your Swing GUI without regard to threading is extremely dangerous.
Putting this into a painting method such as paint is GUI suicide. Never do this since a painting method is a major determinant in the perceived responsiveness of your program. If you slow it down, the program will be perceived as being slow and poorly responsive, and thus it must be lean and fast as possible, and you should have painting and only painting code within it.
Your paint method has program logic in it, something that also shouldn't be done. You don't have full control over whether or even if a painting method will be called, and so program logic should never be placed inside one of these.
As MadProgrammer well notes, don't use the src path for your images as this won't exist once you build your program into a jar file. Better to Create a resource directory in the jar file, and to refer to your images as resources, not as files.

Selecting a JLabel by knowing its Name

If I have many JLabels on the screen and I know their names how would I go about selecting finding them?
For example I know that I previously (dynamically) created a new JLabel with the name 2340. Is there something like
JLabel image = findJlabel ("2340");
In order to select the JLabel component?
Thanks,
Neco
EDIT: Just want to show how I am creating these JLabels
// Initially creates all the imagelabels
public static void createImageLabels (){
int xcord, ycord;
JLabel image;
String imageLabelName;
xcord = 0;
ycord = yOffset;
for (int row = 0 ; row < map.length; row++) {
xcord = 0;
for (int col = 0 ; col < map[0].length; col++) {
imageLabelName = Integer.toString(row);
imageLabelName += Integer.toString(col);
image = new JLabel(new ImageIcon(space));
image.setName(imageLabelName);
image.setLocation(xcord, ycord);
image.setSize(24, 24);
imagePanel.add(image);
System.out.println ("Created a Jlabel Named "+imageLabelName);
xcord += 24;
}
ycord += 24;
}
}
I create a tile of imageIcons on the screen and then later on if I want to change the image displayed by them I want to select it and change it.
Well, assuming you have different names for all labels, i would recommend you using HashMaps.
HashMap<String, JLabel> labels = new HashMap<String,JLabel>();
Inside you "for" sicle, use:
labels.put("1233", new JLabel(new ImageIcon(space)));
To use your the label you want, use:
labels.get("1233");
For more information, check:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
If you only want to find the component with the concept of the current container it easy, if you want to search child or parent containers, it becomes little more complicated...
public JLabel findLabelByName(Container parent, String name) {
JLabel found = null;
if (name != null) {
for (Component child : parent.getComponents()) {
if (child instanceof JLabel) {
JLabel label = (JLabel)child;
if (name.equals(label.getName()) {
found = label;
break;
}
}
}
}
return found;
}
Now, if you want to search up or down the container hierarchy, you would need to perform a recursive call to this method, passing in the new parent, but I'm sure you can figure that out, don't want to rob you of all the fun ;)
In order to select the JLabel component?
how are you 'selecting' the label?
if it's via the mouse, just add a mouseListener and use getSource()

Two icons in a JLabel?

I have an icon in a JLabel as shown below:
Is it possible to add another icon (e.g. a flag representing a country) between the color icon and the text? For example, I want to add an icon depicting the U.S. flag between the red icon and US. Thanks!
Yes, use nested JLabel with BoxLayout in the container label:
JLabel container = new JLabel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JLabel icon1Label = new JLabel();
JLabel icon2Label = new JLabel();
icon1Label.setIcon(icon1);
icon2Label.setIcon(icon2);
container.add(icon1Label);
container.add(icon2Label);
Try CompoundIcon
Edit: Heisenbug's layout-based solution is the
trivial answer.
I just did this recently - I wanted to be able to combine multiple icons in a single row into a single icon. The way I did it was to go down to the BufferedImage level and compose the two images manually into a single image, and then use that as my JLabel icon. There are other ways of achieving the same effect, but I didn't want to have to change my UI component hierarchy.
I ended up creating a class that combines multiple images and caches them. I used it like this:
ImageIcon icon1 = ...;
ImageIcon icon2 = ...;
ImageIcon labelIcon = new CachedCompositeIcon( icon1, icon2 ).getIcon();
jLabel.setIcon( labelIcon );
Here's the source:
/** This is a convenience class to handle creating a single composite icon from several icons, and caching the
* created icons to eliminate duplicate work. This class is basically used as a key into a map, allowing us to
* define both a hashCode and equals in a single place.
*/
public class CachedCompositeIcon
{
private static final byte ICON_PADDING = 2;
private static final HashMap<CachedCompositeIcon, ImageIcon> CACHED_ICONS =
new HashMap<CachedCompositeIcon, ImageIcon>( 4 );
private final ImageIcon[] m_icons;
public CachedCompositeIcon(final ImageIcon... icons) {
m_icons = icons;
}
public ImageIcon getIcon() {
if ( !CACHED_ICONS.containsKey( this ) ) {
CACHED_ICONS.put( this, lcl_combineIcons() );
}
return CACHED_ICONS.get( this );
}
/** Generates an icon that is a composition of several icons by appending each icon together with some
* padding between them.
*
* #return An icon that is the concatenation of all the icons this was constructed with.
*/
private ImageIcon lcl_combineIcons() {
// First determine how big our composite icon will be; we need to know how wide & tall to make it.
int totalWidth = (m_icons.length - 1) * ICON_PADDING; // Take into account the padding between icons
int minHeight = 0;
for ( int i = 0; i < m_icons.length; ++i ) {
totalWidth += m_icons[i].getIconWidth();
if ( m_icons[i].getIconHeight() > minHeight ) {
minHeight = m_icons[i].getIconHeight();
}
}
// Create an image big enough and acquire the image canvas to draw on
final BufferedImage compositeImage = new BufferedImage( totalWidth, minHeight, BufferedImage.TYPE_INT_ARGB );
final Graphics graphics = compositeImage.createGraphics();
// Iterate over the icons, painting each icon and adding some padding space between them
int x = 0;
for ( int i = 0; i < m_icons.length; ++i ) {
final ImageIcon icon = m_icons[ i ];
graphics.drawImage( icon.getImage(), x, 0, null );
x += icon.getIconWidth() + ICON_PADDING;
}
return new ImageIcon( compositeImage );
}
/** Generates a hash that takes into account the number of icons this composition includes and the hash &
* order of those icons.
*
* #return A hash code.
*/
#Override
public int hashCode() {
int weakHash = m_icons.length;
for ( int i = 0; i < m_icons.length; ++i ) {
weakHash += m_icons[i].hashCode() * (i + 1);
}
return weakHash;
}
/** Two instances are equal if and only if they include the same icons and they're in the same order.
*
* #param obj An object to check for equality with this.
*
* #return true if the two objects are equal, false otherwise.
*/
#Override
public boolean equals(final Object obj) {
if ( !(obj instanceof CachedCompositeIcon) ) {
return false;
}
final CachedCompositeIcon other = (CachedCompositeIcon) obj;
if ( m_icons.length != other.m_icons.length ) {
return false;
}
for ( int i = 0; i < m_icons.length; ++i ) {
if ( m_icons[i].hashCode() != other.m_icons[i].hashCode() ) {
return false;
}
}
return true;
}
}
this is pretty possible, JLabel is Swing JComponent and you can add any JComponent to the another JComponent, that's same for JLabel
label.setLayout(new GridLayout(0, 2, 10, 10));
label.add(myIcon1);
label.add(myIcon2);
if you add f.e. JPanel to the JLabel then don't forget to setOpaque(false);

Categories

Resources