I need help with JButton

i need help setting the size of JButton

Page 1 of 1

5 Replies - 2744 Views - Last Post: 15 November 2010 - 03:05 AM Rate Topic: -----

#1 nyro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-October 10

I need help with JButton

Posted 14 November 2010 - 10:55 AM

okay so in my computer programming class we were given a challenge to add to this pong game. it's a very a simple game just a paddle and a ball. well i want to add a clock that counts down from 60 and the game ends, a start button and pause button. and maybe a leader board. but right now my main question is how do i set the size of my start and pause button? I am using JButton.
and if you could help with the timer that would be nice too. I dont want code just maybe a website i could look at or something.

                                     
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import acm.graphics.*;
import acm.program.*;
import sun.audio.*;

public class ModPong extends GraphicsProgram {
	private GRect paddle;
	private JButton start;
	private JButton pause;

	public void run() {
		this.setSize(500, 500);
		this.setBackground(Color.black);

		paddle = new GRect(10, 10, 10, 100);
		paddle.setFilled(true);
		paddle.setFillColor(Color.green);
		addMouseListeners();
		add(paddle);

		start = new JButton("Start");
		start.setPreferredSize(new Dimension(100, 70));
		addActionListeners();
		add(start);

		GOval ball = new GOval(25, 25, 20, 20);
		ball.setFilled(true);
		ball.setFillColor(Color.blue);
		add(ball);

		double dx = 3;
		double dy = -4;f
		while (true) {
			pause(25);
			double ballLeft = ball.getX();
			double ballMid = ball.getY() + ball.getHeight() / 2;
			double paddleRight = paddle.getX() + paddle.getWidth();
			double paddleTop = paddle.getY();
			double paddleBot = paddle.getY() + paddle.getHeight();
			if (ballLeft + ball.getWidth() >= getWidth()
					|| ballLeft <= paddleRight && ballLeft > paddleRight + dx
					&& ballMid >= paddleTop && ballMid < paddleBot) {
				dx = -dx;
			}// close if statement

			if (ball.getY() + ball.getHeight() >= getHeight()
					|| ball.getY() <= 0.0) {
				dy = -dy;

			}// close if statement
			ball.move(dx, dy);
		}// close while statement
	}// close run method

	public void mouseMoved(MouseEvent a) {
		double paddleY = a.getY() - paddle.getHeight() / 2;
		double paddleMax = getHeight() - paddle.getHeight();
		if (paddleY < 0)
			paddleY = 0;
		if (paddleY > paddleMax)
			paddleY = paddleMax;
		paddle.setLocation(10, paddleY);

	}// closes mouseMoved

	public void mouseClick(MouseEvent B)/> {

	}// close mousClick
}// close public class









Is This A Good Question/Topic? 0
  • +

Replies To: I need help with JButton

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: I need help with JButton

Posted 14 November 2010 - 11:49 AM

You can check this link for JButtons:
http://download.orac...nts/button.html
and the JButton API:
http://download.orac...ng/JButton.html

for the timer use a swing Timer, just set a variable time to 60, and use the timer to decrease 1 from that variable each second. end the game when that variable's value reaches 0.
check swing timer here:
http://download.orac...misc/timer.html

swing timer API
http://download.orac...wing/Timer.html


Feel free to post again if you have other problems :)
Was This Post Helpful? 0
  • +
  • -

#3 nyro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-October 10

Re: I need help with JButton

Posted 14 November 2010 - 01:22 PM

thank you,now i also need the ball to refresh after if it passes the paddle. how would i do that?
Was This Post Helpful? 0
  • +
  • -

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: I need help with JButton

Posted 14 November 2010 - 01:50 PM

By "passing the paddle" you mean the ball "exited" the Panel south bound?
if so, all you have to do is setting the ball X and Y position on the screen.
Was This Post Helpful? 0
  • +
  • -

#5 nyro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-October 10

Re: I need help with JButton

Posted 15 November 2010 - 02:16 AM

How would I be able to tell if the ball hit the paddle?
Was This Post Helpful? 0
  • +
  • -

#6 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: I need help with JButton

Posted 15 November 2010 - 03:05 AM

you can use the intersects method of the Rectangle class.
http://download.orac....awt.Rectangle)
create a Rectangle object of the ball, and of the paddle.
you can then check if they collide using:
if(ball.getRectangle().intersects(paddle.getRectangle()){
  //do whatever needs to be done if the ball hits the paddle
}


you will have to write the getRectangle method.

This is a very simple solution, and not the best one, because you'll have also to know what die of the paddle the ball hit (to know if to change dy or dx).
Try that, and if it does not suit you, try the more advanced option:
check those links for ideas:
http://stackoverflow...lision-handling
http://beginwithjava...cing-right.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1