I created a class called VainillaImagen:
public VainillaImage(String url){
this.icimg=new ImageIcon(url);
this.imagen=new JLabel(this.icimg);
this.imagen.setVisible(true);
}
and then I created a methos called setDimensions that use another method called resizeVainillaImg. But the resizeVainillaImg method dont work any ideas why?
public void setDimensions(boolean wRel,int width,boolean hRel,int height){
Dimension dimPantalla = Toolkit.getDefaultToolkit().getScreenSize();
int nwidth,nheight;
if(wRel){
nwidth=(int)(width*(dimPantalla.width));
}else{
nwidth=width;
}
if(hRel){
nheight=(int)(height*(dimPantalla.height));
}else{
nheight=height;
}
resizeVainillaImg(nwidth,nheight);
}
public void resizeVainillaImg(int newWidth,int newHeight){
Image img = this.icimg.getImage();
BufferedImage bi = new BufferedImage(newWidth,newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newWidth, newHeight,null);
g.dispose();
this.icimg = new ImageIcon(bi);
this.imagen.setIcon(this.icimg);
}
Although I didn't understand the setDimensions(), but I think you are trying to fit your image into screen width and height.
By multiplying int values of width and height in setDimensions(), you will simply be able to multiply small int numbers. For bigger numbers you will run out of memory because of huge image size (widthscreenwidth , heightscreenheight).
Lets assume you want to resize your image to percent of your screen, or use the default height and with of the image. Using the code below, pass negative number (-1 for example) to ignore the screen size, and 0> number to resize it to percent of screen.
I hope this help. However, it you have some other think in your mind, just remember to use float because of int * int multiplications :)
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
/**
*
* #author Pasban
*/
public class VainillaImage {
private ImageIcon icimg;
private JLabel imagen;
public static void main(String args[]) {
JDialog d = new JDialog();
VainillaImage v = new VainillaImage("92-1024x576.jpg");
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.getContentPane().add(v.imagen);
v.setDimensions(-1, 1);
d.pack();
d.setLocationRelativeTo(null);
d.setVisible(true);
}
public void setDimensions(double width, double height) {
Dimension dimPantalla = Toolkit.getDefaultToolkit().getScreenSize();
int nwidth, nheight;
nwidth = (int) (width * (dimPantalla.width));
nheight = (int) (height * (dimPantalla.height));
resizeVainillaImg(nwidth, nheight);
}
public void resizeVainillaImg(int newWidth, int newHeight) {
Image img = this.icimg.getImage();
newWidth = Math.max(newWidth, img.getHeight(null));
newWidth = Math.max(newHeight, img.getHeight(null));
BufferedImage bi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newWidth, newHeight, null);
g.dispose();
this.icimg = new ImageIcon(bi);
this.imagen.setIcon(this.icimg);
}
public VainillaImage(String url) {
this.icimg = new ImageIcon(url);
this.imagen = new JLabel(this.icimg);
this.imagen.setVisible(true);
}
}
If you are trying to dynamically resize an Icon based on the space available to the label then check out Darryl's Stretch Icon.
Related
i am making simple image editor in java. My problem is that when i rotate image its being cropped, i will try to show it on images:
before rotation
http://i.imgur.com/KjtHslb.jpg
after 45 degre
http://i.imgur.com/SKOEFP7.jpg
after 90 degrees rotation
i[dot]imgur[dot]com/4NrABIA.jpg
It looks like JPanel crops rotated image into size of original image, i dont know why :/ so i am asking for help. Thank you.
Heres my code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import static java.awt.image.ImageObserver.WIDTH;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class OknoInterfejsu extends JFrame{
Container content;
BufferedImage image = null;
JLabel picLabel;
Image scaledImage;
private final JButton przyciski[];
private JComboBox wybor;
private String wybor_str[];
private int picHeight;
private int picWidth;
private int realHeight;
double picAspect;
private String path;
private class ObslugaPrzycisku implements ActionListener{
private final JFrame ref_okno;
ObslugaPrzycisku(JFrame okno){
ref_okno = okno;
}
public void actionPerformed(ActionEvent e) {
JButton bt = (JButton)e.getSource();
if(bt==przyciski[0]){
//-----------WCZYTAJ PRZYCISK-------------
{
JFileChooser fileChooser = new JFileChooser("C:\\Users\\Filip\\Pictures\\");
FileFilter imageFilter = new FileNameExtensionFilter(
"Image files", ImageIO.getReaderFileSuffixes());
fileChooser.setFileFilter(imageFilter);
if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
File plik = fileChooser.getSelectedFile();
path = plik.getPath();
System.out.print(path);
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(OknoInterfejsu.class.getName()).log(Level.SEVERE, null, ex);
}
picHeight = image.getHeight();
picWidth = image.getWidth();
picAspect = (double) picHeight / picWidth;
//if(picAspect>1)realHeight = 400;
//else realHeight = 800;
realHeight = 400;
scaledImage = image.getScaledInstance(realHeight, (int) (realHeight*picAspect),Image.SCALE_SMOOTH);
picLabel = new JLabel(new ImageIcon(scaledImage));
content.add(picLabel);
content.revalidate();
content.repaint();
}
}
}
//--------------------------OBROT +------------
else if(bt==przyciski[1]){
picLabel.setIcon(null);
int temp1 = (int) (400*picAspect);
picRotate(scaledImage, 400,temp1);
picLabel = new JLabel(new ImageIcon(scaledImage));
content.add(picLabel);
content.revalidate();
content.repaint();
}
}
}
public OknoInterfejsu()
{
super("Okno interfejsu");
setSize(1000,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//-------------------------------------------------------
przyciski = new JButton[2];
przyciski[0] = new JButton("Wczytaj Obraz");
przyciski[0].addActionListener(new ObslugaPrzycisku(this));
przyciski[1] = new JButton("ObrĂ³t +");
przyciski[1].addActionListener(new ObslugaPrzycisku(this));
JPanel panelPrzyciski = new JPanel(new FlowLayout());
panelPrzyciski.add(przyciski[0]);
panelPrzyciski.add(przyciski[1]);
//-------------------------------------------------------
content = getContentPane();
content.setLayout(new BorderLayout());
content.add(panelPrzyciski,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String args[]){
new OknoInterfejsu();
}
public void picRotate(Image image,int w,int h){
BufferedImage temp=(BufferedImage)createImage(w,h);
Graphics2D g2d=(Graphics2D)temp.createGraphics();
g2d.rotate((Math.PI/2)*22.5, w/2, h/2);
g2d.drawImage(image,0,0,null);
this.scaledImage=temp;
g2d.dispose();
}
}
When you rotate an image, it changes size, you need to create a new image whose bounds encompass this new size, for example
public Image rotateBy(Image image, double degrees) {
// The size of the original image
int w = source.getWidth();
int h = source.getHeight();
// The angel of the rotation in radians
double rads = Math.toRadians(degrees);
// Some nice math which demonstrates I have no idea what I'm talking about
// Okay, this calculates the amount of space the image will need in
// order not be clipped when it's rotated
double sin = Math.abs(Math.sin(rads));
double cos = Math.abs(Math.cos(rads));
int newWidth = (int) Math.floor(w * cos + h * sin);
int newHeight = (int) Math.floor(h * cos + w * sin);
// A new image, into which the original can be painted
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
// The transformation which will be used to actually rotate the image
// The translation, actually makes sure that the image is positioned onto
// the viewable area of the image
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);
// And we rotate about the center of the image...
int x = w / 2;
int y = h / 2;
at.rotate(rads, x, y);
g2d.setTransform(at);
// And we paint the original image onto the new image
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return rotated;
}
Then you just need to call it...
picLabel.setIcon(new ImageIcon(rotateBy(scaledImage, 22.5)));
Because setIcon is a bound property, it should trigger a layout and paint pass automatically, if not, you can simply call revalidate and repaint to trigger them manually
I see you're using picLabel to store the image and display it:
picRotate(scaledImage, 400,temp1);
picLabel = new JLabel(new ImageIcon(scaledImage));
content.add(picLabel);
content.revalidate();
content.repaint();
For some reason label size is the Image height and width but label doesn't know the image was rotated. A work around this might be to set the size of the label prior to adding the image to it, and have a way to guaranteed that the size will accommodate the whole image even if is rotated:
picRotate(scaledImage, 400,temp1);
picLabel = new JLabel();
picLabel.setPreferredSize(new Dimension(800, 800));
picLabel.add(new ImageIcon(scaledImage))
content.add(picLabel);
content.revalidate();
content.repaint();
Now picLabel width and height in the preferred size has to be calculated based on the space needed by the rotated image. I thing some trigonometric functions might help for this. I'm assuming 800x800 is enough space to accommodate the image
I want to rotate a bufferedImage. The code I use makes it possible that the image rotates. But it cuts the image to a square. The screen shows then a black "border" on the left and the right side.
If I use debugging tool, the image width is about the whole width included the black "border". But the black "border" don't rotate, it's always at the left and the right side. And the image is missing the picture-parts left and right. The squared-image isn't cut again if I rotate it again. If I change the src.getWidth()-parts the image will be smaller with each rotation.
private static BufferedImage rotateImage(BufferedImage src, double degrees) {
AffineTransform affineTransform = AffineTransform.getRotateInstance(
Math.toRadians(degrees), (src.getWidth() / 2), (src.getHeight() / 2));
BufferedImage rotatedImage = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
Graphics2D g = (Graphics2D) rotatedImage.getGraphics();
g.setTransform(affineTransform);
g.drawImage(src, 0, 0, null);
return rotatedImage;
}
public void rotateImage(int degree) {
if (this.image != null) {
this.setImage(myJComponent.rotateImage(this.image, degree));
}
}
The size of the image will change as you rotate it.
Here is some code to play with:
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class Rotation
{
BufferedImage image;
JLabel label;
public Rotation(BufferedImage image)
{
this.image = image;
}
private BufferedImage getImage(double theta)
{
// Determine the size of the rotated image
double cos = Math.abs(Math.cos(theta));
double sin = Math.abs(Math.sin(theta));
double width = image.getWidth();
double height = image.getHeight();
int w = (int)(width * cos + height * sin);
int h = (int)(width * sin + height * cos);
// Rotate and paint the original image onto a BufferedImage
BufferedImage out = new BufferedImage(w, h, image.getType());
Graphics2D g2 = out.createGraphics();
g2.setPaint(UIManager.getColor("Panel.background"));
g2.fillRect(0,0,w,h);
double x = w/2;
double y = h/2;
AffineTransform at = AffineTransform.getRotateInstance(theta, x, y);
x = (w - width)/2;
y = (h - height)/2;
at.translate(x, y);
g2.drawRenderedImage(image, at);
g2.dispose();
return out;
}
private JLabel getLabel()
{
ImageIcon icon = new ImageIcon(image);
label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}
private JSlider getSlider()
{
final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
BufferedImage bi = getImage(Math.toRadians(value));
label.setIcon(new ImageIcon(bi));
}
});
return slider;
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
String path = "mong.jpg";
ClassLoader cl = Rotation.class.getClassLoader();
BufferedImage bi = ImageIO.read(cl.getResourceAsStream(path));
Rotation r = new Rotation(bi);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(r.getLabel()));
f.getContentPane().add(r.getSlider(), "South");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
catch(IOException e)
{
System.out.println(e);
}
}
});
}
}
Just change the image you want to read and use the slider to rotate the image.
I am rotating and scaling the image using AffineTransform. When I display the image using Graphics2D.drawImage() the whole image does not get displayed so I am calling the AffineTransform.translate method.
Here is the code I have currently written:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Dimension dim = getPreferredSize();
int x = (getWidth() - image.getWidth(null)) / 2;
int y = (getHeight() - image.getHeight(null)) / 2;
double angle = Math.toRadians(rotateAngle);
AffineTransform identity = new AffineTransform();
identity.scale(scale, scale);
identity.translate(x,y);
AffineTransform at = new AffineTransform();
setPreferredSize(new Dimension((int)(identity.getTranslateX()+(image.getWidth(null)*scale)),(int)(identity.getTranslateY()+(image.getHeight(null)*scale))));
at.setTransform(identity);
at.rotate(angle);
g2d.transform(at);
g2d.drawImage(image, 0, 0, this);
g2d.dispose();
}
The translate method sometimes displays the whole image and sometimes does not depending on the image size and rotating angle. Is there anyway to make sure the whole image gets displayed after a rotate.
I had a look at this previous asked question:
Java2D Image Rotation Issue
but the solution posted there gave the same problem for me.
I finally figured out a way to do this for 90 degrees, it will not work for other degrees.
void rotate90(){
if(image!=null){
int w = image.getWidth(null); //the Width of the original image
int h = image.getHeight(null);//the Height of the original image
if(w>h){
BufferedImage dimg = new BufferedImage(w, w, BufferedImage.TYPE_3BYTE_BGR );
Graphics2D g = dimg.createGraphics();
g.translate(-(w-h), 0);
g.rotate(Math.toRadians(90), w/2, w/2);
g.drawImage(image, 0, 0,null);
BufferedImage bimg = new BufferedImage(h, w, BufferedImage.TYPE_3BYTE_BGR );
Graphics2D g2 = (Graphics2D)bimg.getGraphics();
g2.drawImage(dimg,0,0,h,w,0,0,h,w,null);
dimg.flush();
dimg=null;
image = createImage(bimg.getSource());
bimg.flush();
bimg= null;
}
else{
BufferedImage dimg = new BufferedImage(h, h, BufferedImage.TYPE_3BYTE_BGR );
Graphics2D g = dimg.createGraphics();
g.translate(-(w-h), 0);
g.rotate(Math.toRadians(90), w/2, w/2);
g.drawImage(image, 0, 0,null);
BufferedImage bimg = new BufferedImage(h, w, BufferedImage.TYPE_3BYTE_BGR );
Graphics2D g2 = (Graphics2D)bimg.getGraphics();
g2.drawImage(dimg,0,0,h,w,0,0,h,w,null);
dimg.flush();
dimg=null;
image = createImage(bimg.getSource());
bimg.flush();
bimg= null;
}
}
}
void rotateClockWise(){
if(image!=null){
rotate90();
setPanelsize();
revalidate();
repaint();
}
}
sometimes displays the whole image and sometimes does not depending on the image size and rotating angle
You need to recalculate the size of the rotated image. Here is an example of how to do that:
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class Rotation
{
BufferedImage image;
JLabel label;
public Rotation(BufferedImage image)
{
this.image = image;
}
private BufferedImage getImage(double theta)
{
// Determine the size of the rotated image
double cos = Math.abs(Math.cos(theta));
double sin = Math.abs(Math.sin(theta));
double width = image.getWidth();
double height = image.getHeight();
int w = (int)(width * cos + height * sin);
int h = (int)(width * sin + height * cos);
// Rotate and paint the original image onto a BufferedImage
BufferedImage out = new BufferedImage(w, h, image.getType());
Graphics2D g2 = out.createGraphics();
g2.setPaint(UIManager.getColor("Panel.background"));
g2.fillRect(0,0,w,h);
double x = w/2;
double y = h/2;
AffineTransform at = AffineTransform.getRotateInstance(theta, x, y);
x = (w - width)/2;
y = (h - height)/2;
at.translate(x, y);
g2.drawRenderedImage(image, at);
g2.dispose();
return out;
}
private JLabel getLabel()
{
ImageIcon icon = new ImageIcon(image);
label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}
private JSlider getSlider()
{
final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
BufferedImage bi = getImage(Math.toRadians(value));
label.setIcon(new ImageIcon(bi));
}
});
return slider;
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
String path = "mong.jpg";
ClassLoader cl = Rotation.class.getClassLoader();
BufferedImage bi = ImageIO.read(cl.getResourceAsStream(path));
Rotation r = new Rotation(bi);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(r.getLabel()));
f.getContentPane().add(r.getSlider(), "South");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
catch(IOException e)
{
System.out.println(e);
}
}
});
}
}
Not sure how the scaling will affect this. Maybe you can just multiply the width/height by the scaling factor?
Alright so I'm making a game, and I'm trying to modify the original hit marker image by adding text on it, and I'm using the following code:
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
public class HitMarker {
public static final Image rangeHitMarker = new ImageIcon(HitMarker.class.getResource("rangeHitMarker.png")).getImage();
public static final Image magicHitMarker = new ImageIcon(HitMarker.class.getResource("magicHitMarker.png")).getImage();
public static final Image monsterHitMarker = new ImageIcon(HitMarker.class.getResource("monsterHitMarker.png")).getImage();
public static final Font font = new Font("Tahoma", Font.PLAIN, 10);
public static final Color t = new Color(0,0,0,0);
public Image hitMarker;
public BufferedImage image;
public String hit;
public int attackStyle;
public boolean rangeAttack;
public int x;
public int y;
public Timer timer;
public boolean remove;
public HitMarker(int x, int y, int hit, int attackStyle){
this.hit = String.format("%d", hit);
this.remove = false;
this.x = x;
this.y = y;
this.attackStyle = attackStyle;
this.hitMarker = getImage();
BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(hitMarker, 0, 0, null);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(this.hit, 18, 13);
g.dispose();
image = bi;
timer = new Timer(800,
new ActionListener(){
public void actionPerformed(ActionEvent e){
remove = true;
timer.stop();
}
}
);
timer.setInitialDelay(800);
timer.start();
}
public HitMarker(int x, int y, int hit){
this.hit = String.format("%d", hit);
this.remove = false;
this.x = x;
this.y = y;
this.hitMarker = monsterHitMarker;
BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(hitMarker, 0, 0, null);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(this.hit, 18, 13);
g.dispose();
image = bi;
timer = new Timer(800,
new ActionListener(){
public void actionPerformed(ActionEvent e){
remove = true;
timer.stop();
}
}
);
timer.setInitialDelay(800);
timer.start();
}
public boolean isRangeAttack(){
return attackStyle == AttackStyleConstants.RANGE || attackStyle == AttackStyleConstants.RANGE_DEFENCE ? true : false;
}
public Image getImage(){
return isRangeAttack() ? rangeHitMarker : magicHitMarker;
}
}
Focusing particularly on either constructor:
And the error that I'm having is that when I create the BufferedImage and draw the image on the buffered image, it's creating a black background automatically and I don't know why. I've tried researching on this topic and some say to change something about the AlphaComposite and the g.clearRect() method, but neither of those seem to work. By the way, the image that I'm painting on the buffered image is 35x20 (which is the dimensions of the buffered image) and it has a transparent background. If anyone can tell me how to remove this black background, it would be very much appreciated, thank you.
Try BufferedImage.TYPE_INT_ARGB. This will make the regions transparent instead of black.
You might want to try and store the alpha channel as well,
BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_ARGB);
If you need a JPG with white background, you need to draw the image like this:
g.drawImage(hitMarker, 0, 0, Color.WHITE, null);
This way you avoid the black background when going from PNG to JPG.
Use png instead of jpeg. Png is much suitable for transparency operations.
Here is simple png export code snippet;
BufferedImage bImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bImage.getGraphics();
DrawingContext context = new DrawingContext(g2d);
plot.draw(context);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png");
wr.write(plot, baos, 640, 480);
baos.flush();
baos.close();
InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
BufferedImage bufferedImage = ImageIO.read(inputStream);
ImageIO.write(bufferedImage,"png",new File(outputFolder.getPath()+"/result.png"));
I have checked similarly named questions, but they don't answer this use case.
Basically, I was to overlay some text (text) at a given coordinate (x,y) I have the below function in a package;
protected BufferedImage Process2(BufferedImage image){
Graphics2D gO = image.createGraphics();
gO.setColor(Color.red);
gO.setFont(new Font( "SansSerif", Font.BOLD, 12 ));
gO.drawString(this.text, this.x, this.y);
System.err.println(this.text+this.x+this.y);
return image;
}
I feel like im missing something patently obvious; every reference to Graphics2D I can find is dealing with either games or writing directly to a file but I just want a BufferedImage returned. with the overlay 'rendered'
In the current code, the image appears out the end unchanged.
Thanks!
The method drawString() uses x and y for the leftmost character's baseline. Numbers typically have no descenders; if the same is true of text, a string drawn at position (0,0) will be rendered entirely outside the image. See this example.
Addendum: You may be having trouble with an incompatible color model in your image. One simple expedient is to render the image and then modify it in situ.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* #see https://stackoverflow.com/questions/2658663
*/
public class TextOverlay extends JPanel {
private BufferedImage image;
public TextOverlay() {
try {
image = ImageIO.read(new URL(
"http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"));
} catch (IOException e) {
e.printStackTrace();
}
image = process(image);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
private BufferedImage process(BufferedImage old) {
int w = old.getWidth() / 3;
int h = old.getHeight() / 3;
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, this);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
String s = "Hello, world!";
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(s) - 5;
int y = fm.getHeight();
g2d.drawString(s, x, y);
g2d.dispose();
return img;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TextOverlay());
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
create();
}
});
}
}