I am still pretty new to Java and tried to find information about how to include a picture in a frame.
Some pictures have the right size and fit in my screen, however other definitely have to be scaled to make them fit in.
So far I got this, is there a way to improve it and scale the picture down? For example half of the size it has now? What do I have to change in the code?
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
class DisplayImage {
public static void main(String avg[]) throws IOException
{
DisplayImage img =new DisplayImage();
}
public DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("C://Address.jpg"));
BufferedImage ret = new BufferedImage(32,32,BufferedImage.SCALE_FAST);
ret.getGraphics().drawImage(img,0,0,32,32,null);
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You can scale an image with the following code:
image.getScaledInstance(-1, 25,
java.awt.Image.SCALE_SMOOTH);
Related
I copied the path of the image but no icon appears click on the image to see
no icon
I created simple frame and needed to include icon
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.Color;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JFrame title goes here");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of app
frame.setResizable(false); // prevent frame from being resized
frame.setSize(700,700);
frame.setVisible(true);
try {
URL resource = frame.getClass().getResource("/games.png");
BufferedImage image = ImageIO.read(resource);
frame.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
frame.getContentPane().setBackground(new Color(123,50,250));
}
}
I am attempting to make a game and I am using a tileset I found(png). I want to be able to break the image up into parts and place them in specific places in the jframe to show movment. But first how would I begin to import the image. All attempts I have had have failed and the image turns to a text file when importing. Please help solve this problem and how would I cut up and arrange the tiles?
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at World.Display.<init>(Display.java:15)
at Main
.main(Main.java:15)
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
World.Display l = new World.Display();
}
}
package World;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Display {
public Display() throws IOException{
BufferedImage img=ImageIO.read(getClass().getResource("assets/tilesetbackground.png/"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setTitle("Another RPG Version: "+Config.Global.Version);
frame.setLayout(new GridLayout(1024,768));
frame.setSize(1024,768);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I'm sure the correct format of the string should starts from /
I checked it locally. Please take a look to the picture
I found a GIF I want to add into one of my Java programs. When I run it the picture shows up and works, but it leaves a trail behind it (it isn't actually replacing the white behind the picture) and the window is incorrectly sized. I searched before but nothing fixed it.
package tasks;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class duane
{
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://i2.kym-cdn.com/photos/images/newsfeed/000/602/307/4d3.gif");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
What would I add in to make the window the correct size and how would I repaint it?
I've been looking for a way to paint the JSwing component to an image for which I found many answers.
What I haven't found is the way to paint the component in such a way for it to ignore current window size and print itself to an image at full preferred size.
I have components in a re-sizable frame. After making it smaller some components are cropped.
What I want is to paint this frame into an image at its preferred size regardless.
BufferedImage img = new BufferedImage(getPreferredSize().width,
getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
this.printAll(g);
This results in an image as seen on the resized window, but I want it to paint itself like it has enough preferred space regardless of window or computer screen size.
Edited.
Here is SSCCE:
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(100, 100);
JLabel lbl = new JLabel("Long text. Long text. Long text. Long text.");
JPanel panel = new JPanel();
panel.add(lbl);
JButton btn = new JButton("Screenshot");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
BufferedImage img = new BufferedImage(getPreferredSize().width,
getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
printAll(g);
try {
ImageIO.write(img, "jpeg", new File("image.jpg"));
} catch (IOException ex) {
}
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(btn, BorderLayout.PAGE_START);
}
public static void main(String[] args) {
Test t = new Test();
t.setVisible(true);
}
}
This creates a small window with a button and a label which is too long to be displayed fully. Button takes the screenshot as is. What I want is an image.jpg which has fully visible texts on button and label, as if the frame was large enough for them to fit.
If I do this:
Graphics2D g = img.createGraphics();
lbl.printAll(g); // instead of printAll(g);
I get the label fully printed to image.jpg (ignoring the frame boundaries). I want to do that with all the components of a frame.
save an image that is generated by merging an image over another image?
i have an image first and i want to insert some text over this image in a specified position given....that i got coorectly..bt the new task is to place this last generated image over another image template in the location given and save it as a new jpg image in my work directory..
Here is an example of how to use Image Overlay using java2D. Since you tagged also with [jquery] I'm not sure if you want to do this using jquery or java.
I meant the 2nd Snippet, repaired and runs
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class WaterMark {
public static void main(String[] args) throws IOException {
URL url = new URL("http://sstatic.net/so/img/logo.png");
BufferedImage im = ImageIO.read(url);
URL url2 = new URL("http://sstatic.net/sf/img/logo.png");
BufferedImage im2 = ImageIO.read(url2);
Graphics2D g = im.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight())/2, null);
g.dispose();
display(im);
ImageIO.write(im, "jpeg", new File("sample_output.jpeg"));
}
public static void display(BufferedImage image) {
JFrame f = new JFrame("WaterMark");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JLabel(new ImageIcon(image)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}