-3

I have a single frame and multiple panels, which implement draw operation . I want to switch between them. I am using cardLayout, but am unable to do so. Frame code :

import java.awt.CardLayout;
import javax.swing.SwingUtilities;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MyFrame extends JFrame implements ActionListener {

    JPanel pp=new JPanel();
    GamePanel g=null;
    GamePanel1 g1=null;
    GamePanel2 g2=null;
    GamePanel3 g3=null;
    GamePanel4 g4=null;
    GamePanel5 g5=null;
    CardLayout c1=new CardLayout();
    public MyFrame()
    {
        setTitle("Deadly Combat");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800,500);
        initComponents();
        new Timer(50,this).start();

    }
    public void initComponents()
    {
        g=new GamePanel(this);
        g1=new GamePanel1(this);
        g2=new GamePanel2(this);
        g3=new GamePanel3(this);
        g4=new GamePanel4(this);
        g5=new GamePanel5(this);
        Menu m = new Menu(g1,g3,g2,this);
        FighterChoice fc = new FighterChoice(g3,g,this);
        HelpMove hm = new HelpMove(g2,g1,this);
        Fighter1 f=new Fighter1(g,750,250);
        Fighter2 f2=new Fighter2(g,50,250);
        g3.arr.add(fc);
        g1.arr.add(m);
        g2.arr.add(hm);
        g.arr.add(f);
        g.arr.add(f2);
        addKeyListener(fc);
        addKeyListener(m);
        addKeyListener(hm);
        addKeyListener(f);
        addKeyListener(f2);
        pp.setLayout(c1);
        pp.add(g,"1");
        pp.add(g1,"2");
        pp.add(g2,"3");
        pp.add(g3,"4");
        pp.add(g4,"5");
        add(g1);
        c1.show(pp,"2");
        add(pp);
        //add(g1);

    }
    public static void main(String[] args) {
        MyFrame f=new MyFrame();
        f.setVisible(true);
        f.setResizable(false);
        //f.g1.as.playClip(0, true, 6);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      g1.repaint();  
      g2.repaint();
      g.repaint();
      g3.repaint();

    }

}

Here is how my panel code look like :

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.util.*;
import javax.swing.*;

public class GamePanel1 extends JPanel {

    Image img = null; 
    AudioSink as=null;
    List<ObjectIntf> arr=new ArrayList<ObjectIntf>();
    MyFrame f;

public GamePanel1(MyFrame f){

    this.f = f;
    img = new ImageIcon(getClass().getResource("combat3.jpg")).getImage();
    as= new AudioSink();
    as.addAudioClip("map_1_bgsound");
    as.addAudioClip("mm_spawn_sound");
}  
@Override
protected void paintComponent(Graphics g)  
{
    super.paintComponent(g);

    g.drawImage(img,0,0,800,500,null);
    arr.size();
    for(ObjectIntf o : arr)
    {
        o.draw(g);
        System.out.println(arr.size());
    }
}

Here is how I want to switch between panels in the Menu function called from MyFrame:

private void select() {
    if(currentChoice == 0) {
        f.c1.show(f.pp, "4");
        f.add(f.pp);
    }
}

It doesn't work. Please help .

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
user3598542
  • 61
  • 1
  • 10

1 Answers1

1

My guess -- that your f.c1.show(f.pp, "4"); is working with the wrong MyFrame instance. You declare and create your visualized MyFrame instance in the main method, and by doing this its scope is limited to that main method only. A bad solution is to create a MyFrame f static field. A better solution is to give MyFrame its own show(String key) method that calls c1.show(this, key) internally, I would make sure that my menu or "Controller" class has a proper reference to the visualized MyFrame instance so that it can call its public show(String key) method.

Having said this, my answer is just a guess since your current code snippets don't let us fully understand just what your problem could be, and this is impeding your getting decent answers. If my suggestions don't help, then please help us to better understand exactly what is going on. We don't want to see all of your code, but we do want to see a small program that is a consolidation of your code and demonstrates your problem for us, a mcve.


Edit:
I now see in your code that you have:

Menu m = new Menu(g1,g3,g2,this);

that you're passing this or the current MyFrame instance into your Menu class, which kind of shoots my above guess to hades. In other words in all likelihood my guess above is wrong, sorry. Please post that mcve so we can better understand what might be wrong with your program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373