0

I would like to create different graphical object but I would like that each is a different Jpanel object.

I can draw all of my Graphics like that :

import java.awt.Color;
import java.awt.Graphics;
import java.awt.PaintContext;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Panel extends JPanel{

    private static BufferedImage IMG;
        static{
            try {
                IMG = ImageIO.read(new File("bground.jpg")); //Replace with your image path
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Erreur");
            }
        }       

    public void paintComponent(Graphics g){
        setBackground(Color.BLUE);
        g.drawImage(IMG, 0, 0, 500, 700, this);
        // Carre pour l'eau en Amont
        g.setColor(new Color(34,34,212));
        g.fillRect(3, 250, 200, 100);
        // Carre pour la porte en Amont
        g.setColor(new Color(166,3,0));
        g.fillRect(203, 150,10, 350);
        // Carre pour la porte en Aval
        g.fillRect(283, 150,10, 350);
        // Carre pour l'eau dans le SAS
        g.setColor(new Color(34,34,212));
        g.fillRect(213, 250, 70, 100);

        // Les Feux
        // En amont
        g.setColor(Color.BLACK);
        g.fillRect(203,80,10,40);

        //EN aval
        g.fillRect(283,80,10,40);

        //feu rouge
        g.setColor(Color.RED);
        g.fillArc(203, 82, 10, 10, 0, 360);
        g.fillArc(283, 82, 10, 10, 0, 360);

        //feu Orange
        g.setColor(Color.ORANGE);
        g.fillArc(203, 95, 10, 10, 0, 360);
        g.fillArc(283, 95, 10, 10, 0, 360);

        //feu Vert

        g.setColor(Color.GREEN);
        g.fillArc(203, 108, 10, 10, 0, 360);
        g.fillArc(283, 108, 10, 10, 0, 360);

        // Vanne d'evacuation d'eau en Amont
        }           
    }

However I want EVERY forms are in a separate object.

Like that

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

public class Vannes extends JPanel{

    public Vannes(){
    }

    public void paintComponent(Graphics g){
        // Vanne d'evacuation d'eau en Amont 
        g.setColor(Color.BLACK);
        g.fillRect(180, 480, 5, 50);
    }       
}

But when i add in my first panel

Vannes v = new Vanne();

nothing appear!

Why

I tried to add like that but not works :

 static Panel panel = new Panel();
 static JLabel j = new JLabel(new ImageIcon("bateauptit.png"));
 static Vannes v1 = new Vannes();

 public ExempleDeplace(){
     //getContentPane().setLayout(new FlowLayout());
     setTitle("Modele Ecluse");
     setSize(500,700);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setContentPane(panel);
     panel.add(v1);
     panel.add(j);
     System.out.println("Nbr Componemnt" + panel.getComponentCount()); 
     j.setLocation(0, 100);
     System.out.println("X : " + j.getX()+ " Y: " + j.getY());
     //panel.repaint();
     setVisible(true);
     trame();        

     System.out.println("X : " + j.getX()+ " Y: " + j.getY());
}

My question is so How can I implement a Jpanel into and upon another JPANEL?

shmoolki
  • 1,551
  • 8
  • 34
  • 57
  • 1
    @trashgod He isn't using `java.awt.Panel`. `Panel` is his own class extending `javax.swing.JPanel`. – xehpuk Mar 01 '15 at 15:21
  • @xehpuk: Good catch; shmoolki: don't forget `super.paintComponent()`. – trashgod Mar 01 '15 at 16:32
  • _How can I implement one_ `JPanel` _… upon another_ `JPanel`? Possible [duplicate](http://stackoverflow.com/q/13436787/230513). – trashgod Mar 01 '15 at 16:47
  • *"However I want EVERY forms are in a separate object."* Just because it is separate objects does not mean it has to be **separate objects based on components.** BTW - See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Mar 04 '15 at 06:38

0 Answers0