How to get (a part of) Graphics2D as an Image?

I have drawn something on a Graphics2D object. On another Graphics2D object, I want to draw just a part of the first Graphics2D object (subImage). To be able to call getSubimage() I need a BufferedImage. So how can I "convert" a Graphics2D to a BufferedImage?
[266 byte] By [MartinHilperta] at [2007-11-26 13:25:47]
# 1
I dont get what youre asking. You dont draw on Graphics objects, you draw with Graphics objects.
CaptainMorgan08a at 2007-7-7 19:54:58 > top of Java-index,Security,Cryptography...
# 2

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import javax.swing.*;

public class SubTest {

JTabbedPane tabbedPane;

DemoGraphicPanel dgPanel;

private void demonstrate(final Rectangle r) {

tabbedPane.addTab("full image", new JPanel() {

BufferedImage image = makeImage();

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, 0, 0, this);

}

});

tabbedPane.addTab("clipped image", new JPanel() {

BufferedImage image = makeClippedImage(r);

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, r.x, r.y, this);

}

});

tabbedPane.addTab("clipped draw", new JPanel() {

BufferedImage image = makeImage();

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setClip(r);

g.drawImage(image, 0, 0, this);

//g.setColor(Color.blue);

//((Graphics2D)g).draw(r);

}

});

tabbedPane.addTab("subImage", new JPanel() {

BufferedImage image = makeImage().getSubimage(r.x, r.y,

r.width, r.height);

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, r.x, r.y, this);

}

});

// Resize to accomodate multiple rows of tabs.

//((Window)tabbedPane.getTopLevelAncestor()).pack();

SwingUtilities.windowForComponent(tabbedPane).pack();

}

private BufferedImage makeImage() {

int w = dgPanel.getWidth();

int h = dgPanel.getHeight();

int type = BufferedImage.TYPE_INT_RGB;

BufferedImage image = new BufferedImage(w, h, type);

Graphics2D g2 = image.createGraphics();

dgPanel.paint(g2);

g2.dispose();

return image;

}

private BufferedImage makeClippedImage(Rectangle r) {

int w = r.width;

int h = r.height;

int type = BufferedImage.TYPE_INT_RGB;

BufferedImage image = new BufferedImage(w, h, type);

Graphics2D g2 = image.createGraphics();

g2.translate(-r.x, -r.y);

dgPanel.paint(g2);

g2.dispose();

return image;

}

private JTabbedPane getContent(Rectangle r) {

dgPanel = new DemoGraphicPanel(r);

tabbedPane = new JTabbedPane();

tabbedPane.addTab("original", dgPanel);

return tabbedPane;

}

public static void main(String[] args) {

final Rectangle clip = new Rectangle(100, 100, 150, 150);

final SubTest test = new SubTest();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(clip));

f.pack();

f.setLocation(200,200);

f.setVisible(true);

// All events in single-file queue.

EventQueue.invokeLater(new Runnable() {

public void run() {

test.demonstrate(clip);

}

});

}

}

class DemoGraphicPanel extends JPanel {

Rectangle r;

Dimension size = new Dimension(400,400);

DemoGraphicPanel(Rectangle r) {

this.r = r;

setBackground(new Color(240, 220, 220));

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int w = size.width;

int h = size.height;

g.setColor(Color.red);

g.drawLine(0,0,w,h);

g.setColor(Color.blue);

g.drawLine(0,h,w,0);

g.setColor(Color.red);

g.drawRect(r.x, r.y, r.width-1, r.height-1);

}

public Dimension getPreferredSize() {

return size;

}

}

crwooda at 2007-7-7 19:54:58 > top of Java-index,Security,Cryptography...