« Back to profile of Emmy

  • Java

    AffineTransform

    Write java program with GUI components as in the following figure. The user should enter the values for scaling, rotating and translating a Rectangle, then the program should display the results of three different compositions with different colors using ONE AffineTransform object. Discuss your results. Note: in rotation operation, you need to convert the angle of rotation from degrees to radian. *NEEDS SOME MODIFICATIONS*

    20:44 May 05 2011 | Tags : AffineTransform,
    						//Student Name: Eman Basahel
//ID:0931047
//-----------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

    

//First class (Applet class)
public class Rec extends JApplet  implements ActionListener{
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Rec applet = new Rec();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  JTextField text1 = new JTextField(3);
  JTextField text2 = new JTextField(3);
  JTextField text3= new JTextField(3);
  JTextField text4= new JTextField(3);
  JTextField text5= new JTextField(3);
  JLabel l1= new JLabel("X scale:");
  JLabel l2= new JLabel("Y scale:");
  JLabel l3= new JLabel("Rotation by:");
  JLabel l4= new JLabel("X translate:");
  JLabel l5= new JLabel("Y translate:");
  
  
   APanel panel = new APanel();
  
  public void init() {
    
    JPanel p= new JPanel();
    JButton b = new JButton("Draw");
    p.setLayout(new FlowLayout());
    p.add(l1);
    p.add(text1);
    p.add(l2);
    p.add(text2);
    p.add(l3);
    p.add(text3);
    p.add(l4);
    p.add(text4);
    p.add(l5);
    p.add(text5);
    p.add(b);
    //..................................
    
    
    b.addActionListener(this);
     
    this.getContentPane().setLayout(new BorderLayout());

    this.getContentPane().add(p, BorderLayout.NORTH);
        
    this.getContentPane().add(panel, BorderLayout.CENTER);
  }
  
  
  
    public void actionPerformed(ActionEvent e) {
   
  	String dim1= text1.getText();
  	String dim2= text2.getText();
  	String dim3= text3.getText();
  	String dim4= text4.getText();
  	String dim5= text5.getText();
  	panel.d1 = Integer.parseInt(dim1.trim());
  	panel.d2 = Integer.parseInt(dim2.trim());
  	panel.d3 = Integer.parseInt(dim3.trim());
  	panel.d4 = Integer.parseInt(dim4.trim());
  	panel.d5 = Integer.parseInt(dim5.trim());
  	
  	System.out.println(panel.d1 +"  "+panel.d2+"  "+panel.d3+"  "+panel.d4+"  "+panel.d5 );
  }
}

//Second class (Panel class)
class APanel extends JPanel{
  public APanel() {
    setPreferredSize(new Dimension(500, 500));
    setBackground(Color.white);
  }
  
     	public int d1, d2,d3,d4,d5;
  
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
     repaint();  
    
     Rectangle2D rec = new Rectangle.Double(0,0,100,100);
     g2.setColor(Color.pink);
     g2.fill(rec);   

     AffineTransform a = new AffineTransform();
     a.setToIdentity();
    a.translate(d4, d5);
    Shape s2 = a.createTransformedShape(rec);
    g2.setColor(Color.yellow); 
    g2.fill(s2);   
  
    double  x = Math.PI/d1;
    a.rotate(Math.PI/x);
    Shape s = a.createTransformedShape(rec);  
    g2.setColor(Color.pink);
    g2.fill(s);  
    
    a.scale(d2,d3 ) ;
    Shape s1 = a.createTransformedShape(rec); 
    g2.setColor(Color.green);
    g2.fill(s1);     
  }
}    			         

    1 comments

  • Emmy says :

    http://s1.postimage.org/9xuao1mcc/Affine_Transform.jpg

Add comment
To add a comment, please : Login or Sign up
    Emmy
    Eman Basahel
  • New visitor?

    On setCode, you can save your codes, functions and classes, you can also share them with your friends, and track your favorite developer

    Join the SetCode community
  • Your language here :