I've gone through a couple of questions here and I'd like to apologize in advance. I know the common advice is that you're best to start out without using GUI builders so as to have a better understanding of the works, as I understand that GUI Building in the hands of an beginner is like tackling the whole without really knowing about the parts.
The three important components are imageArea, toBoatBtn, and toRespBtn. I've already set imageArea's "Custom Creation Code" in Netbeans to "new loadImage(1);" to load the first image. And this bit is fine.
The two buttons are supposed to change the image in imageArea. toBoatBtn is supposed to load the 2nd image (loadImage(2)), and toRespBtn is supposed to load the initial image (loadImage(1)).
I don't know how to continue with this process. It isn't changing the images over. I've tried revalidating and repainting, changing the inheritance to JPanel and making the changes to that, but still no luck. It's not the case of a wrong pathname for the 2nd image because if I set the "Custom Creation Code" in Netbeans to "new loadImage(2);", it shows up as well. I really want to pursue with this instead of using the JLabel.setIcon(parameter) because there's the prospect of zooming in and re-positioning the image (I can't even figure this out, so that's definitely shaky). I appreciate any help.
(I removed the fluff comments and the code that netbeans doesn't allow you to touch.)
package source;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
class loadImage extends JLabel {
BufferedImage img;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img,0,0,getWidth(),getHeight(),this);
System.out.println("\nImgWidth: " + img.getWidth()
+ "\nFrameWidth: " + getWidth()
+ "\nImgHeight: " + img.getHeight()
+ "\nFrameHeight: " + getHeight()
);
}
public loadImage(int imageToUse) {
System.out.println("Image Num: " + imageToUse);
try {
if(imageToUse == 1)
{
img = ImageIO.read(new File("resp.png"));
}
else
{
img = ImageIO.read(new File("boat.png"));
}
} catch (IOException e) {}
}
}
/****************************************************************/
public class NewJFrame extends javax.swing.JFrame{
public NewJFrame() {
initComponents();
setLocationRelativeTo(null);
}
private void toBoatBtnActionPerformed(java.awt.event.ActionEvent evt) {
imageArea = new loadImage(2);
super.revalidate();
super.repaint();
}
private void toRespBtnActionPerformed(java.awt.event.ActionEvent evt) {
imageArea = new loadImage(1);
super.revalidate();
super.repaint();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel imageArea;
private javax.swing.JButton toBoatBtn;
private javax.swing.JButton toRespBtn;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel workspace;
// End of variables declaration
}
EDIT: loadImage changes
public loadImage() {
try {
img = ImageIO.read(new File("resp.png"));
} catch (IOException ex) {}
}
public void setImage(int imageToUse)
{
System.out.println("Image Num: " + imageToUse);
try {
if(imageToUse == 1)
{
img = ImageIO.read(new File("resp.png"));
}
else
{
img = ImageIO.read(new File("boat.png"));
}
} catch (IOException e) {
}
}
EDIT: Problem is solved thanks to Fast Snail. The help was well appreciated! I'll also read on about the setIcon method that Hovercraft Full of Eels' suggested.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
((loadImage)imageArea).setImage(2);
this.repaint();
}