How to copy image data into a subclass of BufferedImage?









up vote
2
down vote

favorite












I have a class called Bitmap which extends from BufferedImage,



public class Bitmap extends BufferedImage {...


One of its methods called copyImage, which copies contents from a source image into the class, it worked but this method doesn't maintain the original aspect ratio and dimensions of the source image.



public void copyImage(Image image) 
if (image != null)
BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




I want this method to copy the source image to the class with its original aspect ratio and dimensions maintained, I thought of resizing an width and height I modified the code above to this:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




But it didn't work, how can I modify the code above to copy the image?
Thanks in advance.










share|improve this question























  • @HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
    – user123456789
    Nov 11 at 2:34










  • @HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
    – user123456789
    Nov 11 at 2:47










  • Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
    – Hovercraft Full Of Eels
    Nov 11 at 2:52











  • @HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
    – user123456789
    Nov 11 at 2:58










  • Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
    – Hovercraft Full Of Eels
    Nov 11 at 3:01














up vote
2
down vote

favorite












I have a class called Bitmap which extends from BufferedImage,



public class Bitmap extends BufferedImage {...


One of its methods called copyImage, which copies contents from a source image into the class, it worked but this method doesn't maintain the original aspect ratio and dimensions of the source image.



public void copyImage(Image image) 
if (image != null)
BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




I want this method to copy the source image to the class with its original aspect ratio and dimensions maintained, I thought of resizing an width and height I modified the code above to this:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




But it didn't work, how can I modify the code above to copy the image?
Thanks in advance.










share|improve this question























  • @HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
    – user123456789
    Nov 11 at 2:34










  • @HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
    – user123456789
    Nov 11 at 2:47










  • Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
    – Hovercraft Full Of Eels
    Nov 11 at 2:52











  • @HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
    – user123456789
    Nov 11 at 2:58










  • Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
    – Hovercraft Full Of Eels
    Nov 11 at 3:01












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a class called Bitmap which extends from BufferedImage,



public class Bitmap extends BufferedImage {...


One of its methods called copyImage, which copies contents from a source image into the class, it worked but this method doesn't maintain the original aspect ratio and dimensions of the source image.



public void copyImage(Image image) 
if (image != null)
BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




I want this method to copy the source image to the class with its original aspect ratio and dimensions maintained, I thought of resizing an width and height I modified the code above to this:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




But it didn't work, how can I modify the code above to copy the image?
Thanks in advance.










share|improve this question















I have a class called Bitmap which extends from BufferedImage,



public class Bitmap extends BufferedImage {...


One of its methods called copyImage, which copies contents from a source image into the class, it worked but this method doesn't maintain the original aspect ratio and dimensions of the source image.



public void copyImage(Image image) 
if (image != null)
BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




I want this method to copy the source image to the class with its original aspect ratio and dimensions maintained, I thought of resizing an width and height I modified the code above to this:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




But it didn't work, how can I modify the code above to copy the image?
Thanks in advance.







java graphics copy bufferedimage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:01









Hovercraft Full Of Eels

260k20209316




260k20209316










asked Nov 11 at 2:11









user123456789

274




274











  • @HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
    – user123456789
    Nov 11 at 2:34










  • @HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
    – user123456789
    Nov 11 at 2:47










  • Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
    – Hovercraft Full Of Eels
    Nov 11 at 2:52











  • @HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
    – user123456789
    Nov 11 at 2:58










  • Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
    – Hovercraft Full Of Eels
    Nov 11 at 3:01
















  • @HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
    – user123456789
    Nov 11 at 2:34










  • @HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
    – user123456789
    Nov 11 at 2:47










  • Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
    – Hovercraft Full Of Eels
    Nov 11 at 2:52











  • @HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
    – user123456789
    Nov 11 at 2:58










  • Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
    – Hovercraft Full Of Eels
    Nov 11 at 3:01















@HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
– user123456789
Nov 11 at 2:34




@HovercraftFullOfEels I want to use BufferedImage for a pixel drawing panel for my project. I added few methods to BufferedImage like putPixel(Int x, int y, Color color) and getPixels(). It's for flexibility.
– user123456789
Nov 11 at 2:34












@HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
– user123456789
Nov 11 at 2:47




@HovercraftFullOfEels What does "copying an image" means anyway? Is drawing contents of the source image to the destination image a way to copy?
– user123456789
Nov 11 at 2:47












Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
– Hovercraft Full Of Eels
Nov 11 at 2:52





Also note that this BufferedImage bi = (BufferedImage) image; creates a new variable but not a new object as it simply references the parameter, and is a dangerous cast -- what if the image parameter isn't a BufferedImage but is some other type of Image? Again your code looks very confusing to me.
– Hovercraft Full Of Eels
Nov 11 at 2:52













@HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
– user123456789
Nov 11 at 2:58




@HovercraftFullOfEels You misunderstood, Graphics g = getGraphics(); not Graphics g = bi.getGraphics(); So I did not draw image to itself, I want to draw image to the class.
– user123456789
Nov 11 at 2:58












Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
– Hovercraft Full Of Eels
Nov 11 at 3:01




Ah, you are correct, and I am at fault. My apologies and thank you for clarifying -- so you're drawing the image in it's not maintaining its aspect ratio -- are you trying to resize it so that it fits into the current image?
– Hovercraft Full Of Eels
Nov 11 at 3:01












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This is in error:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




Your main problems are:



  1. You appear to be trying to change the intrinsic width and height of the original image, the this image, and you shouldn't do this, not this way

  2. You are assigning the parameter image's width to the this.height field with this.height = image.getWidth(null);

Other issues:



  • You're not conserving resources

  • You're making a dangerous and unnecessary cast

and it should be



public void copyImage(Image image) 
if (image != null)
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***

// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources




Test code using a MCVE showing proof of concept:



import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

public static void main(String args)
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;

try
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);

url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
System.exit(-1);


Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);







class MyImage extends BufferedImage 

public MyImage(int width, int height, int imageType)
super(width, height, imageType);


public void copyImage(Image image)
if (image != null)
int width = image.getWidth(null);

int height = image.getHeight(null); // *** Note change ***

Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources





If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.






share|improve this answer






















  • Nice answer but it didn't work, I did not get what I expected, have you tested your code?
    – user123456789
    Nov 11 at 4:16










  • @user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
    – Hovercraft Full Of Eels
    Nov 11 at 4:31










  • @user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
    – Hovercraft Full Of Eels
    Nov 11 at 4:42










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245254%2fhow-to-copy-image-data-into-a-subclass-of-bufferedimage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










This is in error:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




Your main problems are:



  1. You appear to be trying to change the intrinsic width and height of the original image, the this image, and you shouldn't do this, not this way

  2. You are assigning the parameter image's width to the this.height field with this.height = image.getWidth(null);

Other issues:



  • You're not conserving resources

  • You're making a dangerous and unnecessary cast

and it should be



public void copyImage(Image image) 
if (image != null)
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***

// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources




Test code using a MCVE showing proof of concept:



import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

public static void main(String args)
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;

try
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);

url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
System.exit(-1);


Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);







class MyImage extends BufferedImage 

public MyImage(int width, int height, int imageType)
super(width, height, imageType);


public void copyImage(Image image)
if (image != null)
int width = image.getWidth(null);

int height = image.getHeight(null); // *** Note change ***

Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources





If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.






share|improve this answer






















  • Nice answer but it didn't work, I did not get what I expected, have you tested your code?
    – user123456789
    Nov 11 at 4:16










  • @user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
    – Hovercraft Full Of Eels
    Nov 11 at 4:31










  • @user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
    – Hovercraft Full Of Eels
    Nov 11 at 4:42














up vote
1
down vote



accepted










This is in error:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




Your main problems are:



  1. You appear to be trying to change the intrinsic width and height of the original image, the this image, and you shouldn't do this, not this way

  2. You are assigning the parameter image's width to the this.height field with this.height = image.getWidth(null);

Other issues:



  • You're not conserving resources

  • You're making a dangerous and unnecessary cast

and it should be



public void copyImage(Image image) 
if (image != null)
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***

// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources




Test code using a MCVE showing proof of concept:



import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

public static void main(String args)
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;

try
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);

url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
System.exit(-1);


Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);







class MyImage extends BufferedImage 

public MyImage(int width, int height, int imageType)
super(width, height, imageType);


public void copyImage(Image image)
if (image != null)
int width = image.getWidth(null);

int height = image.getHeight(null); // *** Note change ***

Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources





If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.






share|improve this answer






















  • Nice answer but it didn't work, I did not get what I expected, have you tested your code?
    – user123456789
    Nov 11 at 4:16










  • @user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
    – Hovercraft Full Of Eels
    Nov 11 at 4:31










  • @user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
    – Hovercraft Full Of Eels
    Nov 11 at 4:42












up vote
1
down vote



accepted







up vote
1
down vote



accepted






This is in error:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




Your main problems are:



  1. You appear to be trying to change the intrinsic width and height of the original image, the this image, and you shouldn't do this, not this way

  2. You are assigning the parameter image's width to the this.height field with this.height = image.getWidth(null);

Other issues:



  • You're not conserving resources

  • You're making a dangerous and unnecessary cast

and it should be



public void copyImage(Image image) 
if (image != null)
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***

// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources




Test code using a MCVE showing proof of concept:



import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

public static void main(String args)
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;

try
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);

url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
System.exit(-1);


Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);







class MyImage extends BufferedImage 

public MyImage(int width, int height, int imageType)
super(width, height, imageType);


public void copyImage(Image image)
if (image != null)
int width = image.getWidth(null);

int height = image.getHeight(null); // *** Note change ***

Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources





If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.






share|improve this answer














This is in error:



public void copyImage(Image image) 
if (image != null)
this.width = image.getWidth(null);
this.height = image.getWidth(null);

BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();

g.drawImage(bi, 0, 0, width, height, null);




Your main problems are:



  1. You appear to be trying to change the intrinsic width and height of the original image, the this image, and you shouldn't do this, not this way

  2. You are assigning the parameter image's width to the this.height field with this.height = image.getWidth(null);

Other issues:



  • You're not conserving resources

  • You're making a dangerous and unnecessary cast

and it should be



public void copyImage(Image image) 
if (image != null)
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***

// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources




Test code using a MCVE showing proof of concept:



import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

public static void main(String args)
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;

try
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);

url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
System.exit(-1);


Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);







class MyImage extends BufferedImage 

public MyImage(int width, int height, int imageType)
super(width, height, imageType);


public void copyImage(Image image)
if (image != null)
int width = image.getWidth(null);

int height = image.getHeight(null); // *** Note change ***

Graphics g = getGraphics();

g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources





If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 4:43

























answered Nov 11 at 3:15









Hovercraft Full Of Eels

260k20209316




260k20209316











  • Nice answer but it didn't work, I did not get what I expected, have you tested your code?
    – user123456789
    Nov 11 at 4:16










  • @user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
    – Hovercraft Full Of Eels
    Nov 11 at 4:31










  • @user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
    – Hovercraft Full Of Eels
    Nov 11 at 4:42
















  • Nice answer but it didn't work, I did not get what I expected, have you tested your code?
    – user123456789
    Nov 11 at 4:16










  • @user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
    – Hovercraft Full Of Eels
    Nov 11 at 4:31










  • @user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
    – Hovercraft Full Of Eels
    Nov 11 at 4:42















Nice answer but it didn't work, I did not get what I expected, have you tested your code?
– user123456789
Nov 11 at 4:16




Nice answer but it didn't work, I did not get what I expected, have you tested your code?
– user123456789
Nov 11 at 4:16












@user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
– Hovercraft Full Of Eels
Nov 11 at 4:31




@user123456789: testing it now with my code, but I can't with yours, not without a Minimal, Complete, and Verifiable example post. Hang on...
– Hovercraft Full Of Eels
Nov 11 at 4:31












@user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
– Hovercraft Full Of Eels
Nov 11 at 4:42




@user123456789: OK, yes, now I've tested my code for proof of concept in a valid Minimal, Complete, and Verifiable example, and have proven that it works. To prove it to yourself, copy my classes into your IDE and run them. They can all be copied into a single file called TestImage.java. You will see 3 JOptionPanes, the first showing the original myImage that is blank, then after 2 images have been copied into it.
– Hovercraft Full Of Eels
Nov 11 at 4:42

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245254%2fhow-to-copy-image-data-into-a-subclass-of-bufferedimage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto