/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package packagename;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @Aravind Sankaran Nair
*/
public class Game extends JPanel{
int x=0;
int y=0;
int xa=30;
int ya=30;
int xxa=60;
int yya=60;
int ballRadius=30;
public Game() {
this.setPreferredSize(new Dimension(320, 400));
}
void grow(){
x=x+1;
y=y;
}
/**
* @param args the
command line arguments
*/
void
moveTowardsBorder(){
if(x>=0&&x<=(300-(ballRadius+20))){
x=x+1;
y=y;
}
if(x==251&&y>=0&&y<=(400-(ballRadius)-40)){
x=x;
y=y+1;
}
if(y==331&&x<=251&&x>=0){
y=y;
x=x-2;
}
if(x==-1&&y>=y){
if(y!=0){
x=x;
y=y-1;}
else{
x=0;
y=0;
}
}
//
if(xa==30&&ya>=0&&ya<=300){
xa=xa;
ya=ya+1;
}
if(ya==301&&xa>=0&&xa<=230){
ya=ya;
xa=xa+1;
}
if(xa==231&&ya<=301){
if(ya!=30){
ya=ya-1;
xa=xa;}
}
if(ya==30&xa<=231){
xa=xa-1;
ya=ya;
}
if(xxa>=60&&xxa<=(250-(ballRadius+20))){
xxa=xxa+1;
yya=yya;
}
if(xxa==201&&yya>=0&&yya<=260){
xxa=xxa;
yya=yya+1;
}
if(yya==261&&xxa<=201&&xxa>=60){
yya=yya;
xxa=xxa-2;
}
if(xxa==59&&yya>=yya){
if(yya!=60){
xxa=xxa;
yya=yya-1;
}else{
xxa=60;
yya=60;
}
}
System.out.println("xxa=="+x);
System.out.println("yya=="+y);
}
@Override
public void
paint(Graphics g) {
super.paint(g);
Graphics2D
g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, 300, 400);
if(y==0){
g2d.setColor(Color.red);
g2d.fillOval(x, y, ballRadius/2, ballRadius/2);
}
else
if(x==251){
g2d.setColor(Color.green);
g2d.fillOval(x, y, ballRadius/2, ballRadius/2);
}
else
if(y==331){
g2d.setColor(Color.red);
g2d.fillOval(x, y, ballRadius/2, ballRadius/2);
}
else
if(x==-1){
g2d.setColor(Color.green);
g2d.fillOval(x, y, ballRadius/2, ballRadius/2);
}
if(xa==30){
g2d.setColor(Color.white);
g2d.fillOval(xa, ya, ballRadius/2, ballRadius/2);
}
else
if(ya==301){
g2d.setColor(Color.ORANGE);
g2d.fillOval(xa, ya, ballRadius/2, ballRadius/2);
}
else
if(xa==231){
g2d.setColor(Color.white);
g2d.fillOval(xa, ya, ballRadius/2, ballRadius/2);
}
else
if(ya==30){
g2d.setColor(Color.ORANGE);
g2d.fillOval(xa, ya, ballRadius/2, ballRadius/2);
}
if(xxa==60){
g2d.setColor(Color.blue);
g2d.fillOval(xxa, yya, ballRadius/2, ballRadius/2);
}
else
if(xxa==201){
g2d.setColor(Color.PINK);
g2d.fillOval(xxa, yya, ballRadius/2, ballRadius/2);
}
else
if(yya==261){
g2d.setColor(Color.blue);
g2d.fillOval(xxa, yya, ballRadius/2, ballRadius/2);
}
else
if(xxa==59){
g2d.setColor(Color.pink);
g2d.fillOval(xxa, yya, ballRadius/2, ballRadius/2);
}
else
{
g2d.setColor(Color.blue);
g2d.fillOval(xxa, yya, ballRadius/2, ballRadius/2);
}
}
public static void
main(String[] args) {
Game g=new
Game();
// TODO code
application logic here
JFrame
frame=new JFrame();
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(g);
frame.setVisible(true);
while(true){
g.moveTowardsBorder();
g.repaint();
try {
Thread.sleep(5);
} catch
(InterruptedException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
}
Output:
No comments:
Post a Comment