Results 1 to 2 of 2
Thread: Graphics Problem
- 12-12-2011, 01:23 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Graphics Problem
For this assignment you are given a very simple bouncing ball graphics program. The code resides in RollA.java; RollA is a subclass of JPanel. It is attached below, along with the class Ball.java, which it relies on, and a simple driver for displaying and running RollA, DriverA. The DisplayWindow class from the text is also attached.
Your assignment here is to implement a class called RollB (which works with DriverB, below). RollB drastically expands the functionality of RollA; its actions are tied to these menu choices, which you need to implement:
Quit (2 points) - terminate the program
Stop (1 point) - stop ball movement
Start (1 point) - start up ball movement
Speed (2 points) - set the speed of the balls (do this using the setDelay method from the Timer class)
New Start (2 points) - start the balls rolling with a fresh start: all balls should have new, randomly generated starting positions, and should move in new, randomly generated directions (each ball should take on new randomly generated values for h and v between -5 and 5). Note: When you start up the program for the first time, start it up in the style of "New Start" - that is, start it with balls at random positions, moving in random directions. Initially begin with 20 balls.
BallCount (2 points) - this can be any positive integer value -- although things get a little silly when this number gets above ~150.
In general, when balls are created, they should be placed randomly in the window, and should move in random directions, which are determined by h and v, as described above. Thus in general a ball advances by h and v, as shown below - here, increasing h and decreasing v. You should download and compile the DriverA code and related files to see the one ball version at work, and you will need to read that code over carefully.
The pulled-down menu reveals the various command choices that should be available in the final version of the rolling assignment. For two of the commands, speed and ball count, use a JOptionPane method call to enter the data the command requires.
RollA CLass:Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class RollA extends JPanel implements ActionListener{ int width = 700; int height = 250; //height, width of panel/window Timer clock = new Timer(20,this); //timer object with delay 20 // wall values int NWall = 0; int WWall = 0; int EWall = width; int SWall = height; // initial ball directions: h increasing by 3, v by 2 int h = 3; int v = 2; // initial position of ball int x = 125; int y = 21; Ball ball = new Ball(x,y,50,h,v); public RollA(){ setPreferredSize(new Dimension(width,height)); clock.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); drawBalls(g); } public void drawBalls(Graphics g){ g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR()); } public void reflectBalls(){ // when ball reaches (crosses) a wall, it flips (bounces) if (ball.getY() <= NWall) ball.flipVertical(); else if (ball.getY() >= (SWall-50)) ball.flipVertical(); else if (ball.getX() <= WWall) ball.flipHorizontal(); else if (ball.getX() >= EWall-50) ball.flipHorizontal(); } public void advanceBalls(){ // ball moves ahead h,v units ball.advance(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == clock) { advanceBalls(); reflectBalls(); } repaint(); } } Ball Class: public class Ball{ private int x; private int y; private int r; private int h; private int v; public Ball(int x,int y, int r, int h, int v){ this.x = x; this.y = y; this.r = r; this.h = h; this.v = v; } public void advance(){ x = x + h; y = y + v; } public void flipVertical(){ v = -v; } public void flipHorizontal(){ h = -h; } public int getX(){return x;} public int getY(){return y;} public int getR(){return r;} public int getH(){return h;} public int getV(){return v;} public void setX(int x){this.x = x;} public void setY(int y){this.y = y;} public void setR(int r){this.r = r;} public void setH(int h){this.h = h;} public void setV(int v){this.v = v;} } DriverA Class: public class DriverA{ public static void main(String[] args){ DisplayWindow d = new DisplayWindow(); RollA p = new RollA(); d.addPanel(p); d.showFrame(); } } DriverB Class import javax.swing.*; public class DriverB{ public static void main(String[] args){ DisplayWindow d = new DisplayWindow(); JMenuBar b = new JMenuBar(); d.setJMenuBar(b); RollB p = new RollB(b); d.addPanel(p); d.showFrame(); } } DisplayWindow Class: /** * A class that puts a graphics window on your display */ import java.awt.*; import javax.swing.*; public class DisplayWindow extends JFrame{ /** * Content pane that will hold the added Jpanel */ private Container c; /** * DisplayWindow constructor - no parameters */ public DisplayWindow(){ super("Display"); c = this.getContentPane(); } /** * Adds panel to content pane * @parameter the panel to be added */ public void addPanel(JPanel p){ c.add(p); } /** * consolidates the frame, makes it visible, * causes program termination when window is closed manually */ public void showFrame(){ this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
I really just need help getting this thing started. I don't know where to begin. I usually have no problem with these sort of things but this one is killing me and I need some outside insight on it, I've been staring at it all day. So if someone can just give me a little jump start, it would be amazing and you would never be more greatly appreciated.Last edited by Norm; 12-12-2011 at 05:43 PM. Reason: added code tags
- 12-12-2011, 05:44 PM #2
Similar Threads
-
Graphics problem?
By santa in forum Java 2DReplies: 1Last Post: 05-28-2011, 10:06 PM -
KeyListener Graphics Problem
By Makesfolkslose in forum New To JavaReplies: 2Last Post: 05-26-2011, 05:07 AM -
Problem in Graphics
By justbeller in forum Java 2DReplies: 1Last Post: 04-03-2011, 01:39 PM -
Graphics problem
By luke24 in forum New To JavaReplies: 7Last Post: 02-19-2011, 09:19 PM -
i am having a problem with creating graphics
By umpire43055 in forum AWT / SwingReplies: 3Last Post: 08-07-2009, 06:26 AM
Bookmarks