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

AWT/Swing Java Discussion :

Texture dans un JPanel


Sujet :

AWT/Swing Java

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 10
    Par défaut Texture dans un JPanel
    Bonjour,

    J'ai voulu tester un nouveau style de JPanel avec des textures en fond, semblable a une boite.

    Cette boite contient donc 5 textures différentes :
    - 1 pour le fond,
    - 4 pour chaque cotés (gauche, droite, haut, bas).

    La texture est la même pour chaque coté (une ombre noire), elle est juste retourné de 90°. (C'est temporaire...).

    Un truc très simple finalement... Seulement, le résultat est juste monstrueux, pourtant je pensais m'y prendre correctement :

    Code JAVA : 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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    public class WorldListPanel extends JPanel {
     
    	private TexturePaint boxBackground = null;
    	private TexturePaint boxLeft = null;
    	private TexturePaint boxRight = null;
    	private TexturePaint boxTop = null;
    	private TexturePaint boxBottom = null;
     
    	public WorldListPanel() {
    		super(new GridBagLayout());
     
    		ResourceManager resourceManager = ResourceManager.getInstance();
     
    		BufferedImage boxBackImage = resourceManager.loadImage(GUIResourcePath.WORLDLIST_BOX_BACKGROUND);
    		boxBackground = new TexturePaint(
    			boxBackImage,
    			new Rectangle(0, 0, boxBackImage.getWidth(), boxBackImage.getHeight())
    		);
    		BufferedImage boxLeftImage = resourceManager.loadImage(GUIResourcePath.WORLDLIST_BOX_LEFT);
    		boxLeft = new TexturePaint(
    			boxLeftImage,
    			new Rectangle(0, 0, boxLeftImage.getWidth(), boxLeftImage.getHeight())
    		);
    		BufferedImage boxRightImage = resourceManager.loadImage(GUIResourcePath.WORLDLIST_BOX_RIGHT);
    		boxRight = new TexturePaint(
    			boxRightImage,
    			new Rectangle(0, 0, boxRightImage.getWidth(), boxRightImage.getHeight())
    		);
    		BufferedImage boxTopImage = resourceManager.loadImage(GUIResourcePath.WORLDLIST_BOX_TOP);
    		boxTop = new TexturePaint(
    			boxTopImage,
    			new Rectangle(0, 0, boxTopImage.getHeight(), boxTopImage.getWidth())
    		);
    		BufferedImage boxBottomImage = resourceManager.loadImage(GUIResourcePath.WORLDLIST_BOX_BOTTOM);
    		boxBottom = new TexturePaint(
    			boxBottomImage,
    			new Rectangle(0, 0, boxBottomImage.getHeight(), boxBottomImage.getWidth())
    		);
     
    		setPreferredSize(new Dimension(400, 500));
    		setOpaque(false);
     
    		add(new JButton("Charger un monde"));
    	}
     
    	public void paintComponent(Graphics g) {
    		Graphics2D g2d = (Graphics2D) g;
     
    		g2d.setPaint(boxLeft);
    		g2d.fillRect(
    			0,
    			boxTop.getImage().getHeight(),
    			boxLeft.getImage().getWidth(),
    			getHeight() - (boxBottom.getImage().getHeight() * 2)
    		);
     
    		g2d.setPaint(boxRight);
    		g2d.fillRect(
    			getWidth() - boxRight.getImage().getWidth(),
    			boxTop.getImage().getHeight(),
    			boxRight.getImage().getWidth(),
    			getHeight() - (boxTop.getImage().getHeight() * 2)
    		);
     
    		g2d.setPaint(boxTop);
    		g2d.fillRect(
    			boxLeft.getImage().getWidth(),
    			0,
    			getWidth() - (boxLeft.getImage().getWidth() * 2),
    			boxTop.getImage().getHeight()
    		);
     
    		g2d.setPaint(boxBottom);
    		g2d.fillRect(
    			boxLeft.getImage().getWidth(),
    			getHeight() - boxBottom.getImage().getHeight(),
    			getWidth() - (boxLeft.getImage().getWidth() * 2),
    			boxBottom.getImage().getHeight()
    		);
     
    		g2d.setPaint(boxBackground);
    		g2d.fillRect(
    			boxLeft.getImage().getWidth(),
    			boxTop.getImage().getHeight(),
    			getWidth() - (boxLeft.getImage().getWidth() * 2),
    			getHeight() - (boxTop.getImage().getHeight() * 2)
    		);
    	}
    }

    Une idée ?
    Images attachées Images attachées  

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

Discussions similaires

  1. Dessiner dans différents JPanels
    Par N@sH dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 20/05/2005, 15h39
  2. [JPanel] parcours d'objets dans un JPanel
    Par fleur1234 dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 17/01/2005, 13h24
  3. texturing dans un renderToTexture
    Par beebop dans le forum OpenGL
    Réponses: 3
    Dernier message: 30/08/2004, 14h16
  4. Copier une texture dans une autre?
    Par supergrey dans le forum DirectX
    Réponses: 9
    Dernier message: 28/07/2004, 11h12
  5. Dessiner dans un JPanel
    Par Oliveuh dans le forum Composants
    Réponses: 5
    Dernier message: 19/07/2004, 12h13

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