public class image {
JFrame pen = new JFrame();
public image () {
pen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pen.setBounds(150, 100, 613, 231);
pen.setVisible(true);
try {
URL url = new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg");
BufferedImage bI = ImageIO.read(url);
ImageIO.write(bI, "jpg", new File("C:\\kibAr.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
I dont have error but dont work why?(I want to use BufferedImage)
And how i can set the window background this graphic?
Sory for my bad english
If by work you mean display the BufferedImage on the frame, then that's because there's no code where you're actually adding it to the frame at all!
You may wish to have a look here for some examples of how to do this.
The quickest way would probably be something along the lines of:
JLabel picLabel = new JLabel(new ImageIcon(bI));
pen.add(picLabel);
Related
This is so frustrated. The code only work on manual generated but not work in auto generate ???
There is an image in project "Bird.png".
public ComboBox() {
initComponents();
try {
image = ImageIO.read(new File("Bird.png"));
lblShow = new JLabel(new ImageIcon(image.getScaledInstance(300, 300, Image.SCALE_SMOOTH)));
} catch (Exception e) {
}
}
This is my manual generate code :
public MainFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,400);
//create new panel and add panel to frame
JPanel pnlImg=new JPanel();
add(pnlImg);
//create new label for showing image
JLabel lblShowImg;
BufferedImage image = null;
try {
image = ImageIO.read(new File("1.jpg"));
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon ii=new ImageIcon(image.getScaledInstance(300,300,Image.SCALE_SMOOTH));
lblShowImg=new JLabel(ii);
/*add label to panel */
pnlImg.add(lblShowImg);
/* show frame*/
setVisible(true);
}
Why is this not working?
public ComboBox() {
initComponents();
try {
image = ImageIO.read(new File("Bird.png"));
lblShow = new JLabel(new ImageIcon(image.getScaledInstance(300, 300, Image.SCALE_SMOOTH)));
} catch (Exception e) {
}
}
Likely due to your not taking layout managers into account, but the truth is, as written it is impossible to say
You never show where you add the JLabel to any container
You don't indicate what layout manager the container is using
You haven't posted a valid mcve.
To possibly solve this --
allow NetBeans to generate a JPanel
Add your JLabel to your own JPanel, one whose layout you control.
Add both to a JFrame (respecting its contentPane's layout) that you display.
I am trying to make an image writer that will read a template image and then write text on it, then create a new image with the new text. For some reason I am getting errors when my code seems fine to me. Here is the code below:
public class GUI extends JFrame{
private JPanel p1 = new JPanel();//Puts tiles in, and organizes them for you
private JPanel p2 = new JPanel();//Holds trash tiles
JLabel ll = new JLabel();
//private JPanel p3 = new JPanel();//Holds trash tiles
public GUI(){
this.setTitle("Tile Game");
this.setSize(600,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
try {
createTile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.add(ll);
}
public void createTile() throws IOException{
final BufferedImage image = ImageIO.read(getClass().getResource("/src/tile.png"));
//File outPut = new File("saved.png");
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString("Hello World!", 100, 100);
g.dispose();
ImageIO.write(image, "png", new File("/src/test2.png"));
final BufferedImage image2 = ImageIO.read(new File("/src/test2.png"));
ImageIcon icon1 = new ImageIcon(image2);
ll.setIcon(icon1);
p1.add(ll); }
}
When I run the code above, I end up getting theses errors:
GUI.<init>() line: 36
Start.main(String[]) line: 7
I don't understand why I am also asked to debug the GUI constructor...It all seems to work perfectly fine to me.
your image location is not suitable! (ignore the other classes, it's just a test project ^^)
make an folder in your eclipse project BUT NOT IN YOUR SOURCE dir (!!!) and then refer to the image like this:
Image img1 = Toolkit.getDefaultToolkit().getImage("img/index.png");
I am trying to display a string and a BufferedImage onto a JFrame that both come as output from a method. I am not able to separate the String from the image, therefore I need to add both to the JFrame.
Here is the code I have do far and nothing is displaying. Thank you very much for your help in advance.
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
f.setSize(400,400);
f.setVisible(true);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);
You can render the string over the image using drawString(), as shown here.
Alternatively, you can use label.setText() and rely on the label's horizontal & vertical alignment for positioning, as illustrated here.
Basically, you can use the label's setText method to supply a text value for the label.
You also need to "add" the label to the frame, or it won't display anything.
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel("This is some text", icon, JLabel.CENTER);
f.add(label);
f.setVisible(true);
Hovercraft Full Of Eels actually answered my question. I wanted to return both a String and BufferedImage but now I know that is not possible. Thank you all for your help :)
How is it possible to store a JPanel as a jpeg image the solutions I could search takes a snapshot of the display, this does not capture all the contents of the JPanel as part of it is hidden. Is there any way of getting the entire image in jpeg.
This solution available here is not working fine at all solution
code I am currently using to take snapshot of JPanel
if (panelx == null) {
jButton3.setText("Generate Diagram");
} else {
jButton3.setText("Generate Diagram");
jTextPane1
.setText(Messages.getString("statement2")); //$NON-NLS-1$
JFrame win = (JFrame) SwingUtilities
.getWindowAncestor(panelx.panelx);
Dimension size = win.getSize();
// BufferedImage image = new BufferedImage(size.width, size.height,
// BufferedImage.TYPE_INT_RGB);
BufferedImage image = (BufferedImage) win.createImage(size.width,
size.height);
Graphics g = image.getGraphics();
win.paint(g);
g.dispose();
try {
ImageIO.write(
image,
Messages.getString("Statement38"), new File(Messages.getString("statement5"))); //$NON-NLS-1$ //$NON-NLS-2$
} catch (IOException e) {
System.out.println("Image cannot be created");
}
You don't explain why the example doesn't work.
Why are you using the window not the panel? If you were using the example answer you should refer to the panel within the scrollpane.
You're calling paint on the whole JFrame rather than just the panel you are interested in.
I'm trying to load an animated GIF in a JLabel.
While this works:
URL urlsd;
try {
urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
ImageIcon imageIcon = new ImageIcon(urlsd);
JLabel progress = new JLabel(imageIcon);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
This, on the other hand, does not, and I don't want to get the GIF from an URL, since I already have the GIF. Result of loading this shows only the first frame of the GIF:
try {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));
JLabel progress = new JLabel(imageIcon);
imageIcon.setImageObserver(progress);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
I guess there must be a reason for this, but I cannot find it.
Thanks!
Alex
You can try loading your GIF file like that:
public class Test extends JPanel {
public Test() {
ImageIcon imageIcon =
new ImageIcon(Test.this.getClass().getResource("starzoom-thumb.gif"));
}
}
Or using Test.class.getResouce() if your context is static.
Below code worked for me, it will show the animation instead of first frame of the image.
public class Test extends JPanel {
public Test() {
ImageIcon yyyyIcon = new ImageIcon(xxx.class.getClassLoader().getResource("yyyy.gif"));
connectionLabel.setIcon(yyyy);
}
}
So there is also a simpler way of doing it. The following line is how I did it.
this.setContentPane(new JLabel(new ImageIcon("Path To Gif File")));