-
ImageIcon and size
Hi guys,
I have problem,I´want change size at ImageIcon,but I can´t it.This is my code:
Code:
package dalsik;
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class Main extends JFrame{
public Main() throws Exception{
super("None");
setSize(400,400);
Container c=getContentPane();
FlowLayout layout=new FlowLayout();
c.setLayout(layout);
ImageIcon vv=new ImageIcon("C:\\image.jpg"); // I want resize this picture (image.jpg)
JLabel jl=new JLabel("",vv,JLabel.LEFT);
c.add(jl);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) throws Exception{
Main m=new Main();m.setVisible(true);
}
}
Can you help me??
Thanks for your answers.
-
So, rather than reply to morgalr's reply to your original post in the Sun Java forums here: Java 2D - ImageIcon and size
You have decided to waste someone else's time by cross-posting this same question here. That's not very considerate of you.
-
Code:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.*;
public class MainResize extends JFrame {
public MainResize() {
super("None");
// setSize(400,400);
Container c=getContentPane();
c.setLayout(new GridLayout(1,0));
// I want resize this picture (image.jpg)
String path = //"images/bison.jpg";
"C:/image.jpg";
URL url = getClass().getResource(path);
System.out.println("url = " + url);
ImageIcon vv=new ImageIcon(url);
JLabel jl=new JLabel("",vv,JLabel.LEFT);
c.add(jl);
c.add(new JLabel(scale(vv.getImage(), 0.75)));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private ImageIcon scale(Image src, double scale) {
int w = (int)(scale*src.getWidth(this));
int h = (int)(scale*src.getHeight(this));
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage dst = new BufferedImage(w, h, type);
Graphics2D g2 = dst.createGraphics();
g2.drawImage(src, 0, 0, w, h, this);
g2.dispose();
return new ImageIcon(dst);
}
public static void main(String[] args) {
new MainResize();
}
}
-
to hardwired:Thank you verry much!