'Moving a Character that is rotating in 360 degrees clockwise in 2D Graphics in a path in Java
I have this assignment
OUTPUT AN ALPHABET/CHARACTER AND MAKE IT ROTATE IN 360 DEGREES CLOCKWISE ON THE SCREEN AND MOVE IN A PATH AS IF IT IS DRAWING A BIGGER SIZE OF THE ALPHABET/CHARACTER.
I have been able to get the character "F" to rotate 360 degrees clockwise. Now, I need to further move the rotating string in a path as if drawing a larger size of the character.
I need assistance getting the alphabet to move from right to left, top to down, down to middle, and then middle left to right.
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
public class Main extends JPanel{
static int angdeg=0;
@Override
// importing paint method to use Graphics library
public void paint(Graphics g){
// creating 2D graphic object
Graphics2D g2d = (Graphics2D) g;
//
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//
//setting background
g2d.setColor(Color.white); //to remove trail of painting
g2d.fillRect(0,0,getWidth(),getHeight());
// creating font
Font font = new Font("bloomsburg",Font.BOLD,150);
g2d.setFont(font); //setting font of surface
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout("F", font, frc);
//getting width & height of the text
double sw = layout.getBounds().getWidth();
double sh = layout.getBounds().getHeight();
//getting original transform instance
AffineTransform saveTransform=g2d.getTransform();
g2d.setColor(Color.black);
Rectangle rect = this.getBounds();
//drawing the axis
//g2d.drawLine((int)(rect.width)/2,0,(int)(rect.width)/2,rect.height);
//g2d.drawLine(0,(int)(rect.height)/2,rect.width,(int)(rect.height)/2);
/*creating instance set the translation to the mid of the component*/
AffineTransform affineTransform = new AffineTransform();
affineTransform.setToTranslation((rect.width)/2,(rect.height)/2);
//rotate with the anchor point as the mid of the text
affineTransform.rotate(Math.toRadians(angdeg), 0, 0);
g2d.setTransform(affineTransform);
g2d.drawString("F",(int)-sw/2,(int)sh/2);
//restoring original transform
g2d.setTransform(saveTransform);
}
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame("Main");
Main rt=new Main();
frame.add(rt);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
while(true){
Thread.sleep(10); //sleeping then increasing angle by 5
angdeg=(angdeg>=360)?0:angdeg+6; //
rt.repaint(); //repainting the surface
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
