Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import javax.swing.JPanel;
- public class GamePanel extends JPanel{
- //First Ball Horizontal Movement with Collision Detection
- int fx = 0;
- int fy = 30;
- int firstXBallSpeed = 1;
- void moveFBall() {
- fx = fx + firstXBallSpeed;
- if(fx > 80 || fx < 1){
- firstXBallSpeed *= -1;
- }
- }
- //second ball Diagonal
- int sx = 100;
- int sy = 0;
- int secYBallSpeed = 1;
- int secXBallSpeed = 1;
- void moveSBall(){
- sy = sy + secYBallSpeed;
- sx = sx + secYBallSpeed;
- if(sy > 80 || sy < 0){
- secYBallSpeed *= -1;
- }
- }
- //second third Diagonal
- int tx = 230;
- int ty = 0;
- int thYBallSpeed = 1;
- void movetBall(){
- ty = ty + thYBallSpeed;
- if(ty > 80 || ty < 1){
- thYBallSpeed *= -1;
- }
- }
- double MAX_X = 30;
- double MAX_Y = 30;
- double angle = 0;
- int frballx = 0;
- int frbally = 0;
- void movefrBall(){
- //making angle lower will reduce its speed
- angle += 0.1;
- frballx = (int) (Math.cos(angle) * MAX_X);
- frbally = (int) (Math.sin(angle) * MAX_Y);
- //move the ball to desired location
- frballx += 330;
- frbally += 40;
- }
- //rectangular movement
- int fyy = 0;
- int fxx = 400;
- int fyyspeed = 1;
- int fxxspeed = 1;
- void moveftBall(){
- if(fyy == 0){
- fxx += fxxspeed;
- }else if (fyy == 80){
- fyyspeed = 0;
- fxxspeed = 1;
- fxx -= fxxspeed;
- }
- if(fxx == 480){
- fxxspeed = 0;
- fyy += fyyspeed;
- }
- if(fxx == 400){
- fyyspeed = 1;
- fyy -= fyyspeed;
- }
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- for(int i = 0; i < 5 ; i++){
- g2d.drawRect(i * 100, 0, 100, 100);
- }
- g2d.drawString("Horizontal", 20, 120);
- g2d.drawString("Diagonal", 120, 120);
- g2d.drawString("Vertical", 220, 120);
- g2d.drawString("Circular", 320, 120);
- g2d.drawString("Rectangular", 410, 120);
- g2d.setColor(Color.RED);
- //Draw 1st ball;
- g2d.fillOval(fx, fy, 20, 20);
- //Draw 2nd ball;
- g2d.fillOval(sx, sy, 20, 20);
- //Draw 3rd ball;
- g2d.fillOval(tx, ty, 20, 20);
- //Draw 4th ball;
- g2d.fillOval(frballx, frbally, 20, 20);
- //Draw 5th Ball
- g2d.fillOval(fxx, fyy, 20, 20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement