Resize a picture to fit a JLabel in java - java

I know this question has been asked many times, but I can't get any of the answers to work for me.
here is the code produced by netbeans for my image:
jLabel2 = new javax.swing.JLabel();
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pathtomastery/pathToMastery.png"))); // NOI18N
progressPanel.add(jLabel2);
jLabel2.setBounds(130, 20, 460, 80);
What can I add to this code to make my image resize to fit the size of the label?

You can check out Darryl's Stretch Icon. The image will automatically be scaled to fill the space available to the label.

Have you tried:
jLabel2.setPreferredSize(new Dimension(460, 80));

Set the label with a new icon and use BufferedImage like:
BufferedImage bi = new BufferedImage(
100, 100, BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(image, 0, 0, null);
label.setIcon(new ImageIcon(bi));

Related

JavaFX create a new image from an existing one

In JavaFX u can create a new Image from a string(path), how would i go about creating a new Image from an existing javafx.scene.image.image?
as following:
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);
But instead of the path an actual image object.
I want to change the size of the image.
There is typically no need to create a new Image instance in order to perform rescaling. The API allows you to view or draw scaled versions of an existing Image instance. For example, given
Image image = new Image("my/res/flower.png");
you can create an ImageView that displays a scaled version with
ImageView imageView = new ImageView(image);
imageView.setFitWidth(100);
imageView.setFitHeight(150);
or you can draw a scaled version to a canvas with
Canvas canvas = ... ;
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, 100, 150);
A late response, but since I ran into the same issue (I needed an Image to pass to setDragView() ), here is the solution I have implemented:
Image makeThumbnail(Image original) {
ImageView i = new ImageView(original);
i.setFitHeight(64); i.setFitWidth(64);
return i.snapshot(new SnapshotParameters(), new WritableImage(64, 64));
}

Add Translucent Image to JLayeredPane

So I'm making a stopmotion application - the live feed from my webcam is inside a JPanel (with setOpaque(false)) that is inside the top (10th) layer in a JLayeredPane - and basically, when I take a picture, I want to add it to one of the lower layers so that a trace of the previous pictures you've taken shows up on screen. Here's how I'm trying to do that now:
EDIT: this is my new code based off the answer below - this now does nothing, as opposed to just adding the opaque image as before - if I add this to a JPanel, though, and add the JPanel to the JLayeredPane, then all I get is gray
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon imgIcon = new ImageIcon(new ImageIcon(img2).getImage().getScaledInstance(img2.getWidth(), img2.getHeight(), Image.SCALE_SMOOTH));
JLabel showPic = new JLabel(imgIcon);
showPic.setSize(layers.getSize());
showPic.setBounds(layers.getX() + 18, layers.getY(), img2.getWidth(), img2.getHeight());
layers.add(showPic, new Integer(1)); //layers is my JLayeredPane
layers.repaint();
layers.revalidate();
img is the picture I've just captured from my webcam, and I'm trying to make it semi transparent, then add it to a JLabel. How can I make this work? Or is there a better way to do this?
I don't know if this will solve the problem but the image you are looking for is
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
java.awt.Transparency.TRANSLUCENT has the value of 3 and corresponds to BufferedImage.TYPE_INT_ARGB_PRE which only god knows what it's doing.
With BufferedImage.TYPE_INT_ARGB you can create a transparent image and apply Composite etc
You also need to correct this
g.drawImage(img2, null, 0, 0);
to
g.drawImage(img2, 0, 0, null);
--
Heres how transparency works for me:
I have two images bim and bim2 and I draw one on top of the other:
BufferedImage bim=null, bim2=null;
try {
bim=ImageIO.read(new File("...."));
bim2=ImageIO.read(new File("...."));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
int wc=bim.getWidth(), hc=bim.getHeight();
BufferedImage img2 = new BufferedImage(bim.getWidth(), bim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(bim, 0, 0, null);
g.drawImage(bim2, 0, 0, wc, hc, null);
Then I can display it on a JPanel Jframe or whatever.
I can even create the Label:
JLabel showPic = new JLabel(new ImageIcon(img2));
JFrame f=new JFrame();
f.setSize(500, 500);
f.add(showPic);
f.setVisible(true);

Set Font type to Futura in Java

Every thing works fine except that it does not print Futura Font on the picture just a default looking font
BufferedImage image = ImageIO.read(new File("image.jpeg"));
Graphics g =image.getGraphics();
g.setFont(new Font("Futura", Font.PLAIN, 45));
g.drawString("Hello", 100, 440);
ImageIO.write(image, "png", new File("test.png"));
Maybe this can help :)
new font type in java
and this one: How to use Open Type Fonts in Java?
Follow links on 1st link

How to change font style of text on image in java?

I am creating image dynamically and writing on image, there are limited set of font style.I want to change it to some other style in "Kunstler Script".
here is my code:
BufferedImage image = new BufferedImage(imageWidth,
imageHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.clearRect(0, 0, imageWidth, imageHeight);
g2.drawImage(image, 0, 0, null);
g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 65));
g2.setColor(Color.orange);
g2.drawString(" MY IMAGE", 350, 70);
g2.drawLine(350, 80, 1000, 80);
g2.dispose();
File out = new File("E:/" + IMAGE + "_1.png");
ImageIO.write(image, "png", out);
here i want to change the font style
g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 65));
at the place of "SANS_SERIF" i want some another font like "Kunstler Script"
please anybody help me to FIX it.
Thank you.
Assuming Kuntsler Script is an available font, it's as simple as:
g2.setFont(new Font("Kunstler Script", Font.BOLD, 65));

How to draw char of Wingding.ttf font with Java Graphics.DrawString?

I am trying to draw characters from Wingding.ttf font with Java Graphics.DrawString. But resulting image contains only rectangle instead of char.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
Graphics graphics = image.getGraphics();
Font font = new Font("Wingdings", Font.PLAIN, 20);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
graphics.drawString("\u00d8", 10, 10); // THREE-D TOP-LIGHTED RIGHTWARDS ARROWHEAD char
ImageIO.write(image, "PNG", new File(TEST_DATA_DIR + "bullet_char.png"));
How can I do this?
I dont think wingdings is one of the "standard" fonts
Font font = null;
try {
font=Font.createFont( Font.TRUETYPE_FONT,
new FileInputStream(new File("/pathto/WINGDINGS.TTF")) );
} catch(Exception e) {
System.out.println(e);
}
font=font.deriveFont(Font.PLAIN, 200);
graphics.setFont(font);
once you load the font (its always PLANE 1 pnt) you can the derive the style and size you want....
As similar questions has been answered here: Drawing exotic fonts in a java applet, you need to pass \uF0d8 instead of \u00d8.
graphics.drawString("\uF0d8", 10, 10);

Categories