Okay, I'm just getting used to OOP and am just now learning swing. I'm making a simple app that's a 2x2 grid of 4 images (an X, an O, a Square, and a Triangle) and clicking any one switches the color's shape to blue.
I cannot get it to switch to the new image though, and I think it has something to do with something fundamental to my program.
Mind taking a look?
JFrame class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
public class Frame1 {
public JFrame frame;
Frame1 window = new Frame1();
window.frame.setVisible(true);
}
public Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Squares x = new Squares("images\\black-X.png", "images\\blue-X.png", 0, 0, 450, 450, "x");
Squares o = new Squares("images\\black-O.png", "images\\blue-O.png", 450, 0, 450, 450, "o");
Squares sq = new Squares("images\\black-sq.png", "images\\blue-sq.png", 0, 425, 450, 450, "sq");
Squares tri = new Squares("images\\black-tri.png", "images\\blue-tri.png", 450, 410, 450, 450, "tri");
frame.getContentPane().add(x.getLabel());
frame.getContentPane().add(o.getLabel());
frame.getContentPane().add(sq.getLabel());
frame.getContentPane().add(tri.getLabel());
}
}
Mouselistener Class:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class clickListener implements MouseListener{
Squares ob = new Squares();
public clickListener(Squares newSquare) {
ob = newSquare;
}
public void mouseClicked(MouseEvent e) {
ob.changePic();
}
}
And then an object class I'm creating for each image
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Squares {
String pic1, pic2, name;
int x, y, width, height;
JPanel panel = new JPanel();
JLabel label = new JLabel();
public Squares() {
;
}
public Squares(String pic1, String pic2, int x, int y, int width, int height, String name) {
this.pic1 = pic1;
this.pic2 = pic2;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.name = name;
BufferedImage myPic1 = null;
try {
myPic1 = ImageIO.read(new File(pic1));
} catch (IOException ex) {System.out.println("error in image upload");}
/*
panel.setBounds(x, y, width, height);
panel.add(new JLabel(new ImageIcon(myPic1)));
panel.addMouseListener(new clickListener(this));
*/
label = new JLabel(new ImageIcon(myPic1));
label.setBounds(x, y, width, height);
label.addMouseListener(new clickListener(this));
}
public JLabel getLabel() {
return label;
}
public String getName() {
return this.name;
}
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {myPic2 = ImageIO.read(new File(pic2));}
catch(IOException ex) {System.out.println("error in image upload");}
label = new JLabel(new ImageIcon(myPic2));
label.setBounds(x, y, width, height);
label.repaint();
label.revalidate();
}
}
I was originally using JPanels that contained each JLabel, but I got rid of them all to simplify things.
So yeah, any suggestions are appreciated. Thanks!