سؤال

I have this piece of code for a pretty basic 2d platform game I'm working on that I just can't get to work. The game itself is working fine but for some reason the title screen won't appear. I managed to pin this down to the fact that the repaint() method wasn't doing anything when called from the title() method. No matter how I try to invoke it, causing an event/setting up another timer, the paintComponent method isn't doing anything, not even the window appears. Despite this the level() method, which seems pretty similar to me, does cause the window to appear and the paintComponent method to carry out.

I'm sorry about how ugly the code is, this is a bit of an experiment for me so it's a bit untidy at the moment. Any help on how to solve this problem would be much appreciated.

This is the code for the JPanel class, where I'm getting the problem.

public class Screen extends JPanel implements ActionListener{

int currentScreen;
Image title;
Image background;
Image ship;
Image black;
private Timer timer;
private Level lvl;

public Screen(){

    addKeyListener(new TAdapter());
    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    title();
    level();
}


public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    if(currentScreen == 0) {
        g2d.drawImage(title, 0, 0, this);
    }
    else if(currentScreen == 1){
        g2d.drawImage(background, 0, 0, this);
        g2d.drawImage(lvl.getAvImage(), lvl.getAvX(), lvl.getAvY(), this);
        g2d.setFont(new java.awt.Font("SansSerif", Font.PLAIN, 12));
        for(int i=0;i<4;i++)
            for(int j=0; j<4; j++){
                g2d.drawImage(lvl.getPlijImage(i,j), lvl.getPlijX(i,j), lvl.getPlijY(i,j), this);
                g2d.drawString(Integer.toString(lvl.getPlijCurrent(i,j)), lvl.getPlijX(i,j)+64, lvl.getPlijY(i,j)+16);

            }
        g2d.drawImage(spaceship, 0, 0, this);
        g2d.drawImage(black, 0, 736, this);
        g2d.setColor(Color.WHITE);
        g2d.drawString(lvl.getSqDisplay(), 450, 32);
    }
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void title(){

    ImageIcon ii = new ImageIcon(this.getClass().getResource("Title.png"));
    title = ii.getImage();
    currentScreen = 0;
    repaint(); //doesn't invoke paint
    Scanner reader = new Scanner(System.in); //not relevant, just getting it to stop before opening level
    int a=reader.nextInt();
}

public void level(){

    lvl = new Level(1);
    ImageIcon ii = new ImageIcon(this.getClass().getResource("Background.png"));
    background = ii.getImage();
    ii = new ImageIcon(this.getClass().getResource("Ship.png"));
    ship = ii.getImage();
    ii = new ImageIcon(this.getClass().getResource("Black.png"));
    black = ii.getImage();
    currentScreen = 1;
    timer = new Timer(10, this);
    timer.start();

}

public void actionPerformed(ActionEvent e) {
    lvl.actionPerformer(e);
    repaint(); //does invoke paintComponent, but not when I jump here from the title bit
}

private class TAdapter extends KeyAdapter {

    public void keyReleased(KeyEvent e) {
        if(currentScreen == 1) lvl.keyReleased(e);
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(currentScreen == 1) lvl.keyPressed(e);
        else if((currentScreen == 0)&&(key == KeyEvent.VK_ENTER)){
            onTitle = false;
        }
    }
}

}

هل كانت مفيدة؟

المحلول

Scanner reader = new Scanner(System.in); //not relevant, just getting it to stop before opening level
int a=reader.nextInt();

I beg to differ. This is (most likely) why you're having problems.

These two simple lines are preventing the Event Dispatching Thread from processing any new events, including repaint requests. This means, that until the title method returns, no new paints will occur

Take a look at Concurrency in Swing for more details.

There are any number of possible solutions, but you'll need to provide more details about what it is you are trying to achieve before any of them will make sense...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top