Objectives The objective is to help students understand how to create graphics in Java. Steps In this lab, you are going to provide code that builds the application shown below. The application animates a ball moving inside a window. The ball changes color when the ball is clicked on, Please follow the instructions below: Create a new Java Application Project that has a main class named “CatchTheBall1” that ex tends JErame in a package named lab32 Add a new class, ArenaPanel that extends JPanel Copy the constant variables from the skeleton example to ArenaPanel. Add a constructor to CatchTheBall and add the usual code that sets up the JFrame and creates and adds a ArenaPanel object Override the paint () method of ArenaPanel and provide code that draws an oval that bounces off the walls as shown below. Add an inner class named TimerListener that handle timerevents to ArenaPanel. Add an inner class named ClickListener that handle mouse clicks to ArenaPanel. Run the program Raise your hand and demonstrate the running project to the instructor. Lab Questions How do you handle mouse drags?. How do you draw an arc?
Expert Answer
MovingBall.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movingballtester;
/**
*
* @author Akshay Bisht
*/
import java.awt.*;
public class MovingBall
{
private int ballSizeX, ballSizeY;
private int ballCX, ballCY;
private int ballPX, ballPY;
private int maxX, maxY;
Color color;
public MovingBall(int xsize, int ysize, int cX, int cY, int pX, int pY, int windowSizeX, int windowSizeY, Color c)
{
ballSizeX = xsize;
ballSizeY = ysize;
ballCX = cX;
ballCY = cY;
ballPX = pX;
ballPY = pY;
maxX = windowSizeX;
maxY = windowSizeY;
color = c;
}
public int getBallSizeX()
{
return ballSizeX;
}
public int getBallSizeY()
{
return ballSizeY;
}
public int getCX()
{
return ballCX;
}
public int getCY()
{
return ballCY;
}
public int getPX()
{
return ballPX;
}
public int getPY()
{
return ballPY;
}
public Color getBallColor()
{
return color;
}
public void setCX(int i)
{
ballCX = i;
}
public void setCY(int i)
{
ballCY = i;
}
public void setPX(int i)
{
ballPX = i;
}
public void setPY(int i)
{
ballPY = i;
}
public void moveBall(int lowY, int upY)
{
int cx = getCX();
int cy = getCY();
int px = getPX();
int py = getPY();
int x = 0;
int y = 0;
if( (cx>=325) && (cx<=330) && (cy<=5) && (cy<=upY || cy>=lowY) )
{
x = 5;
y = 5;
cx = 327;
cy = 2;
}
else if( (cx>=325) && (cx<=330) && (cy>=475-ballSizeY) && (cy<=upY || cy>=lowY) )
{
x = 5;
y = -5;
cx = 327;
cy = maxY-ballSizeY-2;
}
else if( (cx>=310-ballSizeX) && (cx<=315-ballSizeX) && (cy<=5) && (cy<=upY || cy>=lowY) )
{
x = -5;
y = 5;
cx = 310-ballSizeX-2;
cy = 2;
}
else if( (cx>=310-ballSizeX) && (cx<=315-ballSizeX) && (cy>=475-ballSizeY) && (cy<=upY || cy>=lowY) )
{
x = -5;
y = -5;
cx = 315-ballSizeX-2;
cy = maxY-ballSizeY-2;
}
else if( ( (cx>=315-ballSizeX && cx<=325-ballSizeX) && (cy<=upY || cy>=lowY )) && (cx-px>=5) && (cy-py>=5) )
{
x-=5;
y+=5;
}
else if( ( (cx>=315-ballSizeX && cx<=325-ballSizeX) && (cy<=upY || cy>=lowY ) ) && (cx-px>=5) && (py-cy>=5) )
{
x-=5;
y-=5;
}
else if( ( (cx>=315 && cx<=325) && (cy<=upY || cy>=lowY ) ) && (px-cx>=5) && (cy-py>=5) )
{
x+=5;
y+=5;
}
else if( ( (cx>=315 && cx<=325) && (cy<=upY || cy>=lowY ) ) && (px-cx>=5) && (py-cy>=5) )
{
x+=5;
y-=5;
}
else if( !(cy>=maxY-ballSizeY) && !(cx>=maxX-ballSizeX) && !(cy<=0) && !(cx<=0) && (cx>=5) && (cy>=5) && (cx-px>=5) && (cy-py>=5) )
{
x+=5;
y+=5;
}
else if( !(cy>=maxY-ballSizeY) && !(cx>=maxX-ballSizeX) && !(cy<=0) && !(cx<=0) && (cx>=5)&& (cy>=5) && (cx-px>=5) && (py-cy>=5) )
{
x+=5;
y-=5;
}
else if( !(cy>=maxY-ballSizeY) && !(cx>=maxX-ballSizeX) && !(cy<=0) && !(cx<=0) && (cx>=5)&& (cy>=5) && (px-cx>=5) && (cy-py>=5) )
{
x-=5;
y+=5;
}
else if( !(cy>=maxY-ballSizeY) && !(cx>=maxX-ballSizeX) && !(cy<=0) && !(cx<=0) && (cx>=5)&& (cy>=5) && (px-cx>=5) && (py-cy>=5) )
{
x-=5;
y-=5;
}
else if( (cx<=4) && (cy<=4) )
{
x = 5;
y = 5;
cx = 2;
cy = 2;
}
else if( (cx<=4) && (cy>=maxY-ballSizeY-4) )
{
x = 5;
y = -5;
cx = 2;
cy = maxY-ballSizeY-2;
}
else if( (cy<=4) && (cx>=maxX-ballSizeX-4) )
{
x = -5;
y = 5;
cx = maxX-ballSizeX-2;
cy = 2;
}
else if( (cy>=maxY-ballSizeY-4) && (cx>=maxX-ballSizeX-4) )
{
x = -5;
y = -5;
cx = maxX-ballSizeX-2;
cy = maxY-ballSizeY-2;
}
else if( (cy >= 480-ballSizeY) && (cx-px>= 5) && (cy-py>=5) )
{
x+=5;
y-=5;
}
else if( (cy>=480-ballSizeY) && (px-cx>=5) && (cy-py>=5) )
{
x-=5;
y-=5;
}
else if( (cy <= 4) && (cx-px >=5) && (py-cy>=5) )
{
x+=5;
y+=5;
}
else if( (cy<=4) && (px-cx>=5) && (py-cy>=5) )
{
x-=5;
y+=5;
}
else if( (cx>=640-ballSizeX) && (cx-px>=5) && (cy-py>=5) )
{
x-=5;
y+=5;
}
else if( (cx>=640-ballSizeX) && (cx-px>=5) && (py-cy>=5) )
{
x-=5;
y-=5;
}
else if( (cx<=4) && (px-cx>=5) && (cy-py>=5) )
{
x+=5;
y+=5;
}
else if( (cx<=4) && (px-cx>=5) && (py-cy>=5) )
{
x+=5;
y-=5;
}
setPX(cx);
setPY(cy);
setCX(cx+x);
setCY(cy+y);
}
public void sleep(int i)
{
try
{
Thread.sleep (i);
}
catch (Exception e)
{
}
}
}
MovingBallPanel.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movingballtester;
import javax.swing.*;
import java.awt.*;
import java.util.*;
/**
*
* @author Akshay Bisht
*/
public class MovingBallPanel extends JPanel
{
private ArrayList<MovingBall> mb;
private ArrayList<MovingBall> initials;
private int gapLY, gapTY;
private int level;
private final int APPLET_WIDTH, APPLET_HEIGHT;
public MovingBallPanel(int lowerGapY, int topGapY)
{
mb = new ArrayList<MovingBall>();
initials = new ArrayList<MovingBall>();
gapLY = lowerGapY;
gapTY = topGapY;
level = 1;
APPLET_WIDTH = 640;
APPLET_HEIGHT = 480;
setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
setBackground(Color.black);
mb.add(new MovingBall(20, 20, 136, 39, 131, 34, APPLET_WIDTH, APPLET_HEIGHT, Color.cyan));
mb.add(new MovingBall(20, 20, 200, 200, 205, 195, APPLET_WIDTH, APPLET_HEIGHT, Color.cyan));
mb.add(new MovingBall(20, 20, 453, 282, 448, 277, APPLET_WIDTH, APPLET_HEIGHT, Color.yellow));
mb.add(new MovingBall(20, 20, 97, 365, 92, 360, APPLET_WIDTH, APPLET_HEIGHT, Color.yellow));
initials.add(new MovingBall(20, 20, 136, 39, 131, 34, APPLET_WIDTH, APPLET_HEIGHT, Color.cyan));
initials.add(new MovingBall(20, 20, 200, 200, 205, 195, APPLET_WIDTH, APPLET_HEIGHT, Color.cyan));
initials.add(new MovingBall(20, 20, 453, 282, 448, 277, APPLET_WIDTH, APPLET_HEIGHT, Color.yellow));
initials.add(new MovingBall(20, 20, 97, 365, 92, 360, APPLET_WIDTH, APPLET_HEIGHT, Color.yellow));
}
public void paintComponent(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,640,480);
g.setColor(Color.white);
g.fillRect(315,0,10,gapTY);
g.fillRect(315,gapLY,10,(480-gapLY));
g.setColor(Color.cyan);
g.drawString(“Blue”, 145,20);
g.setColor(Color.yellow);
g.drawString(“Yellow”, 475,20);
g.setColor(Color.yellow);
g.drawString(“Directions”, 541, 410);
g.drawString(“1.Use the up and down”, 500,430);
g.drawString(“arrows to move”, 510,442);
g.drawString(“2.Get the balls on their”, 500, 457);
g.drawString(“proper sides”, 510, 470);
g.setColor(Color.cyan);
g.drawString((“Level: ” + level), 8, 472);
g.drawString(“C0d3r Cr3d|t5: M@5t3r_C0d3r”,6,460);
for(int x = 0; x < mb.size(); x++)
{
MovingBall t = mb.get(x);
g.setColor(t.getBallColor());
g.fillOval(t.getCX(), t.getCY(), t.getBallSizeX(),t.getBallSizeY());
}
}
public void beginGame1()
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillRect(186,157,14,5);
g.fillRect(520,157,14,5);
g.setColor(Color.cyan);
g.drawString(“Loading”, 145,160);
g.setColor(Color.yellow);
g.drawString(“Loading”, 475,160);
}
public void beginGame2()
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillRect(186,157,14,5);
g.fillRect(520,157,14,5);
g.setColor(Color.cyan);
g.drawString(“Loading.”, 145,160);
g.setColor(Color.yellow);
g.drawString(“Loading.”, 475,160);
}
public void beginGame3()
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillRect(186,157,14,5);
g.fillRect(520,157,14,5);
g.setColor(Color.cyan);
g.drawString(“Loading..”, 145,160);
g.setColor(Color.yellow);
g.drawString(“Loading..”, 475,160);
}
public void beginGame4()
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillRect(186,157,14,5);
g.fillRect(520,157,14,5);
g.setColor(Color.cyan);
g.drawString(“Loading…”, 145,160);
g.setColor(Color.yellow);
g.drawString(“Loading…”, 475,160);
}
public int getLowerY()
{
return gapLY;
}
public int getUpperY()
{
return gapTY;
}
public void callMoveBallMethod(int l, int u)
{
for(int x = 0; x < mb.size(); x++)
{
MovingBall t = mb.get(x);
t.moveBall(l,u);
repaint();
}
sleep(20);
}
public boolean checkBalls()
{
for(int x = 0; x < mb.size(); x++)
{
MovingBall temp = mb.get(x);
Color c = temp.getBallColor();
int currentXPosition = temp.getCX();
if(c == Color.cyan)
{
if(!(currentXPosition<315))
{
return false;
}
}
else
{
if( !(currentXPosition>325) )
{
return false;
}
}
}
return true;
}
public void addAnotherBall()
{
Random rm = new Random();
int cxValue = rm.nextInt(550) + 25;
int cyValue = rm.nextInt(435) + 25;
int xValue = -1;
int yValue = -1;
int n = rm.nextInt(4) + 1;
if(n == 1)
{
xValue = 5;
yValue = 5;
}
else if(n == 2)
{
xValue = 5;
yValue = -5;
}
else if(n == 3)
{
xValue = -5;
yValue = 5;
}
else if(n == 4)
{
xValue = -5;
yValue = -5;
}
int k = rm.nextInt(2) + 1;
Color g = Color.cyan;
if(k == 2)
{
g = Color.yellow;
}
initials.add(new MovingBall(20, 20, cxValue, cyValue, (cxValue+xValue), (cyValue+yValue), APPLET_WIDTH, APPLET_HEIGHT, g));
mb.clear();
for(int y = 0; y < initials.size(); y++)
{
mb.add(initials.get(y));
}
level++;
if(checkBalls())
{
level–;
initials.remove(initials.size()-1);
addAnotherBall();
}
beginGame1();
sleep(300);
beginGame2();
sleep(300);
beginGame3();
sleep(300);
beginGame4();
sleep(300);
beginGame1();
sleep(300);
beginGame2();
sleep(300);
beginGame3();
sleep(300);
beginGame4();
sleep(300);
}
public void moveGapLocationUp()
{
gapLY-=10;
gapTY-=10;
}
public void moveGapLocationDown()
{
gapLY+=10;
gapTY+=10;
}
public void sleep(int i)
{
try
{
Thread.sleep (i);
}
catch (Exception e)
{
}
}
}
MovingBallTester.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movingballtester;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Akshay Bisht
*/
public class MovingBallTester
{
private final int UPGAP_Y, LOWGAP_Y;
private MovingBallPanel mbp;
private JFrame movingBallFrame;
public MovingBallTester()
{
LOWGAP_Y = 250;
UPGAP_Y = 210;
mbp = new MovingBallPanel(LOWGAP_Y, UPGAP_Y);
movingBallFrame = new JFrame(“Moving Ball”);
movingBallFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
movingBallFrame.addKeyListener(new KeypadListener());
movingBallFrame.setResizable(false);
movingBallFrame.getContentPane().add(mbp);
displayFrame();
runGame();
}
public void displayFrame()
{
movingBallFrame.pack();
movingBallFrame.show();
}
public void runGame()
{
sleep(500);
mbp.beginGame1();
sleep(300);
mbp.beginGame2();
sleep(300);
mbp.beginGame3();
sleep(300);
mbp.beginGame4();
sleep(300);
mbp.beginGame1();
sleep(300);
mbp.beginGame2();
sleep(300);
mbp.beginGame3();
sleep(300);
mbp.beginGame4();
sleep(300);
mbp.beginGame1();
sleep(300);
mbp.beginGame2();
sleep(300);
mbp.beginGame3();
sleep(300);
mbp.beginGame4();
sleep(300);
while(true)
{
int lowerY = mbp.getLowerY();
int upperY = mbp.getUpperY();
mbp.callMoveBallMethod(lowerY,upperY);
if(mbp.checkBalls())
{
mbp.addAnotherBall();
}
}
}
public void sleep(int i)
{
try
{
Thread.sleep(i);
}
catch (Exception e)
{
}
}
private class KeypadListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int source = e.getKeyCode();
if(source == KeyEvent.VK_UP)
{
mbp.moveGapLocationUp();
}
else if(source == KeyEvent.VK_DOWN)
{
mbp.moveGapLocationDown();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
public static void main (String[] args)
{
new MovingBallTester();
}
}
Output: