javaswingjpaneljcomponentborder-layout

How i can paint in the Panel with two difference class?


I'm working on a project in Java, i must to build a clone of Leemings game and i have a probleme when i try to build the world:

I have created the class "fenetre" (windows in english), the class Leemings and the class Carreau (the plateform where the leemings can walk)

Here is the problem, i have draw one lemmings on panel and i want also draw the plateform where the leemings could walk in the same panel but i can't draw in the same panel so i can see only the plateform but i do not see anymore the leemings.

I share here the code:

package lemmingsLight;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;

public class Fenetre extends JComponent {
    
    public Fenetre() {
        
        
        // Prepare a panel to display the lemmings
        JPanel lemmings = new JPanel();
        lemmings.setLayout(new BorderLayout());
        lemmings.setBounds(0, 0, 500, 250); 
        lemmings.setBackground(Color.WHITE);
        add(lemmings);
    

        // Add the lemmings to the panel
        Lemmings myTestLemming = new Lemmings();
        lemmings.add(myTestLemming, BorderLayout.CENTER);
        
        // Add the plateform to the same panel
        Carreau pf = new Carreau();
        lemmings.add(pf);
        
        MouseAdapter m = new MyMouse(this);
        addMouseListener(new MyMouse(this));
        
        
        
        JPanel lave = new JPanel(); //fire -> if the leemings fall the leemings die (or burn)
        
        lave.setBounds(0, 250, 500, 75); 
        lave.setBackground(Color.RED);
        add(lave);
        
        JPanel panCom = new JPanel(); //Control or command

        panCom.setBounds(0, 320, 500, 200); 
        panCom.setBackground(Color.GRAY);
        add(panCom);
        
        JButton b1 = new JButton("pause");
        JButton b2 = new JButton("bloquer"); 
        JButton b3 = new JButton("creuser"); 
        JButton b4 = new JButton("tunnel");
        JButton b5 = new JButton("bombe");
        JButton b6 = new JButton("escalier"); 
        JButton b7 = new JButton("grimper");
        JButton b8 = new JButton("parachute");
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                //Lemmings.setState(NORMAL);
            }
        });
        panCom.add(b1);
        panCom.add(b2); 
        panCom.add(b3); 
        panCom.add(b4); 
        panCom.add(b5); 
        panCom.add(b6); 
        panCom.add(b7); 
        panCom.add(b8); 
        
    }
}

the class "Carreau":

package lemmingsLight;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

//Pour l'instant class inutile, on va reflechir de ce qu'on peut faire ce Carreau

public class Carreau extends JComponent{
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.BLUE);
        g.fillRect(55, 200, 200, 20);
        
    }
}

and the class Lemmings:

package lemmingsLight;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class Lemmings extends JComponent {
    private LemmingsState state;

    public LemmingsState getState() {
        return state;
    }

    public void setState(LemmingsState state) {
        this.state = state;
    }
    @Override
     public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(45, 150, 20, 20);
    }
    
}

if you want to see the Main:

package lemmingsLight;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;

import javax.swing.JFrame;

public class Main{  
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("LemmingsLight");
        Fenetre f = new Fenetre();
        f.setBackground(Color.WHITE);
        frame.setContentPane(f);
        frame.setSize(500, 410);
        //frame.setSize(f.getSize());
        //frame.setResizable(false); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

I'm adding the class LemmingState to try the code:

package lemmingsLight;


public enum LemmingsState { //états des Lemmings
    NORMAL,
    BOMBEUR,
    TUNNELIER,
    BLOQUEUR,
    GRIMPEUR,
    CHARPENTIER,
    FOREUR,
    PARACHUTEUR,
    
    GLemmingState() {
    }
    
    
}

Thank you to have read my issue


Solution

  • You need to have one class which will extend JPanel (fenetre) and array/list of objects (in your case Lemmings and Carreau list) both of those objects should have a void function which you call and pass the graphics so:

    public class Lemmings{
    
    public void draw(Graphics2D g2d)
    {
    //draw something here
    }
    }
    

    Then in fenetre in paint function you just call the function draw on each of objects in array

    public class Fenetre extends JPanel{
    List<Lemmings> lems = new ArrayList<>(); //add the objects into this one
        @Override
        public void paint(Graphics g)
        {
        Graphics2D g2d = (Graphics2D)g;
             for(int i = 0; i < lems.size(); i++)
             {
                  lems.get(i).draw(g2d);
             }
        }
        }