Re: BufferedImage
Rita Erfurt wrote:
Hallo,
can somebody help me to solve the following problem, please?
I want to mark a part of an image with a blue transparent rectangle.
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();
Viele Gr??e
Rita
There are a couple of ways to do this but the important part is using a
MouseMotionListener (or something similar) to find out where the mouse
is and then draw your transparent rectangle. In the example below, I
took an already existing graphics test program, added the
MouseMotionListener and a couple of lines of code in the
paintComponent() to draw the rectangle. This program, draws to a
BufferedImage and then draws it on the component, followed by the
translucent rectangle.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.reflect.*;
import javax.swing.*;
public class test3 extends JPanel implements Runnable {
volatile BufferedImage bi;
// volatile VolatileImage bi;
volatile long then;
long now,time;
Thread thread;
double angle;
int n;
double rate;
int x=-100,y=-100;
public test3() {
super(false);
setPreferredSize(new Dimension(400,300));
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
GraphicsConfiguration gc = getGraphicsConfiguration();
bi = gc.createCompatibleImage(getWidth(),getHeight());
// bi =
gc.createCompatibleVolatileImage(getWidth(),getHeight());
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
}
public void start() {
then = System.nanoTime();
thread = new Thread(this);
thread.start();
}
public void stop() {
thread.interrupt();
}
public void run() {
try {
long now = 0;
long then = System.nanoTime();
while (true) {
render();
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
paintImmediately(getBounds());
}
});
} catch (InvocationTargetException ite) {
System.out.println(ite);
}
while (now < then + 10000000)
now = System.nanoTime();
then = now;
}
} catch (InterruptedException ie) {
System.out.println(ie);
}
}
public void render() {
int w = getWidth();
int h = getHeight();
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
g.setColor(Color.RED);
g.drawString(String.format("%5.1f",rate),10,12);
angle += 0.001;
g.rotate(angle,w/2,h/2);
g.setColor(Color.BLUE);
g.fillRect(w/2 - 100,h/2 - 100,200,200);
if (++n % 100 == 0) {
now = System.nanoTime();
time = now - then;
then = now;
rate = 100000000000.0 / time;
}
g.dispose();
}
public void paintComponent(Graphics g) {
g.drawImage(bi,0,0,null);
g.setColor(new Color(128,0,0,80));
g.fillRect(x-20,y-20,40,40);
}
public static void main(String[] args) {
final test3 t3 = new test3();
final JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
t3.start();
}
public void windowClosing(WindowEvent we) {
t3.stop();
f.dispose();
}
});
f.add(t3,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access