Current location: Hot Scripts Forums » Programming Languages » Everything Java » Java 2d, scaling an image...


Java 2d, scaling an image...

Reply
  #1 (permalink)  
Old 03-11-04, 07:34 PM
RedWulf RedWulf is offline
New Member
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Java 2d, scaling an image...

/*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.
*/

Code:
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_ANTIALIASING, 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_CLOSE);

						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);
					}
}
Reply With Quote
  #2 (permalink)  
Old 03-15-04, 01:06 PM
RedWulf RedWulf is offline
New Member
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
//Ok, at least i got it to shear now...

Code:
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 = 20;
			public static final int X_INC = 2;
			public static final int Y_INC = X_INC;
			public int c;
			


			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();


							    	c++;
								scalex = (1 - (c/100.0));
								tx.scale(scalex, scaley);
								g2d.setTransform(tx);
								g2d.drawImage(image, 200, 100, null);
								
								
													
						
					}


				public void update(Graphics g)
					{
						paintComponent(g); 
					}



				public void updateImage()
					{ 

						Graphics2D gr = image.createGraphics();
						gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 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_CLOSE);

						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);
					}
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript menu covered by java applet shaisachs JavaScript 7 12-28-04 11:38 PM
how to change text dynamically with image? rabbit51 JavaScript 1 02-23-04 08:35 AM
How to change text dynamically with image? rabbit51 Script Requests 0 02-07-04 12:14 PM
A different kind of image counter Stix Script Requests 1 08-11-03 11:50 PM


All times are GMT -5. The time now is 06:00 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.