RedWulf
03-11-04, 08:34 PM
/*Ok, I'm new here but I hope you guys could help me out... I'm trying to take
*an image(dragon.jpg, it's in the same directory as ImageTest.java) and put in
*on a buffer while scaling it down on the x-axis. Eventually I hope to scale
*it down to 0, mirror it, and scale it back up at the same rate so it looks
*as if it is rotating. As of now, all it does is show up at the same size.
*I tried doinga for loop and all but it just doesn't seem to work for me.
*Any help would be appreciated, thanks.
*/
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class ImageTest extends JPanel implements Runnable, ActionListener
{
public static final long FRAME_INTERVAL = 1;
public static final int X_INC = 2;
public static final int Y_INC = X_INC;
private Thread animationThread;
private BufferedImage image;
ImageIcon icon = new ImageIcon("dragon.jpg");
Image dragon = icon.getImage();
private int xLoc, yLoc, xDirec, yDirec, width, height;
private boolean keepGoing;
public ImageTest(int w, int h)
{
keepGoing = true;
image = new BufferedImage(900,900,BufferedImage.TYPE_INT_RGB);
width = w;
height = h;
xLoc = width;
yLoc = height;
xDirec = 1;
yDirec = 1;
setLayout(null);
}
public void paintComponent(Graphics g)
{
double scalex;
double scaley = 1;
Graphics2D g2d = (Graphics2D) g;
AffineTransform tx = new AffineTransform();
for(int c = 0; c < 101; c++)
{
scalex = (1 - (c/100));
tx.scale(scalex, scaley);
g2d.setTransform(tx);
g2d.drawImage(image, 0, 0, null);
}
}
public void update(Graphics g)
{
paintComponent(g);
}
public void updateImage()
{
Graphics2D gr = image.createGraphics();
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
gr.clearRect(0,0,image.getWidth(), image.getHeight());
gr.drawImage(dragon, 0, 0, null);
gr.dispose();
int x = 0;
int y = 0;
}
public void run()
{
while(keepGoing)
{
updateImage();
repaint();
try
{
Thread.sleep(FRAME_INTERVAL);
}
catch(InterruptedException exc){}
}
}
public void start()
{
keepGoing = true;
animationThread = new Thread(this);
animationThread.start();
}
public void stopAnimation()
{
keepGoing = false;
animationThread = null;
}
public void actionPerformed(ActionEvent event)
{
if(animationThread == null)
start();
else
stopAnimation();
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Image Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
int w = 500, h = 400;
frame.setSize(w + 2, h + 30);
frame.setLocation(100, 100);
ImageTest demo = new ImageTest(w, h);
frame.setContentPane(demo);
demo.start();
frame.setVisible(true);
}
}
*an image(dragon.jpg, it's in the same directory as ImageTest.java) and put in
*on a buffer while scaling it down on the x-axis. Eventually I hope to scale
*it down to 0, mirror it, and scale it back up at the same rate so it looks
*as if it is rotating. As of now, all it does is show up at the same size.
*I tried doinga for loop and all but it just doesn't seem to work for me.
*Any help would be appreciated, thanks.
*/
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class ImageTest extends JPanel implements Runnable, ActionListener
{
public static final long FRAME_INTERVAL = 1;
public static final int X_INC = 2;
public static final int Y_INC = X_INC;
private Thread animationThread;
private BufferedImage image;
ImageIcon icon = new ImageIcon("dragon.jpg");
Image dragon = icon.getImage();
private int xLoc, yLoc, xDirec, yDirec, width, height;
private boolean keepGoing;
public ImageTest(int w, int h)
{
keepGoing = true;
image = new BufferedImage(900,900,BufferedImage.TYPE_INT_RGB);
width = w;
height = h;
xLoc = width;
yLoc = height;
xDirec = 1;
yDirec = 1;
setLayout(null);
}
public void paintComponent(Graphics g)
{
double scalex;
double scaley = 1;
Graphics2D g2d = (Graphics2D) g;
AffineTransform tx = new AffineTransform();
for(int c = 0; c < 101; c++)
{
scalex = (1 - (c/100));
tx.scale(scalex, scaley);
g2d.setTransform(tx);
g2d.drawImage(image, 0, 0, null);
}
}
public void update(Graphics g)
{
paintComponent(g);
}
public void updateImage()
{
Graphics2D gr = image.createGraphics();
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
gr.clearRect(0,0,image.getWidth(), image.getHeight());
gr.drawImage(dragon, 0, 0, null);
gr.dispose();
int x = 0;
int y = 0;
}
public void run()
{
while(keepGoing)
{
updateImage();
repaint();
try
{
Thread.sleep(FRAME_INTERVAL);
}
catch(InterruptedException exc){}
}
}
public void start()
{
keepGoing = true;
animationThread = new Thread(this);
animationThread.start();
}
public void stopAnimation()
{
keepGoing = false;
animationThread = null;
}
public void actionPerformed(ActionEvent event)
{
if(animationThread == null)
start();
else
stopAnimation();
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Image Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
int w = 500, h = 400;
frame.setSize(w + 2, h + 30);
frame.setLocation(100, 100);
ImageTest demo = new ImageTest(w, h);
frame.setContentPane(demo);
demo.start();
frame.setVisible(true);
}
}