Results 1 to 1 of 1
Thread: Help with changing time values.
- 12-10-2011, 11:24 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Help with changing time values.
Hey everyone, this program displays a ball in a screen moving a set speed, set by the Time object clock at 20. One of my jobs is to allow the user to go to the menu, click on set speed option and enter any speed. So far, I've gotten as far as getting the window up asking the user to enter their desired amount. My problem is how to get that amount to a time object allowing it to change from the default. I tried multiple things, none seemed to work. I was now thinking instead of 2 Timer's. I put a variable where the 20 is in Timer clock. But I dont know how to pass the numSpeed to it. Help would be greatly appreciated. Thanks!
(Ps I didn't include the other files of code, I didn't think it would be necessary)
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; public class RollB extends JPanel implements ActionListener{ String inputSpeed; String inputCount; int numBall; int numSpeed; int s; Random random = new Random(); int ranStart = random.nextInt(60) + 1; int width = 700; int height = 250; Timer clock = new Timer(20,this); Timer newClock = new Timer(numSpeed,this); int NWall = 0; int WWall = 0; int EWall = width; int SWall = height; int h = 3; int v = 2; int x = 125; int y = 21; Ball ball = new Ball(x,y,50,h,v); JMenuBar b; JMenu menu = new JMenu("Game Controls"); JMenuItem quit = new JMenuItem("Quit"); JMenuItem stop = new JMenuItem("Stop"); JMenuItem start = new JMenuItem("Start"); JMenuItem speed = new JMenuItem("Speed"); JMenuItem newStart = new JMenuItem("New Start"); JMenuItem ballCount = new JMenuItem("Ball Count"); public RollB(JMenuBar bar){ clock.start(); newClock.start(); this.b=bar; b.add(menu); menu.add(quit); menu.add(stop); menu.add(start); menu.add(speed); menu.add(newStart); menu.add(ballCount); quit.addActionListener(this); stop.addActionListener(this); start.addActionListener(this); speed.addActionListener(this); newStart.addActionListener(this); ballCount.addActionListener(this); setPreferredSize(new Dimension(width,height)); setBackground(Color.white); } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawRect(0,0,width,height); 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()==quit) System.exit(0); else if(e.getSource()==stop) {clock.stop();} else if (e.getSource() == clock) { advanceBalls(); reflectBalls(); } else if(e.getSource()==start) clock.start(); else if(e.getSource()==speed){ inputSpeed=JOptionPane.showInputDialog("Enter delay (smaller=faster)"); numSpeed=Integer.parseInt(inputSpeed); } repaint(); } }
Similar Threads
-
Changing Image Icon of a JButton at run time, NOT state dependent
By jjlin12 in forum New To JavaReplies: 7Last Post: 07-12-2011, 04:19 PM -
Changing Webpage's Input Values
By Lawllerskates in forum New To JavaReplies: 0Last Post: 05-16-2010, 02:26 AM -
Changing the values of a map.
By Onra in forum New To JavaReplies: 1Last Post: 02-26-2010, 12:20 AM -
Changing colors of an image in real time
By chale in forum CLDC and MIDPReplies: 0Last Post: 05-06-2009, 03:04 PM -
Persistence problem when changing type to java.sql.Time
By Inks in forum JDBCReplies: 0Last Post: 03-08-2009, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks