Domanda

I am working on a game right now and have been doing well so far, but I am having trouble right now. I have a JFrame. I added a JPanel to it. The JPanel consists of three JComponents that are supposed to draw pictures. Here is my code: (Note that not EVERYTHING from my methods are shown)

public class Game extends JFrame implements KeyListener {

public static Game g;

public JPanel pan;

Paddle p1;
Paddle p2;
Ball ball;

public int p1Y = 0;
public int p2Y = 0;

public int ballY = 300;

public Game() {
    setTitle("Game");
    setSize(600, 600);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setResizable(false);

    pan = new JPanel();
    pan.setBackground(Color.BLACK);
    p1 = new Paddle(0, p1Y);
    p2 = new Paddle(600, p2Y);
    ball = new Ball(300, ballY);
    pan.add(p1);
    pan.add(p2);

    add(pan);
    setVisible(true);

    pan.addKeyListener(this);
    pan.requestFocus();
}

public static void main(String args[]) {
    g = new Game();
}

This method sets everything up. I have two other methods that will draw the images.

class Paddle extends JComponent {

public Image img = new ImageIcon("resources/paddle.png").getImage();

public int x;
public int y;

public Paddle(int x, int y) {
    this.x = x;
    this.y = y;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, x, y, this);
}
}




class Ball extends JComponent {

public Image img = new ImageIcon("resources/ball.png").getImage();

public int x;
public int y;

public Ball(int x, int y) {
    this.x = x;
    this.y = y;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, x, y, this);
}
}

I made it print whenever the paintComponent was called and it did print. The only thing is is that it won't paint it on the panel. My only guess is that the constructor is stopping it from painting.

È stato utile?

Soluzione

There are a few basic problems...

  1. JPanel by default, uses a FlowLayout
  2. FlowLayout uses the preferred size of the component to determine how to layout the components it is managing
  3. Your components don't override getPreferredSize which means they will return 0x0
  4. A JPanel is not focusable by default, so adding a KeyListener to it and requesting focus will do very little. You should consider using Key Bindings instead
  5. JLabel provides support for drawing Icon. You can wrap an Image in a ImageIcon add use this with a JLabel to paint images more simply
  6. ImageIcon("resources/ball.png") suggests that the images in question are stored as files on the disk, but the path suggests that they may be embedded resources. ImageIcon(String) expects the reference to be a file on the disk, which would be relative to the execution context of the program

Depending on what you want to achieve, you may wish to consider writing your own layout manager which you can use to control the location of the child components OR write a single "game surface" which you use custom painting to paint the game state

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top