Sat Feb 16, 2008 8:37 am
package javaapplication4;
import java.awt.*;
////////// BallModel
public class Ball {
//... Constants
final static int DIAMETER = 21;
public Image buffer; // The off-screen image for double-buffering
public Graphics bufferGraphics; // A Graphics object for the buffer
//... Instance variables
private int m_x; // x and y coordinates upper left
private int m_y;
private int typeColor;
private int m_velocityX; // Pixels to move each time move() is called.
private int m_velocityY;
private int m_rightBound; // Maximum permissible x, y values.
private int m_bottomBound;
//======================================================== constructor
public Ball(int x, int y, int velocityX, int velocityY) {
m_x = x;
m_y = y;
m_velocityX = velocityX;
m_velocityY = velocityY;
}
//======================================================== setBounds
public void setBounds(int width, int height) {
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}
//============================================================== move
public void move() {
//... Move the ball at the give velocity.
m_x += m_velocityX;
m_y += m_velocityY;
//... Bounce the ball off the walls if necessary.
if (m_x < 0) { // If at or beyond left side
typeColor=1;
m_x = 0; // Place against edge and
m_velocityX = -m_velocityX; // reverse direction.
} else if (m_x > m_rightBound) { // If at or beyond right side
m_x = m_rightBound; // Place against right edge.
typeColor=2;
m_velocityX = -m_velocityX; // Reverse direction.
}
if (m_y < 0) { // if we're at top
m_y = 0;
typeColor=3;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) { // if we're at bottom
m_y = m_bottomBound;
typeColor=4;
m_velocityY = -m_velocityY;
}
}
//============================================================== draw
public void draw(Graphics g) {
if(typeColor==1){
g.setColor(Color.BLUE);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==2){
g.setColor(Color.GREEN);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==3){
g.setColor(Color.YELLOW);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==4){
g.setColor(Color.RED);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
}
//==== getDiameter, getX, getY
public int getDiameter() { return DIAMETER;}
public int getX() { return m_x;}
public int getY() { return m_y;}
//===-======= setPosition
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
/*
* BallInBox.java
*/
package javaapplication4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//////BouncingBall
public class BallInBox extends JPanel {
//============================================== fields
//... Instance variables representing the ball.
private Ball m_ball = new Ball(0, 0, 2, 3);
private Ball mm_ball = new Ball(200, 80,2,3);
//... Instance variables for the animiation
private int m_interval = 35; // Milliseconds between updates.
private Timer m_timer; // Timer fires to anmimate one step.
//======= constructor
/** Set panel size and creates timer. */
public BallInBox() {
setPreferredSize(new Dimension(200, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}
//========setAnimation
/** Turn animation on or off.
*@param turnOnOff Specifies state of animation.
*/
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start(); // start animation by starting the timer.
} else {
m_timer.stop(); // stop timer
}
}
//===== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, border
m_ball.draw(g);
mm_ball.draw(g);// Draw the ball.
}
/////// inner listener class ActionListener
class TimerAction implements ActionListener {
//========== actionPerformed
/** ActionListener of the timer. Each time this is called,
* the ball's position is updated, creating the appearance of
* movement.
*@param e This ActionEvent parameter is unused.
*/
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight());
m_ball.move(); // Move the ball.
mm_ball.setBounds(getWidth(),getHeight());
mm_ball.move();
repaint(); // Repaint indirectly calls paintComponent.
}
}
}//endclass
Mon Jan 21, 2013 1:15 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.