2

I have the following code... (plus a generic Player class with getX, getY, setX, setY, render(),tick() etc. not included)

I want player to load in ViewB and when it hits the edge.. I'd like for ViewB to close and be replaced by ViewA or ViewC (depending on if its the left edge hit or right edge)... I can't seem to get this done and I don't know what I am doing wrong.... Player class is created in MainGameThread.java ... and gets passed onto new JPanel so that I am still working with original Player object/sprite that was created.. The
System.out.println("Should change screens here to ViewA"); command does not seem to fire when it hits the edge.. what am I doing wrong here???? and what class do I have to fix??

MainGameThread Class:

import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainGameThread extends JFrame {

    private LinkedList<JPanel> screens = new LinkedList<JPanel>();
    private ViewA va;
    private ViewB vb;
    private ViewC vc;
    private ViewD vd;
    private Player player;

    public MainGameThread() {
        player = new Player();
        // load player settings from server
        //  ..
        //
        //  va = new ViewA(player);
        //vc = new ViewC(player);
        //vd = new ViewD(Player)

        vb = new ViewB(player);

        screens.add(0,ViewB);
        screens.add(1,ViewA);
        screens.add(2,ViewB);
        screens.add(3,ViewC);

        add(screens.get(0)); // starting from view B

        //go to end of view B (x=0), change to View A, close view B

        if(player.getX()<=5){
            System.out.println("Should change screens here to ViewA");
            vb = null;
            va = new ViewA(player);
            remove(screens.get(0)); 

            add(screens.get(1));
        } 

        if(player.getX()>=615){
            System.out.println("Should change screens here to ViewC");
            vb = null;
            vc = new ViewC(player);
            remove(screens.get(0)); 

            add(screens.get(2));
        } 
        setSize(652, 480);
        setLocationRelativeTo(null);
        setTitle("GAME");
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainGameThread();
    }
}

JPanel View: (replace ViewA with ViewB, etc.. to D)

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;


public class ViewA extends JPanel implements ActionListener {

    private Player player;
    private Timer timer;
    private int B_WIDTH;
    private int B_HEIGHT;
    private boolean ingame;

    public ViewA(Player player) {   
        this.player = player;

        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.RED);
        setDoubleBuffered(true);
        setSize(652, 480);
        ingame = true;


        timer = new Timer(5, this);
        timer.start();
    }

    public void addNotify() {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();   

    }

    public void paint(Graphics g) {
        super.paint(g);

        if (ingame) {

            Graphics2D g2d = (Graphics2D)g;

            if (player.isVisible())
                g2d.drawImage(player.getImage()
                    , player.getX(), player.getY(), this);

            g2d.setColor(Color.BLACK);
            g2d.drawString("ViewA: ", 5, 15);           
        } 

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        player.move();
        checkCollisions();
        repaint();  
    }

    public void checkCollisions() {
    }

    private class TAdapter extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
user2556304
  • 159
  • 2
  • 15

1 Answers1

5
  • use CardLayout for switching betweens views (ViewA, ViewB, ViewC, ViewD), otherwise you have to call (re)validate and repaint for JFrame (as last code lines, after all changes to the already visible Swing GUI is executed)

  • custom painting is done by paintComponent() instead of paint()

  • remove void public void addNotify() { and instead of this void to override getPreferredSize, then all coordinates for Objects in JPanel are based on getWeight/Height

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • "remove void public void addNotify() { and instead of this void to override getPreferredSize, then all coordinates for Objects in JPanel are based on getWeight/Height" -- so remove addNotify() altogether? Or do this `public void addNotify() { super.addNotify(); B_WIDTH = getWidth(); B_HEIGHT = getHeight(); super.setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT)); }` – user2556304 Jul 09 '13 at 06:48
  • remove whole code block, there isn't reason anything to notify, please to try linked code by mouse click to getPreferredSize, then you can to compare, btw search here paintComponent + getPreferredSize, order by newest, then you can to see a few miracles – mKorbel Jul 09 '13 at 06:51