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 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!