mercredi 20 mai 2015

PingPongBall wird nicht angezeigt

Hallo liebe java-forum- community,

aufregung! mein erster Beitrag:)

zu meinem problem: ich bin gerade dabei ein simples pingpong zu programmieren.
dazu habe folgende Klassen: Display, Ball und die Panels

Mein Problem ist nun, dass ich in der Displayklasse voher folgende Zeile hatte:
g.drawImage(ball, ( PingPong.getXSIZE() / 2 ) - ball.getWidth() , ( PingPong.getYSIZE() / 2 ) - ball.getHeight() , null);
welches mir den ball in der Mitte des spielfeldes angezeigt hatte.

da ich aber für die bewegung des Balls mit xpos und ypos klar machen möchte, wurde
folgende Zeile draus: g.drawImage(ball, ball.xpos, ball.ypos, null);

Nun die panels werden noch angezeigt. der Ball allerdings nicht. Auch eine Ausgabe von ball.xpos und ball.ypos ergibt sinnvolle werte.

hat jemand ne idee woran es liegen könnte, dass die XY-Werte in ordnung sind aber der ball dennnoch nicht gezeichnet wird?

mfg dichter

Java Code:

  1. import java.awt.Graphics;
  2.  
  3. import javax.swing.JPanel;
  4.  
  5. public class Display extends JPanel{
  6.  
  7. Ball ball;
  8. Panel panel_player1, panel_player2;
  9.  
  10. public Display(){
  11.  
  12. ball = new Ball();
  13. panel_player1 = new Panel();
  14. panel_player2 = new Panel();
  15.  
  16. }
  17.  
  18. public void paintComponent(Graphics g){
  19.  
  20. //Spielfläche
  21. g.drawRect(0, 0, PingPong.getXSIZE() - 1 , PingPong.getYSIZE() - 1 );
  22. //Ball
  23. g.drawImage(ball, ball.xpos, ball.ypos, null);
  24. System.out.println(ball.xpos + " " + ball.ypos);
  25. //Panels
  26. g.drawImage(panel_player1, 0 , ( PingPong.getYSIZE() / 2 ) - panel_player1.getHeight() , null);
  27. g.drawImage(panel_player2, PingPong.getXSIZE() - panel_player1.getWidth(), (PingPong.getYSIZE() / 2 ) - panel_player2.getHeight() , null);
  28.  
  29. }


Klasse Ball
Java Code:

  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3.  
  4. import javax.imageio.ImageIO;
  5.  
  6. public class Ball extends BufferedImage {
  7.  
  8. public int xpos = 0, ypos = 0;
  9. public int speed = 3;
  10.  
  11. public Ball() {
  12. super(48, 47, BufferedImage.TYPE_INT_ARGB);
  13.  
  14. xpos = ( PingPong.getXSIZE() / 2 ) - this.getWidth();
  15. ypos = ( PingPong.getYSIZE() / 2 ) - this.getHeight();
  16.  
  17. try {
  18. ImageIO.read(getClass().getResource("ball.png") );
  19.  
  20. //weiß transparent machen
  21. for (int i = 0; i < getWidth() ; i++) {
  22. for (int j = 0; j < getHeight(); j++) {
  23.  
  24. int rgb = getRGB(i, j);
  25.  
  26. int alpha = (rgb & 0xFF000000) >>> 24;
  27. int red = (rgb & 0x00FF0000) >>> 16;
  28. int green = (rgb & 0x0000FF00) >>> 8;
  29. int blue = (rgb & 0x000000FF) >>> 0;
  30.  
  31. if (red == 255 && green == 255 && blue == 255) {
  32. int new_rgb = (0 << 24) | (red << 16) | (green << 8) | blue;
  33. setRGB(i, j, new_rgb);
  34.  
  35. }
  36. }
  37. }
  38.  
  39. } catch (IOException e) {
  40.  
  41. e.printStackTrace();
  42. }
  43.  
  44. }
  45. }


PingPongBall wird nicht angezeigt

0 commentaires:

Enregistrer un commentaire