0

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();
 }  
Jin Lee
  • 3,194
  • 12
  • 46
  • 86
  • 1
    `new loadImage(n);` create a new lable so you need to add it to your frame. create a `setImage()` on your `LoadImage` class and call it instead of making new lables – Madhawa Priyashantha Sep 20 '15 at 15:09
  • is it pointless to update imageArea? That's where the image is supposed to be. Do I have to do: JLabel x = new loadImage(n); super.add(x); then revalidate and repaint? I'm sorry again, my PC is old and lags when I open mozilla and the program to code so I have the coding program off at the moment. – RandomInquirer Sep 20 '15 at 15:11
  • wait do you want to change image on a lable or add new lable with a different image ? – Madhawa Priyashantha Sep 20 '15 at 15:13
  • I want to change the image on the existing label. – RandomInquirer Sep 20 '15 at 15:13
  • 1
    then you can make a method like `setImage(int imageToUse);` then call `lablename.setImage();` move `constructor` code to set method – Madhawa Priyashantha Sep 20 '15 at 15:16
  • 1
    @FastSnail: your comments look like answers to me. – Hovercraft Full Of Eels Sep 20 '15 at 15:17
  • In my head the code would still be the same in setImage(). What difference would there be in a setImage()? (Ah sorry. I'm slow on the upkeep, I'm reading the older messages.) I have the IDE open. I'll do that right now. – RandomInquirer Sep 20 '15 at 15:18
  • @randominquireralso read images one time .you read them again and again.your image set code exist inside the constructor not in a method. – Madhawa Priyashantha Sep 20 '15 at 15:20
  • imageArea isn't inheriting ".setImage(n)". – RandomInquirer Sep 20 '15 at 15:25
  • @RandomInquirer can you show your updated part? – Madhawa Priyashantha Sep 20 '15 at 15:27
  • Ah sorry, I just added them now. The default would be the first image (resp). So that when I call loadImage() in the Custom Creation Code portion in Netbeans, it'll load that up. – RandomInquirer Sep 20 '15 at 15:28
  • @RandomInquirer yes. so now you can call `imageArea .setImage(1)` – Madhawa Priyashantha Sep 20 '15 at 15:33
  • private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { imageArea.setImage(1); super.revalidate(); super.repaint(); } //It's an error. It says missing symbol. Is there any reason as to why that JLabel it's not inheriting the method in loadImage? – RandomInquirer Sep 20 '15 at 15:35
  • @RandomInquirer not need super .`this.repaint()` or ` imageArea.repaint()` .i think you don't need to call revalidate. you should change `private javax.swing.JLabel imageArea;` to `loadImage imageArea ` – Madhawa Priyashantha Sep 20 '15 at 15:37
  • The error is on imageArea.setImage(n). It says that there isn't a setImage(n) method for it. – RandomInquirer Sep 20 '15 at 15:39
  • 2
    No, JLabel has no `setImage(...)` method. You use `setIcon(Icon icon)`. This is what the Java API is for. – Hovercraft Full Of Eels Sep 20 '15 at 15:40
  • @RandomInquirer hmm as Hovercraft said why do you use `drawImage` with repaint .you can use `setIcon` built in method of jlable – Madhawa Priyashantha Sep 20 '15 at 15:42
  • Ah I thought I'd be stuck there, since Netbeans doesn't allow you to outright modify the variable's data type. But I remember reading a way around that. Typecasting ftw! Thanks Fast Snail. It works now. – RandomInquirer Sep 20 '15 at 15:46
  • 1
    I ended up doing ((loadImage)imageArea).setImage(2); – RandomInquirer Sep 20 '15 at 15:47
  • @RandomInquirer good luck but consider what @Hovercraft said . you can do much easily using `setIcon();` method – Madhawa Priyashantha Sep 20 '15 at 15:48
  • I did fiddle about with setIcon before I decided to go with this, but I couldn't get the datatypes to convert right. Typecasting didn't help since they just weren't possible. But I most likely did it wrong. It's what I like about programming. It's not magic. – RandomInquirer Sep 20 '15 at 15:50
  • 1
    Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Sep 20 '15 at 15:51
  • Thanks for the additional information, Andrew Thompson. I will definitely look into that. Oh and thanks again Fast Snail and Hovercraft Full of Eels. Can I still go about with image zooming and repositioning with setIcon? I think storing the altered image as a new image would help. But I don't really know how to go about it that way given my limited knowledge of paint. I will try my best to understand it. It doesn't feel like it should just end like this. Or is it okay for it to just all be comments that solved the question? – RandomInquirer Sep 20 '15 at 16:00

0 Answers0