This discussion is archived
6 Replies Latest reply: Apr 26, 2012 3:31 PM by morgalr RSS

About BufferedImage to Graphics2D?

907153 Newbie
Currently Being Moderated
Hi,there
I have a question about BufferedImage zooming in a jpanel. But another Graphics2D object created from BufferedImage, How can it get this new context?

public class myPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage backImg = ...
setRect(imgRect); //zoom backImg size into imgRect
g.drawImage(backImg, imgRect.x, imgRect.y, imgRect.width, imgRect.height, this);//draw on jpanel
}
when I change the display of image, the real size of backimg not changed. so
Graphics g2D = image.createGraphics();
g2D is not the useable object, right? How can I transform g2D to fit new region.
  • 1. Re: About BufferedImage to Graphics2D?
    StanislavL Pro
    Currently Being Moderated
    You can use the method of Image to create scaled image instance
    public Image getScaledInstance(int width, int height, int hints)
  • 2. Re: About BufferedImage to Graphics2D?
    907153 Newbie
    Currently Being Moderated
    thanks. Now if I don't want change the Image, just display on JPanel's new rectangle(not equals to the size of image).Could I get a new Graphics2D object from this context? I can use it draw Line on Image, it should be a scaled pen line-width.

    Edited by: 904150 on 2012-4-18 下午5:17

    Edited by: 904150 on 2012-4-18 下午6:45
  • 3. Re: About BufferedImage to Graphics2D?
    morgalr Explorer
    Currently Being Moderated
    From your code snippet you show a paintComponent(Graphics g) for your JPanel, that g is the graphics context that you want to work with isn't it?

    Anything you do to g, will not be reflected in your image object, but rather, it is only being done to the "display area" on your JPanel.
  • 4. Re: About BufferedImage to Graphics2D?
    907153 Newbie
    Currently Being Moderated
    thanks, I need to switch on two Graphics objects. anyway it's not a perfect answer.
  • 5. Re: About BufferedImage to Graphics2D?
    morgalr Explorer
    Currently Being Moderated
    I cannot really decipher what it is that you are trying to do from your descriptions:

    an Image can be scaled by the drawImage method of the target Graphics Context, when you draw onto a Graphics Context, only the object who's Graphics Context you are writing to has anything changed--the Image you drew onto the GC did not change. You can drawImage onto the GC of another Image object, thereby changing the displayable image of that object.
      Graphics2D g = myImage.createGraphics();
      //in both of the following lines myOtherImage remains unchanged in content or size
      g.drawImage(myOtherImage, 0, 0, null); //does not change the size or either image
      g.drawImage)myOtherImage, 0, 0, myImage.getWidth()-1, myImage.getHeight()-1); //scales myOtherImage to fit the size of myImage
    If that does not help, you're going to have to give a much better description of what you need before you can be helped.
  • 6. Re: About BufferedImage to Graphics2D?
    morgalr Explorer
    Currently Being Moderated
    904150 wrote:
    thanks. Now if I don't want change the Image, just display on JPanel's new rectangle(not equals to the size of image).Could I get a new Graphics2D object from this context? I can use it draw Line on Image, it should be a scaled pen line-width.
    Everything viewable in Java has a graphics context.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points