Distorting Graphics....
I don't know really were to ask this, but I will do it here. Is there a certain algorithm or something to where I can distort graphics in a way that looks like a water ripple affect? I am trying to implement this so that I will have a background on a touch screen computer. And when I touch the screen to get it off the screensaver it will make a ripple affect and fade into the desktop. Any help would be thx.
Hi, i'd think the real effect would use displacment mapping, or similar. Might be pretty hard in straight java.
I had a quick hack using transparent circles. Looks alright and its fast, but not very real looking.
remember i said quick hack!
Harley.
import javax.swing.*;
import java.awt.*;
/**
*
* @author Harley Rana Bussell
*/
public class WaterTest extends JFrame implements Runnable
{
Image image;
Image backBuffer;
int[] rings =
{10,-90,-180,-270}; // 100 pixels spacing between rings
int speed = 8;
int delay = 20;
Thread thread;
Color[] colors = new Color[25];
Color outColor = new Color(0,0,0,30);
/** Creates a new instance of WaterTest */
public WaterTest()
{
image = loadImage("c:/test.jpg");
int alpha = 30; int delta = 10;
for(int i=0; i<colors.length; i++)
{
if(i >= colors.length/2)delta = -10;
colors[i] = new Color(255,255,255,alpha);
alpha += delta;
}
this.setSize(600,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.repaint();
thread = new Thread(this);
thread.start();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
new WaterTest();
}
public void update(Graphics g)
{
if(backBuffer==null)backBuffer = createImage(getWidth(),getHeight());
render((Graphics2D)backBuffer.getGraphics());
g.drawImage(backBuffer,0,0,null);
}
public void render(Graphics2D g)
{
g.setColor(Color.blue);
g.fillRect(0,0, getWidth(),getHeight());
g.drawImage(image,0,0,null);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(0.2f));
int centerX = getWidth()/2;int centerY = getHeight()/2;
for(int k=0; k<rings.length; k++)
{
int radius = rings[k];
if(radius >< 10)break;
for(int i=0; i<colors.length; i++)
{
g.setColor(colors[i]);
g.drawOval(centerX-radius-i,centerY-radius-i, radius+radius+i+i,radius+radius+i+i);
}
g.setColor(outColor);
g.drawOval(centerX-radius-colors.length,centerY-radius-colors.length, radius+radius+colors.length+colors.length,radius+radius+colors.length+colors.length);
}
}
private Image loadImage(String name)
{
MediaTracker tracker = new MediaTracker(this);
Image image = Toolkit.getDefaultToolkit().getImage(name);
tracker.addImage(image, 0);
try
{
tracker.waitForAll();
}catch(Exception e)
{}
return image;
}
public void run()
{
for(;;)
{
for(int k=0; k><rings.length; k++)
{
rings[k] += speed;
}
repaint();
try
{
thread.sleep(100);
}catch(InterruptedException e)
{}
}
}
}
>