using java 2d graphics one is supposed to draw a spinning letter E that moves from the right bottom-most corner to the right topmost corner before starting to write the Letter
Σ
What I have tried:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class fanta{
public static void main(String[] args) {
JFrame jf = new JFrame("CS303");
Container cp = jf.getContentPane();
JavaAnimation tl = new JavaAnimation();
cp.add(tl);
jf.setSize(950, 850);
jf.setVisible(true);
}
}
class JavaAnimation extends JPanel implements ActionListener {
Timer t = new Timer(5, this);
int x = 450, y = 650, velX = 5,velY=5;
int x2=850,y2=250, velX2 = 5;
boolean line1=false,line2=false,line3=false,line4=false,line5=false,isDone=false;
int rotation_value=0;
String val="Σ";
public static void drawRotate(Graphics2D g2d, double x, double y, int angle, String text)
{
g2d.translate((float)x,(float)y);
g2d.rotate(Math.toRadians(angle));
g2d.drawString(text,0,0);
g2d.rotate(-Math.toRadians(angle));
g2d.translate(-(float)x,-(float)y);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(g instanceof Graphics2D)
{
Graphics2D g2 = (Graphics2D)g;
Font font = new Font("Times New Roman", Font.PLAIN, 100);
g2.setFont(font);
g2.setColor(new Color(0,255,0));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g2.setColor(new Color(0,0,255));
drawRotate(g2, x, y, rotation_value, "");
t.start();
if(isDone==true) {
t.stop();
}
}
}
public void actionPerformed(ActionEvent e) {
if(y> 100&&line1==false&&line2==false) {
y-=velY;
}
if(y==100&&x<=850&&line1==false&&line2==false) {
x += velX;
if(x==850) {
line1=true;
}
}
else if(y==100&&line1==true&&line2==false) {
x -= velX;
if(x==450) {
line2=true;
line1=false;
}
}
if(line2==true&&y<350&&line3==false) {
y+=velY;
if(y==350) {
line3=true;
}
}
if(y==350&&x<=850&&line3==true&&line4==false) {
x+=velX;
if(x==850) {
line3=false;
line4=true;
}
} else if(y==350&&x>450&&line3==false&&line4==true) {
x-=velX;
if(x==450) {
line5=true;
}
}
if(y<650&&line5==true) {
y+=velY;
if(y==650) {
isDone=true;
}
}
if(rotation_value<360) {
rotation_value+=5;
}
else
rotation_value=0;
repaint();
}
}