Instructions
Requirements and Specifications
Source Code
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author Yashkumar Chadasania
* CS07580_10/12/2020
*/
public class MovingSpace extends JPanel
implements KeyListener, ActionListener {
Timer t=new Timer(5,this);
int x=0, y=0, velx=0, vely=0;
int width = 200;
int height=200;
public MovingSpace() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
@Override
public void paintComponent( Graphics g ) {
//cleans the screen
super.paintComponent(g);
Graphics2D g2= (Graphics2D) g;
//g2.fillRect(x, y, 40, 40);
super.paintComponent(g);
g2.setColor(Color.GREEN);
g2.fillArc(125+x, 140+y, 150, 50, 45, 360);
g2.setColor(Color.black);
g2.drawArc(125+x, 140+y, 150, 50, 50, -280);
g2.setColor(Color.GREEN);
g2.fillArc(150+x, 110+y, 100, 75, 0, 180);
g2.setColor(Color.black);
g2.drawArc(150+x, 110+y, 100, 75, 0, 180);
g2.setColor(Color.black);
g2.fillArc(160+x, 130+y, 15, 25, 0, 360);
g2.setColor(Color.red);
g2.fillArc(180+x, 135+y, 15, 25, 0, 360);
g2.setColor(Color.yellow);
g2.fillArc(200+x, 135+y, 15, 25, 0, 360);
g2.setColor(Color.cyan);
g2.fillArc(220+x, 130+y, 15, 25, 0, 360);
g2.setColor(Color.blue);
g2.fillArc(25, 270, 350, 80, 0, 180);
g2.setColor(Color.black);
g2.fillRect(110, 235, 25, 50);
g2.fillRect(140, 220, 25, 70);
g2.fillRect(170, 235, 25, 50);
g2.setColor(Color.magenta);
g2.setFont(new Font("TimesRoman", Font.BOLD,12));
g2.drawString("Yashkumar's", 160+x, 170+y);
g2.drawString("Spaceship", 160+x, 180+y);
}
public void actionPerformed(ActionEvent e){
x = x+velx;
y = y+vely;
repaint();
}
public void up(){
vely=-1;
velx=0;
}
public void down(){
vely=1;
velx=0;
}
public void left(){
vely=0;
velx=-1;
}
public void right(){
vely=0;
velx=1;
}
public void enlarge(){
x+=width*2;
y+=height*2;
}
@Override
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if (code == KeyEvent.VK_L){
up();
}
if (code == KeyEvent.VK_U){
down();
}
if (code == KeyEvent.VK_R){
left();
}
if (code == KeyEvent.VK_D){
right();
}
if (code == KeyEvent.VK_Q){
enlarge();}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
velx=0;
vely=0;
}
public static void main(String[] args) {
JFrame jp1 = new JFrame();
MovingSpace a = new MovingSpace();
a.setFocusable(true);
a.requestFocusInWindow();
jp1.getContentPane().add(a, BorderLayout.CENTER);
jp1.setSize(new Dimension(500,400));
jp1.setVisible(true);
jp1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setFocusTraversalKeysEnable(boolean b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class keyEvent {
public keyEvent() {
}
private int getKeycode() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
private static class Public {
public Public() {
}
}
}