Hallo zusammen
Ich melde mich jetzt hier einmal in der Hoffnung dass ihr mir ein wenig auf die Sprünge helfen könnt.
Ich bin sicher schon 20h an diesem "Projekt" dran und habe es schon mehrmals umgeschrieben.
Das Ziel ist es ein Pong- Spiel zu erstellen.
Zuerst habe ich es mit dem Tutorial Java Pong Tutorial gemacht, was auch gut geklappt hat. Weil wir aber in der Schule die Classe Class BufferStrategy nicht kennen, wollte ich es noch mit den normalen Swing-Classen machen.
Dazu habe ich das Graphics2D benutzt, um meine Objekte zu zeichnen.
1. Problem war schon, zwei unabhänige Objekte (Paddle und Ball) zu zeichnen, weil ich zwei Klassen habe, eine Ball.java und ein Paddle.java. Mit dem Graphics braucht es zwangsläufig diese Methode:
Aus diesem Grund musste ich meine Klassen ein bisschen umbauen und habe somit eine Klasse "DrawContent" geschrieben, welche alle beide Objekte zeichnen soll. Dazu musste ich aber das ganze Gameplay (Methode: gameLoop) und ActionListener etc. in diese Klasse zügeln.
Und nun ist meine einfache Frage, wenn ich aber aus der Main Klasse, also "myPong.java" auf einen ActionListener-Methode aus "drawContent.java" zugreifen will, reicht dass nicht wenn ich diese Klasse, plus Methode public mache? Ich kann einfach nicht das Objekt zugreifen:
Was ist hier mein Problem? Ich habe bestimmt schon alles auf den Kopf gestellt, damit ich diesen Berechtigungsprobemen aus dem Weg gehen kann. Eine Möglichkeit wäre natürlich, alles in eine Klasse zu schreiben mit "private" Klassen. Dass möchte ich aber eigentlich nicht..
Hier nun mein Code, dieser Funktioniert momentan so nicht, aber wenn ihr noch anderes seht was nicht gut ist, bin ich für jede Kritik/Bemerkung dankbar.
Wie gesagt ich bin Anfänger und wir Programmieren in der Schule.
Hauptgame:
DrawContent:
Ball:
Paddle:
Ich melde mich jetzt hier einmal in der Hoffnung dass ihr mir ein wenig auf die Sprünge helfen könnt.
Ich bin sicher schon 20h an diesem "Projekt" dran und habe es schon mehrmals umgeschrieben.
Das Ziel ist es ein Pong- Spiel zu erstellen.
Zuerst habe ich es mit dem Tutorial Java Pong Tutorial gemacht, was auch gut geklappt hat. Weil wir aber in der Schule die Classe Class BufferStrategy nicht kennen, wollte ich es noch mit den normalen Swing-Classen machen.
Dazu habe ich das Graphics2D benutzt, um meine Objekte zu zeichnen.
1. Problem war schon, zwei unabhänige Objekte (Paddle und Ball) zu zeichnen, weil ich zwei Klassen habe, eine Ball.java und ein Paddle.java. Mit dem Graphics braucht es zwangsläufig diese Methode:
Java:
public void paintComponent(Graphics g){
setOpaque(false);
super.paintComponent(g);
Aus diesem Grund musste ich meine Klassen ein bisschen umbauen und habe somit eine Klasse "DrawContent" geschrieben, welche alle beide Objekte zeichnen soll. Dazu musste ich aber das ganze Gameplay (Methode: gameLoop) und ActionListener etc. in diese Klasse zügeln.
Und nun ist meine einfache Frage, wenn ich aber aus der Main Klasse, also "myPong.java" auf einen ActionListener-Methode aus "drawContent.java" zugreifen will, reicht dass nicht wenn ich diese Klasse, plus Methode public mache? Ich kann einfach nicht das Objekt zugreifen:
Java:
MouseReset ml1 = new MouseReset();
MouseStartStop ml2 = new MouseStartStop();
Was ist hier mein Problem? Ich habe bestimmt schon alles auf den Kopf gestellt, damit ich diesen Berechtigungsprobemen aus dem Weg gehen kann. Eine Möglichkeit wäre natürlich, alles in eine Klasse zu schreiben mit "private" Klassen. Dass möchte ich aber eigentlich nicht..
Hier nun mein Code, dieser Funktioniert momentan so nicht, aber wenn ihr noch anderes seht was nicht gut ist, bin ich für jede Kritik/Bemerkung dankbar.
Wie gesagt ich bin Anfänger und wir Programmieren in der Schule.
Hauptgame:
Java:
package _12_PongSpiel.myPong;
import _12_PongSpiel.myPong.Ball;
import _12_PongSpiel.myPong.Paddle;
import _12_PongSpiel.myPong.DrawContent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* Beispiel von Tutorial:
* [url=http://www.giosoft.net/Development/Java-Pong-Tutorial.html]Java Pong Tutorial[/url]
*/
public class myPong extends JFrame {
/**
* @author Georgi Khomeriki
*/
public final static int windowWidth = 800;
public final static int windowHeight = 600;
private int paddleWidth = 150;
private int paddleHeight = 15;
private int ballWidth = 20;
private int ballHeight = 20;
private int initSpeedx = 5;
private int initSpeedy = 5;
private JPanel gamePanel = new JPanel();
private JPanel showPanel = new JPanel();
private JButton buReset = new JButton("reset");
private JButton buStart = new JButton("start/stop");
public Ball ball_tmp;
private Paddle paddle_tmp;
public DrawContent drawPanel;
public JLabel looseCount = new JLabel("Looses: 0");
public myPong() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(windowWidth, windowHeight);
this.setResizable(false);
this.setLocation(100, 100);
Container frame = this.getContentPane();
frame.setLayout(new BorderLayout());
//layout.collapse;
initGame();
showPanel.setLayout(new FlowLayout());
showPanel.add(buReset);
showPanel.add(buStart);
showPanel.add(looseCount);
showPanel.setBackground(Color.cyan);
gamePanel.setBackground(Color.PINK);
//gamePanel.add(ball);
//gamePanel.add(reset);
//ball.setBackground(Color.PINK);
frame.add(showPanel,BorderLayout.NORTH);
//frame.add(gamePanel,BorderLayout.CENTER);
frame.add(drawPanel,BorderLayout.CENTER);
this.setVisible(true);
//Create MouseListener
//paddleMouseMover = new PaddleAction();
//add MouseListener to JFrame
//this.addMouseListener(paddleMouseMover);
//addMouseMotionListener(paddleMouseMover);
MouseReset ml1 = new MouseReset();
MouseStartStop ml2 = new MouseStartStop();
buReset.addMouseListener(ml1);
buStart.addMouseListener(ml2);
keylistenerclass kl = new keylistenerclass();
addKeyListener(kl);
// this.addKeyListener(kl);
playPong();
}
private void initGame() {
// all you're game variables should be initialized here
// //the ball comes in the middle of the screen with the speed 1
ball_tmp = new Ball(windowWidth/2,windowHeight/2,initSpeedx, initSpeedy, ballWidth, ballHeight);
// //ball = new Ball(325,560,initSpeedx, initSpeedy);
//
// //Creates the paddle
paddle_tmp = new Paddle(windowWidth/2-75, windowHeight-40, paddleWidth, paddleHeight);
//
drawPanel = new DrawContent(ball_tmp, paddle_tmp, drawPanel.getWidth(), drawPanel.getHeight());
// content = new DrawContent(windowWidth/2, windowHeight/2, ballWidth, ballHeight, windowWidth/2-75, windowHeight-40, paddleWidth, paddleHeight);
}
public class keylistenerclass implements KeyListener
{
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println("key pressed" + arg0);
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println("key pressed" + arg0);
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println("key pressed" + arg0);
}
}
/* public class PaddleAction implements MouseMotionListener // MouseListener
{
int k = 0;
private int oldX, oldY, newX, newY;
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent me) {
newX = me.getXOnScreen();
newY = me.getYOnScreen();
if (newX != oldX)
{ paddle.x = me.getX(); }
if (newY != oldY)
{ paddle.y=me.getY(); }
//System.out.println("Mouse entered: " + k + " Location on Screen: " + me.getLocationOnScreen() + " getXOnScreen: " + me.getXOnScreen() + " -- getX: " + me.getX());
oldX = me.getXOnScreen();
oldY = me.getYOnScreen();
//Tell the System to do the Drawing now, otherwise it can take a few extra ms until
//Drawing is done which looks very jerky
Toolkit.getDefaultToolkit().sync();
}
}*/
}
DrawContent:
Java:
package _12_PongSpiel.myPong;
import _12_PongSpiel.myPong.myPong;
import _12_PongSpiel.myPong.Ball;
import _12_PongSpiel.myPong.Paddle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
public class DrawContent extends JComponent {
private Ball ball;
private Paddle paddle;
int counter = 0;
int counterLooses = 0;
private boolean runVar = true;
private int ballX, ballY, ballWidth, ballHeight, ballSpeedX, ballSpeedY, paddleX, paddleY, paddleWidth, paddleHeight;
private int gameScreenWidth, gameScreenHeight;
//DrawContent(int ballX, int ballY, int ballWidth, int ballHeight, int paddleX, int paddleY, int paddleWidth, int paddleHeight)
DrawContent(Ball b, Paddle p, int sw, int sh)
{
this.ballX = b.x;
this.ballY = b.y;
this.ballWidth = b.width;
this.ballHeight = b.height;
this.ballSpeedX = b.speedx;
this.ballSpeedY = b.speedy;
this.paddleX = p.x;
this.paddleY = p.y;
this.paddleWidth = p.width;
this.paddleHeight = p.height;
//Height and Width from CENTER from BorderLayout (ungleich windowHeigth, wWidth)
this.gameScreenWidth = sw;
this.gameScreenHeight = sh;
}
public void paintComponent(Graphics g){
setOpaque(false);
super.paintComponent(g);
//the ball comes in the middle of the screen with the speed 1
ball = new Ball(this.ballX, this.ballY, this.ballSpeedX, this.ballSpeedY, this.ballWidth, this.ballHeight);
//ball = new Ball(325,560,initSpeedx, initSpeedy);
//Creates the paddle
paddle = new Paddle(this.paddleX-75, paddleY-40, this.paddleWidth, this.paddleHeight);
//Graphics2D g2 = (Graphics2D) g;
//Graphics2D bg = (Graphics2D) g;
g.setColor(Color.GREEN);
g.fillOval(ball.x, ball.y, ball.width, ball.height); //ball.x gives the x- coordinate of the actual ball
//because the ball is moving
g.setColor(Color.BLACK);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height); //ball.x gives the x- coordinate of the actual ball
//because the ball is moving
//braucht es das??
g.dispose();
}
public void playPong()
{
while(runVar) {
long start = System.currentTimeMillis();
gameLoop();
while(System.currentTimeMillis()-start < 5) {
//do nothing
}
}
}
private void gameLoop() {
// your game logic goes here
// System.out.println( "ball.x: "+ ball.x + " ball.y: " + ball.y );
//System.out.println( "paddle.x: "+ paddle.x + " paddle.y: " + paddle.y + " -- paddleWidth:" + paddleWidth + " paddleHeight:" + paddleHeight);
//startet diagonal nach unten rechts
ball.x = ball.x + ball.speedx;
ball.y = ball.y + ball.speedy;
if ( ball.x < 0 || ball.x > gameScreenWidth-ballWidth )
{
//ändert das vorzeichen und die Richtung des Balles!
ball.speedx = -ball.speedx;
}
if ( ball.y-20 < 0 || ball.y > gameScreenHeight-ballHeight )
{
//ändert das vorzeichen und die Richtung des Balles!
ball.speedy = -ball.speedy;
}
if ( ball.y >= gameScreenHeight-ballHeight )
{
counterLooses++;
looseCount.setText("Looses: "+ counterLooses);
repaint();
}
if (paddle.x <= ball.x && paddle.x+paddleWidth >= ball.x
&& paddle.y <= ball.y && paddle.y+paddleHeight >= ball.y)
{
counter++;
System.out.println("counter: " + counter);
//ball.speedx = -ball.speedx;
ball.speedy = -ball.speedy;
}
// so much for game logic now let's draw already!
repaint();
}
public class MouseStartStop implements MouseListener{
public void mouseClicked(MouseEvent arg0) {
if(runVar)
{
runVar=false;
}
else
{
runVar = true;
playPong();
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public class MouseReset implements MouseListener{
public void mouseClicked(MouseEvent arg0) {
looseCount.setText("Looses: "+ 0);
counterLooses = 0;
//initGame();
repaint();
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
}
Ball:
Java:
package _12_PongSpiel.myPong;
import _12_PongSpiel.myPong.Paddle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
/*
*
* To do this we start by creating a class that holds our ball object.
* This ball object will have variables x, y, dx and dy. So x and y
* stand for the location of the ball, dx is the speed of the ball
* in the x direction and dy is the speed of the ball in the y direction.
*
* We name our class Ball.java (btw don't kill me for using public variables).
*
* */
public class Ball{
public int x, y, speedx, speedy, width, height;;
public Ball (int x, int y, int sx, int sy, int width, int height)
{
this.x = x; //x- achse
this.y = y; //y- achse
this.speedx = sx; //Speed of the ball in x direction
this.speedy = sy; //Speed of the ball in y direction
this.width= width;
this.height=height;
}
/*public void paintComponent(Graphics g){
setOpaque(false);
super.paintComponent(g);
//Graphics2D g2 = (Graphics2D) g;
//Graphics2D bg = (Graphics2D) g;
g.setColor(Color.GREEN);
g.fillOval(this.x, this.y, this.width, this.height); //ball.x gives the x- coordinate of the actual ball
//because the ball is moving
//g.setColor(Color.BLACK);
//g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height); //ball.x gives the x- coordinate of the actual ball
//because the ball is moving
//bg.setBackground(Color.PINK);
//bg.fillRect(1, 1, 300, 400);
//braucht es das??
g.dispose();
//bg.dispose();
}
*/
}
Paddle:
Java:
package _12_PongSpiel.myPong;
import java.awt.Color;
import java.awt.Graphics;
public class Paddle {
int x, y, width, height;
public Paddle(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}