2011-01-14 7 views
0

Dies ist eine Excerise, die ich für einen Uni-Kurs absolvieren muss, es ist keine markierte Aufgabe und ich könnte mit ein bisschen Hilfe tun. Ich kann den Ball dazu bringen, auf dem Bildschirm zu erscheinen und von den Seiten zu springen, es spielt im Moment keine Rolle, ob er durch den Boden des Bildschirms fällt und ich kann das Paddel zu verschiedenen Zeiten auf dem Bildschirm erscheinen lassen, aber ich kann sie nicht bekommen beide gleichzeitig erscheinen. Hilfe bitteHilfe beim Hinzufügen eines Paddle zu einem JFrame

Hier sind meine Klassen

Mainclass

package movingball; 

public class Main 
{ 
    public static void main (String []args) 
    { 
     MovingBall world = new MovingBall("Moving Ball"); 
     world.setVisible(true); 
     world.move(); 
    } 
} 

BallClass

package movingball; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 

public class Ball 
{ 
    private final int RADIUS = 10; 

    private Point pos; 
    private Color ballColour; 
    private int yChange = 2; 
    private int xChange = 1; 
    private int height, width; 
    private int change; 

    public Ball (int frameWidth, int frameHeight) 
    { 
     change = 3; 
     ballColour = Color.RED; 
     width = frameWidth; 
     height = frameHeight; 
     pos = new Point(); 
     pos.x = (int)(Math.random() * (width - RADIUS)) + RADIUS; 
     pos.y = (int)(Math.random() * (height/2 - RADIUS)) + RADIUS; 
    } 

    //There are lots of ways you can updateBallState 
    //Note that the menu bar occupies some of the visible space 
    public void move() 
    { 
     if(pos.y < RADIUS) 
     { 
      yChange = - yChange; 
     } 
     if(pos.x < RADIUS) 
     { 
      xChange = -xChange; 
     } 
     if(pos.x > width - RADIUS) 
     { 
      xChange = -xChange; 
     } 
     if(pos.y < height - RADIUS) 
     { 
      pos.translate(xChange, yChange); 
     } 
     if(pos.x < width - RADIUS) 
     { 
      pos.translate(xChange, yChange); 
     } 
    } 

    public void updateBallState() 
    { 
     if (pos.y + change < height - 3*RADIUS) 
     { 
      pos.translate(0, change); 
      // change++; //accelerate 
     } 
    } 

    //This method can be called with a provided graphics context 
    //to draw the ball in its current state 
    public void draw(Graphics g) 
    { 
     g.setColor(ballColour); 
     g.fillOval(pos.x - RADIUS, pos.y - RADIUS, 2*RADIUS, 2*RADIUS); 
    } 

    public void bounce() 
    { 
     yChange = -yChange; 
     pos.translate(xChange, yChange); 
    } 

    public Point getPosition() 
    { 
     return pos; 
    } 
} 

Ballgame

package movingball; 

import java.awt.Graphics; 
import java.awt.event.*; 

public class BallGame extends MovingBall 
{ 

    private Paddle myPaddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT); 


    public BallGame(String title) 
    { 
     super(title); 
     addKeyListener(new KeyList());  
    } 

    public void paint(Graphics g) 
    { 
     super.paint(g); 
     myPaddle.paint(g); 
     if(isContact()) 
     { 
      myBall.bounce(); 
     } 
     else 
     { 
      myPaddle.paint(g); 
     } 
    } 

    public boolean isContact() 
    { 
     if (myPaddle.area().contains(myBall.getPosition())) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    public class KeyList extends KeyAdapter 
    { 
     public void keyPressed(KeyEvent k) 
     { 
      if(k.getKeyCode() == KeyEvent.VK_LEFT) 
      { 
      myPaddle.moveLeft(); 
      } 
      if(k.getKeyCode() == KeyEvent.VK_RIGHT) 
      { 
      myPaddle.moveRight(); 
      } 
     } 
    } 
} 

MovingBall Klasse

package movingball; 

import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class MovingBall extends JFrame 
{ 
    protected final int FRAME_WIDTH = 240; 
    protected final int FRAME_HEIGHT = 320; 
    protected Ball myBall = new Ball(FRAME_WIDTH, FRAME_HEIGHT); 

    public MovingBall (String title) 
    { 
     super(title); 

     setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    } 

    public void paint(Graphics g) 
    { 
     super.paint(g); 
     myBall.draw(g); 
    } 

    public void move() 
    { 
     while(true) 
     { 
      myBall.move(); 
      repaint(); 
      try 
      { 
      Thread.sleep(50); 
      } 
      catch(InterruptedException e) 
      { 
      System.exit(0); 
      } 
     } 
    } 
} 

Paddle Klasse

package movingball; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class Paddle 
{ 
    private Color paddleColour = Color.GREEN; 
    private int x,y; 
    private int paddleWidth = 20; 
    private int paddleHeight = 10; 
    private int move = 5; 


    /** Creates a new instance of Paddle */ 
    public Paddle(int frameWidth, int frameHeight) 
    { 

     x =(int)(Math.random()*(frameWidth - paddleWidth)); 
     y = frameHeight - paddleHeight; 

    } 

    public void moveRight() 
    { 
     x = x + move; 
    } 

    public void moveLeft() 
    { 
     x = x - move; 
    } 

    public Rectangle area() 
    { 
     return new Rectangle(x,y, paddleWidth, paddleHeight); 
    } 

    public void paint(Graphics g) 
    { 
     g.setColor(paddleColour); 
     g.fillRect(x,y,paddleWidth, paddleHeight); 
    } 
} 

Antwort

1

Hier sind ein paar Hinweise, um loszulegen. Ich habe viele Dinge vorzuschlagen, aber ich sage das nur, um dich weiter zu bringen, als du jetzt bist. Sie möchten, dass Ihr JFrame doppelt gepuffert wird. Das ist der erste Schritt. Dazu erstellen, nachdem eine neue Pufferstrategie für Ihre JFrame sichtbar zu machen:

world.setVisible(true); 
world.createBufferStrategy(2); 

Was, warum Ihr Paddel ist nicht die Malerei? Du wirst dich selbst treten. Schauen Sie sich diese Zeile:

MovingBall world = new MovingBall("Moving Ball"); 

Sie sind nicht wirklich eine BallGame zu schaffen, das ist, wo die Logik für die Malerei das Paddel enthalten ist! (BallGame.paint()) Versuchen:

BallGame world = new BallGame("Moving Ball"); 
+0

Das ist ein wichtiger :(es war die einzige Klasse i auf Grund sehen i did not angenommen, richtig wäre und ich vergaß die neue Ballspiel-Klasse hinzugefügt. – user445714

Verwandte Themen