0

So I have small "game" that I'm working on and part of it has icons being displayed in rows (each one a jpanel). These Icon components I am adding are displayed perfectly when isolated, however when I add them to each of their rows they seems to not be displing at all. I'm new to swing so I'm wondering if it's because the images are a bit tinier than the rows they are being put into?

here is the code for the icon which runs fine independently:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import java.nio.file.Path;

public class fermiPicoNanoImage extends JPanel {
    private BufferedImage image = null;
    private int[] imageDims = new int[] { 83, 47};

    //for testing
    public String currentImage;

    public fermiPicoNanoImage() throws IOException {

    }

    public void set(String whichImage) throws IOException, IllegalArgumentException  {

        whichImage = whichImage.toLowerCase();

        try {

            if(  whichImage == "fermi"  || whichImage == "f"  ) {
                Path filePath = Paths.get("src\\resources\\icon-fermi.png").toAbsolutePath();
                image = ImageIO.read(  new File(  filePath.toString()  )  );
                currentImage = "fermi";
            }
            else if(  whichImage == "nano"  || whichImage == "n"  ){
                Path filePath = Paths.get("src\\resources\\icon-nano.png").toAbsolutePath();
                image = ImageIO.read(  new File(  filePath.toString()  )  );
                currentImage = "nano";
            } 
            else if(  whichImage == "pico"  || whichImage == "p"  ) {
                Path filePath = Paths.get("src\\resources\\icon-pico.png").toAbsolutePath();
                image = ImageIO.read(  new File(  filePath.toString()  )  );
                currentImage = "pico";
            }else {
                throw new IllegalArgumentException();
            }



        }catch(Exception err) {
            System.err.println( err.getMessage() );
            err.printStackTrace();
        }


        this.repaint();

    }


    public void unset(){
        image = null;
        currentImage = null;
        this.repaint();
    }



    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

       if(image != null) {
            g.drawImage(image, 0, 0, null);
       }else if(image == null) {
           g.drawImage(image, 0, 0, Color.WHITE, null);
       }


    }

    public static void main(String[] args) throws IOException{
        fermiPicoNanoImage fnpImage = new fermiPicoNanoImage();

        JFrame frame = new JFrame();
        frame.add(fnpImage);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);


       fnpImage.set("pico");


    }




}

the rows the custom components should be placed in

The JFrame that is rendering everything is here. And as suggested in other Stack posts, I added the components before the frame was the set to visible. I'm not sure if its a problem with the rendering or the with.

Now I couldn't find an easy way to get the main class to work for you guys, because of local images being used, so I put just the whole eclipse project into a google drive folder for y'all.

https://drive.google.com/drive/folders/12n-Ze2jivwVJ1XxgQILN5zKNRfZNJ6Z5?usp=sharing

Any suggestions about what i need to get the component to render would greatly help. thanks!

Leviathan_the_Great
  • 429
  • 1
  • 5
  • 14
  • 1) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. .. – Andrew Thompson Mar 25 '20 at 17:46
  • .. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 4) 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 Mar 25 '20 at 17:46
  • You only ever have 1 image loaded. You replace image with the new image you load and you draw just image. Also, notice that you check if `image` is null and if it is null. you try to paint it anyways. What is the point of that condition? – matt Mar 25 '20 at 18:03
  • the null is used in conjunction with the unset method to paint the canvas back to white. – Leviathan_the_Great Mar 25 '20 at 18:14
  • I should be more specific. `g.drawImage(image, 0, 0, Color.WHITE, null);` creates a white rectangle, it just uses the image file for the white image's dimensions – Leviathan_the_Great Mar 25 '20 at 18:22

1 Answers1

2

Any suggestions about what i need to get the component to render would greatly help

Did you verify if your code is being executed by adding System.out.println(…) statements inside your if conditions?

One possible problem is the following:

else if(  whichImage == "pico"  || whichImage == "p"  ) {

Don't use "==" for String comparison.

The "==" checks if the objects are the same instance, not if the value of the Object is the same.

Instead use the equals(…) method:

else if(  "pico".equals(whichImage) || "p".equals(whichImage)  ) {

The comparison ordered is changed so it will still work even if the "whichImage" variable is null.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I forgot im supposed to do that when comparing strings. I did change the code, however as I mentioned the `fermiPicoNanoImage` class runs on it's own fine. it's somewhere along the line where it goes off the rails – Leviathan_the_Great Mar 25 '20 at 18:18
  • 1
    You still haven't verified is the code actually gets executed. Post a proper [mre] that demonstrates the problem. – camickr Mar 25 '20 at 21:31