Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2008, 12:59 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
AffineTransform demo
Code:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class AffineTransformApp extends JFrame { DisplayPanel displayPanel; JComboBox scaleXval, scaleYval, shearXval, shearYval; String[] scaleValues = { "0.10", "0.25", "0.50", "0.75", "1.00", "1.25", "1.50", "1.75", "2.00" }; String[] shearValues = { "0.00", "0.25", "0.50", "0.75", "1.00" }; public AffineTransformApp() { super(); Container container = getContentPane(); displayPanel = new DisplayPanel(); container.add(displayPanel); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2, 4, 5, 5)); scaleXval = new JComboBox(scaleValues); scaleXval.setSelectedItem("1.00"); scaleXval.addActionListener(new ComboBoxListener()); scaleYval = new JComboBox(scaleValues); scaleYval.setSelectedItem("1.00"); scaleYval.addActionListener(new ComboBoxListener()); shearXval = new JComboBox(shearValues); shearXval.setSelectedItem("0.00"); shearXval.addActionListener(new ComboBoxListener()); shearYval = new JComboBox(shearValues); shearYval.setSelectedItem("0.00"); shearYval.addActionListener(new ComboBoxListener()); panel.add(new JLabel("Scale X value:")); panel.add(scaleXval); panel.add(new JLabel("Scale Y value:")); panel.add(scaleYval); panel.add(new JLabel("Shear X value:")); panel.add(shearXval); panel.add(new JLabel("Shear Y value:")); panel.add(shearYval); container.add(BorderLayout.SOUTH, panel); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(displayPanel.getWidth(), displayPanel.getHeight() + 10); setVisible(true); } public static void main(String arg[]) { new AffineTransformApp(); } class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { JComboBox temp = (JComboBox) e.getSource(); if (temp == scaleXval) { displayPanel.scalex = Double.parseDouble((String) temp .getSelectedItem()); displayPanel.applyValue(true, false); displayPanel.applyFilter(); displayPanel.repaint(); } else if (temp == scaleYval) { displayPanel.scaley = Double.parseDouble((String) temp .getSelectedItem()); displayPanel.applyValue(true, false); displayPanel.applyFilter(); displayPanel.repaint(); } else if (temp == shearXval) { displayPanel.shearx = Double.parseDouble((String) temp .getSelectedItem()); displayPanel.applyValue(false, true); displayPanel.applyFilter(); displayPanel.repaint(); } else if (temp == shearYval) { displayPanel.sheary = Double.parseDouble((String) temp .getSelectedItem()); displayPanel.applyValue(false, true); displayPanel.applyFilter(); displayPanel.repaint(); } } } } class DisplayPanel extends JLabel { Image displayImage; BufferedImage biSrc, biDest; BufferedImage bi; Graphics2D big; AffineTransform transform; double scalex = 1.0; double scaley = 1.0; double shearx = 1.0; double sheary = 1.0; DisplayPanel() { setBackground(Color.black); loadImage(); setSize(displayImage.getWidth(this), displayImage.getWidth(this)); // panel createBufferedImages(); transform = new AffineTransform(); } public void loadImage() { displayImage = Toolkit.getDefaultToolkit().getImage( "largeJavaLogo.jpg"); MediaTracker mt = new MediaTracker(this); mt.addImage(displayImage, 1); try { mt.waitForAll(); } catch (Exception e) { System.out.println("Exception while loading."); } if (displayImage.getWidth(this) == -1) { System.out.println(" Missing .jpg file"); System.exit(0); } } public void createBufferedImages() { biSrc = new BufferedImage(displayImage.getWidth(this), displayImage .getHeight(this), BufferedImage.TYPE_INT_RGB); big = biSrc.createGraphics(); big.drawImage(displayImage, 0, 0, this); bi = biSrc; biDest = new BufferedImage(displayImage.getWidth(this), displayImage .getHeight(this), BufferedImage.TYPE_INT_RGB); } public void applyValue(boolean scale, boolean shear) { if (scale) { transform.setToScale(scalex, scaley); scale = false; } else if (shear) { transform.setToShear(shearx, sheary); shear = false; } } public void applyFilter() { AffineTransformOp op = new AffineTransformOp(transform, null); Graphics2D biDestG2D = biDest.createGraphics(); biDestG2D .clearRect(0, 0, biDest.getWidth(this), biDest.getHeight(this)); op.filter(biSrc, biDest); bi = biDest; } public void reset() { big.setColor(Color.black); big.clearRect(0, 0, bi.getWidth(this), bi.getHeight(this)); big.drawImage(displayImage, 0, 0, this); } public void update(Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); paintComponent(g); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D) g; g2D.drawImage(bi, 0, 0, this); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform Demo Java Tip java.awt 0 06-21-2008 10:52 PM
Another GradientPaint Demo Java Tip java.awt 0 06-21-2008 10:48 PM
If statement demo Java Tip java.lang 0 04-23-2008 10:05 PM
If Else Demo Java Tip java.lang 0 04-23-2008 10:04 PM
affineTransform rotation MichYer AWT / Swing 0 07-18-2007 10:55 AM


All times are GMT +3. The time now is 07:28 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org