How to resize images in JScrollPane as frame resizes - java

I need to develop an image gallery of sorts, where I can display images in a JFrame, and I can scroll and double click to expand to full screen etc. I was able to display the images the way I want it, but when I resize the JFrame, the images gets cut off as there is no horizontal scroll.
Is there a way where I can have have vertical scroll and when frame resizes, the panel within will resize to fit horizontally?
try {
BufferedImage readImage = ImageIO.read(new File("myFile.png"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(readImage, "png", bos);
byte[] data = bos.toByteArray();
BufferedImage image = ImageIO.read(bis);
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new GridLayout(0,2,5,5));
for (int x=0;x<8;++x){
JLabel l = new JLabel(new ImageIcon(image));
imagePanel.add(l);
}
JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame();
frame.add(scrollPane);
//frame.add(imagePanel); //This will work as i intended it to be, but there is no vertical scroll
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (IOException e) {
e.printStackTrace();
}
Edit:
A visual example is shown below. When the frame resizes, the images get cropped, what I would like instead are for the images within the JScrollPane to resize so that it could all fit horizontally, while at the same time maintaining the vertical scroll.
Edit2:
Using StretchIcon, I was able to achieve having the images resize dynamically, but this introduced another issue where there are huge spaces in between the images as I resize.

Related

Make label with any image (gif and other)

What I want to do is:
Load an image that it is a gif, jpeg etc ... coming from a URL (but it is only the gif that I do not arrive but it must work with the others)
Put it in a JLabel in IconImage (although it suits me if it is not in the icon of the label as long as it is in the label)
Make it auto resize by taking all the space of the label but keeping its ratio
And finaly make this.add (label)
I already managed to do it with any type of image but the gifs was freeze to the first image
I searched for more solutions but could not find any that worked for me.
So, if someone could help me from the very beginning. I have the URLs and can share, if required.
Exemple of my code
//that work but images is not resize
JLabel label = new JLabel();
label.setIcon(new ImageIcon(new URL(image.getPath())));
this.add(label);
//images don't appear
URL url = new URL(image.getPath());
JLabel label = new JLabel();
java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url);
img = this.scaleImage(img,Toolkit.getDefaultToolkit().getScreenSize().width/6); label.setIcon(new ImageIcon(img));
Try this, you may need to put it in a try-catch
BufferedImage image = ImageIO.read(new URL(url));
JLabel label = new JLabel(new ImageIcon(image));

Text not show correct FontAwesome and Swing

I tried to follow this example Font Awesome with Swing
Every thing work fine but when i tried to add some text the font not show correct, it show me something like this:
My code
....
try (InputStream is = TestFontAwsome.class.getResourceAsStream("fontawesome-webfont.ttf")) {
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(Font.PLAIN, 24f);
JLabel label = new JLabel("\uf0c0 font not correct");
label.setFont(font);
label.setForeground(Color.red);
label.setFont(font);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException | FontFormatException exp) {
exp.printStackTrace();
}
....
The result
I already installed the Font:
Any idea about the problem?
JLabel label = new JLabel("\uf0c0 font not correct");
The JLabel font is set to Font Awesome, which has no glyph for the ascii text provided in the JLabel. AFAIK there is not a way to mix the fonts within a single JLabel - you may be able to do this with some form of html, but a easier solution might be to just use two JLabels with different fonts.
JLabel l1 = new JLabel("\uf0c0");
JLabel l2 = new JLabel("This is ascii text");
l1.setFont(fontAwesome);
Box mix = Box.createHorizontalBox();
mix.add(l1); mix.add(l2);
myContainer.add(mix);
You can alternatively custom paint a Component using paintComponent, setting the Font as needed.

How to draw the subtitle that is in the ImageJ window

I have made my own customized GUI where it displays images that are opened and read by ImageJ. I am able to display the images but I can't display the subtitle that is displayed in the ImageJ window before the image.
This is what I have been trying but it doesn't seem to display anything. Is there something simple that I am missing?
JPanel panel2 = new JPanel();
ImageCanvas ic = new ImageCanvas(image);
StackWindow sw = new StackWindow(image,ic);
Insets insets = sw.getInsets();
BufferedImage bi = image.getBufferedImage();
Graphics2D g = (Graphics2D)bi.getGraphics();
//g.drawImage(image.getBufferedImage(), 0, 0, null);
g.drawString(sw.createSubtitle(), insets.left+5, insets.top+10);
//image.draw();
sw.drawInfo(g);
panel2.add(sw.getContentPane());
frame.add(panel2);

Adapting pixels of the image depending files and rows

I have this function where I receive String[][], f (rows), and c (columns). I've got to adapt my labels occupying all of my frame (1270,750).
For example, if I receive:
f=3 and c=3, the images will have: (423.333,250.) pixels
f=10 and c=10, the images will have (127,75) pixels.
I've proved with all the functions which are in the example, but it doesn't work. Any idea how to do it?
public void inicialitzamatriu(String[][] arraystrings,int f,int c) {
ff=f;
cc=c;
compsToExperiment=new JPanel();
GridLayout experimentLayout = new GridLayout(ff,cc);
compsToExperiment.setLayout(experimentLayout);
this.setContentPane(compsToExperiment);
for (int filas=0;filas<ff;filas++){
for (int columnas=0;columnas<cc;columnas++){
if (arraystrings[filas][columnas].equals("gat")){
JLabel cat2 = new JLabel();
cat2.resize(1270/cc, 750/cc);
cat2.setIcon(new ImageIcon("cat.png"));
compsToExperiment.add(cat2);
/*cat2.getSize();
//cat2.getWidth();
//cat2.getHeight();
cat2.resize(width, height);
cat2.setBounds(x, y, width, height)
cat2.setSize(width, height)
cat2.
*/
}
if (arraystrings[filas][columnas].equals("rat")){
JLabel rat2 = new JLabel();
rat2.setSize(1270/cc, 750/cc);
rat2.setIcon(new ImageIcon("raton.png"));
compsToExperiment.add(rat2);
}
if (arraystrings[filas][columnas].equals("menjar")){
JLabel comida2 = new JLabel();
comida2.setSize(1270/cc, 750/cc);
comida2.setIcon(new ImageIcon("comida.png"));
compsToExperiment.add(comida2);
}
if (arraystrings[filas][columnas].equals("res")){
JLabel nada2 = new JLabel();
nada2.setSize(1270/cc, 750/cc);
nada2.setIcon(new ImageIcon("nada.png"));
compsToExperiment.add(nada2);
}
}
}
this.repaint();
//compsToExperiment.repaint();
compsToExperiment.setVisible(true);
//this.setVisible(true);
//this.setVisible(false);
}
You need to do custom painting to scale the image to the space available in the grid.
You can try using Darryl's Stretch Icon (or maybe the Shrink Icon).

Displaying jpg image on JPanel

How can I display jpg image which I stored in arraylist in JPanel?
Im not able to display the jpg files in the JPanel.
String[] pictureFile = {"A.jpg","B.jpg","C.jpg"};
List<String> picList1 = Arrays.asList(pictureFile);
Collections.shuffle(picList1);
ImageIcon icon = new ImageIcon("picList1.get(0)");
JLabel label1 = new JLabel();
label1.setIcon(icon);
JPanel panel = newJPanel;
panel.add(label);
You should not put the call to the array in quotes.
Instead, you should try the following:
ImageIcon icon = new ImageIcon(picList1.get(0));
The problem is in the line
ImageIcon icon = new ImageIcon("picList1.get(0)");
It's interpreting the string as a file name. You should just need to unquote the picList1.get(0) bit.

Categories

Resources