Important alert: (current site time 5/23/2013 8:33:03 PM EDT)
 

VB icon

Translate and Rotate

Email
Submitted on: 4/27/2012 5:23:48 PM
By: Creator_1493  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 1242
(About the author)
 
     This applet basically draws some shapes and then, rotates the applet screen to some angle depending upon click(make sure you click on applet to rotate it to a specific angle). comments are highly accepted.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Translate and Rotate
// Description:This applet basically draws some shapes and then, rotates the applet screen to some angle depending upon click(make sure you click on applet to rotate it to a specific angle).
comments are highly accepted.
// By: Creator_1493
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7026&lngWId=2//for details.//**************************************

// Translation and Rotation applet
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Translate_Rotate extends JComponent {
 private Image image;
 private int theta;
 public Translate_Rotate() {
image = Toolkit.getDefaultToolkit().getImage("A.jpg");
theta = 0;
addMouseListener(new MouseAdapter() {
 public void mousePressed(MouseEvent me) {
theta = (theta + 15) % 360;
repaint();
 }
});
 }
 public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int cx = getSize().width / 2;
int cy = getSize().height / 2;
g2.translate(cx, cy);
g2.rotate(theta * Math.PI / 180);
Shape oldClip = g2.getClip();
Shape e = new Ellipse2D.Float(-cx, -cy, cx * 3, cy * 2);
g2.clip(e);
Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 3);
g2.setPaint(new GradientPaint(40, 40, Color.green, 70, 40, Color.yellow, true));
g2.fill(c);
Shape c2 = new Ellipse2D.Float(-cx * 2, -cy /2, cx * 2, cy * 5);
g2.setPaint(new GradientPaint(40, 40, Color.red, 70, 40, Color.green, true));
g2.fill(c2);
g2.setPaint(Color.cyan);
g2.fillOval(cx / 4, 10, cx * 2, cy / 2);
g2.setClip(oldClip);
g2.setFont(new Font("Times New Roman", Font.ITALIC, 100));
g2.setPaint(new GradientPaint(-cx, 0, Color.black, cx, 0, Color.black, false));
g2.drawString("CREATOR 1493!", -cx * 3 / 4, cy / 4);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
g2.setComposite(ac);
Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 1 / 4, cy * 3 / 4, 20, 20);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.magenta);
g2.fill(r);
g2.setPaint(Color.orange);
g2.draw(r);
Shape r2 = new RoundRectangle2D.Float(20, -cy * 1 / 4, cx * 1 / 4, cy * 3 / 4, 120, 100);
g2.setStroke(new BasicStroke(5));
g2.setPaint(Color.yellow);
g2.fill(r2);
g2.setPaint(Color.red);
g2.draw(r2);
g2.drawImage(image, -cx / 3, -cy / 2, this);
 }
 public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Translate_Rotate(), BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
 }
}
// end


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.