IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

2D Java Discussion :

[Applet] Panel Transparent


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 27
    Par défaut [Applet] Panel Transparent
    Bonjour,

    J'ai créé un applet ouvrant une JFrame avec une image de fond. Le probleme est que lorsque j'ajoute des Panels transparents (méthode setOpaque(false)) à ma JFrame mes panels restent opaques

    Voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    import java.awt.*;
    import java.applet.*;
    import javax.swing.border.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    	public class frame extends Applet
    	{  		
    		Image Fond;
    		Image Cour;
    		JFrame_fond fenetre;
     
     
    		public void init()
        		{ 
     
    			//Instanciation des objets de l'IHM
     
    			JPanel centre_panel = new JPanel();
    			JPanel img_panel = new JPanel();
    			JPanel titre_panel = new JPanel();
    			JPanel rep_ind_panel = new JPanel();
    			Border b = new CompoundBorder(new LineBorder(Color.black,2),new LineBorder(Color.gray,2));
    			Fond = getImage(getCodeBase(), "fond.jpg");
    			Cour = getImage(getCodeBase(), "cour.jpg");
     
    			fenetre = new JFrame_fond(Fond);
     
    			img_panel.setOpaque(false);
    			titre_panel.setOpaque(false);
    			rep_ind_panel.setOpaque(false);
    			centre_panel.setOpaque(false);
     
    			//Definition des proriétés des objets de l'IHM
     
    			img_panel.setBorder(b);
    			titre_panel.setBorder(b);
    			rep_ind_panel.setBorder(b);
     
     
    			centre_panel.setLayout(new GridLayout(1, 2));
    			centre_panel.add(img_panel);
    			centre_panel.add(rep_ind_panel);
     
    			fenetre.setLayout(new BorderLayout());
    			fenetre.add("North",titre_panel);
    			fenetre.add("Center",centre_panel);
    			fenetre.setResizable(false);
    			fenetre.setExtendedState(fenetre.MAXIMIZED_BOTH);
    			fenetre.setUndecorated(true);
    			fenetre.setVisible(true);
    			centre_panel.setVisible(true);
     
         		}
     
     
     
    	}
     
    	class JFrame_fond extends JFrame
    	{
    		Image Fond;
     
    		JFrame_fond(Image fond)
    		{
    			Fond = fond;
    		}
     
    		public void paintComponent(Graphics g) {
    			g.drawImage(Fond,0,0,null);
    		}
     
    	}
    Avez vous une ideés ?

    merci

    ++ nairolf

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    Il faut aussi leur mettre une couleur de fond transparente avec setBackground() pour que cela fonctionne.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 27
    Par défaut
    merci pour l'info mais comment créé la couleur transparente ? j'ai regarder la classe java.awt.Color rien ne fais reference a la couleur transparente

    il y a t'il un code RGB specifique ? si oui le quel ?

    j'ai testé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    (...)
    Color transparent = new Color (0, 0, 0, 0);
    (...)
    img_panel.setBackground(transparent);
    titre_panel.setBackground(transparent);
    rep_ind_panel.setBackground(transparent);
    centre_panel.setBackground(transparent);
    img_panel.setOpaque(false);
    titre_panel.setOpaque(false);
    rep_ind_panel.setOpaque(false);
    centre_panel.setOpaque(false);
    (...)
    sa ne change rien...

    une idee ?

    @++ nairolf

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    Apparement ca marche tres bien et meme sans mettre de couleur transparente dans le panel (tu utilises la bonne methode pour la creer - il faut mettre l'alpha a 0).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
     
    package test;
     
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.imageio.*;
     
    /**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2006</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class TestInterface {
      private static final String[] DIRECTIONS = {BorderLayout.NORTH, BorderLayout.WEST, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.CENTER};
      private static final Color[] COLORS = {Color.GREEN, Color.ORANGE, Color.BLUE, Color.CYAN, Color.RED};
     
      public static void main(String ...args) {
        try {
          BufferedImage image = ImageIO.read(new File("test.jpg"));
          for (int j = 0; j < 2; j++) {
            boolean opaque = (j % 2 != 0);
            JFrame frame = new JFrame();
            frame.setTitle(String.valueOf(j) + " " + opaque);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ImagePanel imagePanel = new ImagePanel(image);
            frame.setContentPane(imagePanel);
            frame.setLayout(new BorderLayout());
            for (int i = 0; i < DIRECTIONS.length; i++) {
              JPanel panel = new JPanel();
              panel.setBorder(new LineBorder(Color.WHITE, 2));
              panel.setBackground(COLORS[i]);
              panel.setOpaque(opaque);
              frame.add(panel, DIRECTIONS[i]);
            }
            frame.setSize(400, 400);
            frame.setVisible(true);
          }
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
     
      private static class ImagePanel extends JPanel {
        private Image image = null;
     
        public ImagePanel(Image image) {
          this.image = image;
        }
     
        /** {@inheritDoc}
         */
        protected void paintComponent(Graphics graphics) {
          super.paintComponent(graphics);
          if (image != null) {
            Insets insets = getInsets();
            Dimension size = getSize();
            int width = size.width - (insets.left + insets.right);
            int height = size.height - (insets.top + insets.bottom);
            Graphics2D g2d = (Graphics2D) graphics.create(insets.left, insets.top, width, height);
            try {
              g2d.drawImage(image, 0, 0, width, height, null);
            }
            finally {
              g2d.dispose();
            }
          }
        }
      }
    }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 27
    Par défaut
    J'ai resolu mon probleme. J'ai redefini ma classe JFrame_fond en JPanel_fond heritant de Jpanel afin cree un panel avec un fond et je l'ai ajouté a ma JFrame et cela l'air fonctionnel

    merci de ton aide

    @++ nairolf

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Widgets à background transparent sur un Panel
    Par Lotendan dans le forum wxPython
    Réponses: 6
    Dernier message: 28/01/2009, 17h10
  2. [C# + GDI]Panel transparent:Pb de rafraichissement
    Par Wolverin dans le forum Windows Forms
    Réponses: 9
    Dernier message: 26/06/2007, 10h57
  3. Applet avec fond transparent
    Par yotta dans le forum Applets
    Réponses: 1
    Dernier message: 18/09/2006, 15h16
  4. [C++ .Net 1.1] Comment superposer des panels transparents ?
    Par juliano_bipso dans le forum VC++ .NET
    Réponses: 3
    Dernier message: 16/08/2006, 07h38
  5. [Composant] Panel transparent paramétré
    Par Pedro dans le forum Composants VCL
    Réponses: 2
    Dernier message: 08/10/2005, 18h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo