Re: BufferedImage
Rita Erfurt wrote:
My problem is the correct update of the screen. There is an object of class
BufferedImage. In this object offscreen everything is drawn, what is on the
screen. It is a png-Image with rectangles, lines, cycles and other little
Images on it. The little Images move persistently on the screen. During the
mark-process I can draw a new square, but I need to remove this as soon as
the mouse moves to a new position on the screen. How can I realise it? Or
how can I make it better? Has anybody an idea?
private BufferedImage offscreen;
....
Graphics2D graphic=offscreen.createGraphics();
Rectangle rect= new
Rectangle(startp.x,startp.y,endp.x-startp.x,endp.y-startp.y);
graphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.2f));
graphic.setColor(Color.CYAN);
graphic.fill(rect);
paint(graphic, rect); //draws the rect-part of offscreen with graphic on it
graphic.dispose();
Which method contains these lines?
In general I prefer to paint directly in Swing because of it's support
for double buffering. But if you really want to use an offscreen image
here's some test code:
public class Test extends JComponent {
private Image background;
private BufferedImage offscreen;
private Rectangle selection;
private MouseAdapter selectionHandler = new MouseAdapter() {
private Point p;
public void mousePressed(MouseEvent e) {
p = e.getPoint();
setSelection(new Rectangle(p.x, p.y, 0, 0));
}
public void mouseDragged(MouseEvent e) {
Point dp = e.getPoint();
int x1 = Math.min(p.x, dp.x);
int y1 = Math.min(p.y, dp.y);
int x2 = Math.max(p.x, dp.x);
int y2 = Math.max(p.y, dp.y);
setSelection(new Rectangle(x1, y1, x2 - x1 + 1,
y2 - y1 + 1));
}
};
public Test(Image img) {
setBackground(img);
addMouseListener(selectionHandler);
addMouseMotionListener(selectionHandler);
}
public Dimension getPreferredSize() {
return new Dimension(offscreen.getWidth(),
offscreen.getHeight());
}
public void setSelection(Rectangle r) {
selection = r;
repaintOffscreen();
}
public void setBackground(Image img) {
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
try {
t.waitForAll();
} catch (InterruptedException ex) { }
background = img;
offscreen = new BufferedImage(img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
repaintOffscreen();
}
private void repaintOffscreen() {
Graphics2D g2 = offscreen.createGraphics();
g2.drawImage(background, 0, 0, null);
if ( selection != null ) {
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.2f));
g2.setColor(Color.CYAN);
g2.fill(selection);
}
g2.dispose();
repaint();
}
protected void paintComponent(Graphics g) {
g.drawImage(offscreen, 0, 0, null);
}
public static final void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BufferedImage img = new BufferedImage(600, 400,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.createGraphics();
g.setColor(Color.RED);
g.drawLine(0, 0, 600, 400);
g.drawLine(0, 400, 600, 0);
g.dispose();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new Test(img));
frame.pack();
frame.setVisible(true);
}
});
}
}
Viele Gr??e
Auch sch?nen Gru?
Bye
Michael