Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
4571 connectés 

 


Dernière réponse
Sujet : [Java/Swing/Graphics] pbl d'affichage
aurel Ouais ba non j'es suis revenu a l'ancienne méthode, voila le code:package com.intoan.peditor.gui;
 
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
 
public class ProcessorEditorButton extends JComponent
{
 public static final Color BACKCOLOR = new Color(150,150,150); // color of the Editor
 public static final int COMPACTSQUARE = 12; // Compactness of the scope of the square interior
 public static final int WIDHTSQUARE = 64; // Largeur et hauteur du carré
 public static final int COORDONATE = 50; // coordonnées of the top-left corner of the square
 public static final int TOP = 1;
 public static final int BOTTOM = 2;
 public static final int LEFT = 3;
 public static final int RIGHT = 4;
 public static final int ARCWIDHT = 8 ; //widht and height od the arc of the lttle square
 public int ALIGN;
 public int[] Coor;
 public int x,y,c,w;
 
 
 public ProcessorEditorButton(int align, int x, int y,int c, int w )
 {
  ALIGN = align;
  this.x = x;  //Coordonate x of the topbottom point of the interior square
  this.y = y;  //Coordonate y of the topbottom point of the interior square
  this.c = c;  //widht of the interior square
  this.w = w;  //compactness of the big square
  Coor = getCoordinate(ALIGN,x,y,c,w);
  setSize(new Dimension(100,100));
 }
 
 public void paint(Graphics g)
 {
 
  g.setColor(ProcessorEditorIcon.BACKCOLOR);
  g.fillRect(COORDONATE,COORDONATE,WIDHTSQUARE,WIDHTSQUARE);  
  g.setColor(Color.white);
  g.fillRect(COORDONATE+(COMPACTSQUARE/2),COORDONATE+(COMPACTSQUARE/2),WIDHTSQUARE-COMPACTSQUARE,WIDHTSQUARE-COMPACTSQUARE);  
  g.setColor(ProcessorEditorIcon.BACKCOLOR);
  switch (ALIGN)
  {
   case 1:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/2),(WIDHTSQUARE/4)+(WIDHTSQUARE/8),ARCWIDHT,ARCWIDHT);
    break;
   case 2:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/2),(WIDHTSQUARE/4)+(WIDHTSQUARE/8),ARCWIDHT,ARCWIDHT);
    break;
   case 3:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/4)+(WIDHTSQUARE/8),(WIDHTSQUARE/2),ARCWIDHT,ARCWIDHT);
    break;
   case 4:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/4)+(WIDHTSQUARE/8),(WIDHTSQUARE/2),ARCWIDHT,ARCWIDHT);
    break;
  }
 }
 
 
/* getCoordinate
* Return the coordonate of the top bottom point if the littke square to be displayed
*
*
**/
 
 public int[] getCoordinate(int a, int xx, int yy, int cc, int ww)
 {
  int xp,yp;
  int align = a;
  int x = xx;
  int y = yy;
  int c = cc;
  int w = ww;  
  xp = 0;
  yp = 0;
  switch (align)
  {
   case 1: // top
    xp = x + w/4;
    yp = y - w/4 -w/16;
    break;
   case 2: // Bottom
    xp = x + w/4;
    yp = y + w - w/16;
    break;
   case 3: // left
    xp = x -w/4 -w/16;
    yp = y +w/4;
    break;
   case 4: // right
    xp = x + w - w/16;
    yp = y + w/4;
   break;
  }
  int[] coord = new int[2];  
  coord[0] = xp;
  coord[1] = yp;
  return coord;    
 }
 
 
 public static void main (String[] args)
 {
  JFrame jf = new JFrame("Little Button Icon" );
  jf.getContentPane().setBackground(Color.white);
  jf.setBackground(Color.white);
  JComponent jc = (JComponent) jf.getContentPane();
  jc.setPreferredSize(new Dimension(200,200));
  ProcessorEditorButton peb1 = new ProcessorEditorButton(TOP,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb1);
  ProcessorEditorButton peb3 = new ProcessorEditorButton(LEFT,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb3);
  ProcessorEditorButton peb4 = new ProcessorEditorButton(RIGHT,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb4);
  ProcessorEditorButton peb2 = new ProcessorEditorButton(BOTTOM,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb2);
  System.out.println(jf.getContentPane().getComponents().length);
   
  jf.pack();
  jf.setVisible(true);
  jf.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){ System.exit(0) ; }
  });
 }
}
 
 
 
 
ouais c comme meme un gros morceau, il  est compilable en direct j'import pas de classe a moi.si t'es motiv' wouatouwouatou,merci

Votre réponse
Nom d'utilisateur    Pour poster, vous devez être inscrit sur ce forum .... si ce n'est pas le cas, cliquez ici !
Le ton de votre message                        
                       
Votre réponse


[b][i][u][strike][spoiler][fixed][cpp][url][email][img][*]   
 
   [quote]
 

Options

 
Vous avez perdu votre mot de passe ?


Vue Rapide de la discussion
aurel Ouais ba non j'es suis revenu a l'ancienne méthode, voila le code:package com.intoan.peditor.gui;
 
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
 
public class ProcessorEditorButton extends JComponent
{
 public static final Color BACKCOLOR = new Color(150,150,150); // color of the Editor
 public static final int COMPACTSQUARE = 12; // Compactness of the scope of the square interior
 public static final int WIDHTSQUARE = 64; // Largeur et hauteur du carré
 public static final int COORDONATE = 50; // coordonnées of the top-left corner of the square
 public static final int TOP = 1;
 public static final int BOTTOM = 2;
 public static final int LEFT = 3;
 public static final int RIGHT = 4;
 public static final int ARCWIDHT = 8 ; //widht and height od the arc of the lttle square
 public int ALIGN;
 public int[] Coor;
 public int x,y,c,w;
 
 
 public ProcessorEditorButton(int align, int x, int y,int c, int w )
 {
  ALIGN = align;
  this.x = x;  //Coordonate x of the topbottom point of the interior square
  this.y = y;  //Coordonate y of the topbottom point of the interior square
  this.c = c;  //widht of the interior square
  this.w = w;  //compactness of the big square
  Coor = getCoordinate(ALIGN,x,y,c,w);
  setSize(new Dimension(100,100));
 }
 
 public void paint(Graphics g)
 {
 
  g.setColor(ProcessorEditorIcon.BACKCOLOR);
  g.fillRect(COORDONATE,COORDONATE,WIDHTSQUARE,WIDHTSQUARE);  
  g.setColor(Color.white);
  g.fillRect(COORDONATE+(COMPACTSQUARE/2),COORDONATE+(COMPACTSQUARE/2),WIDHTSQUARE-COMPACTSQUARE,WIDHTSQUARE-COMPACTSQUARE);  
  g.setColor(ProcessorEditorIcon.BACKCOLOR);
  switch (ALIGN)
  {
   case 1:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/2),(WIDHTSQUARE/4)+(WIDHTSQUARE/8),ARCWIDHT,ARCWIDHT);
    break;
   case 2:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/2),(WIDHTSQUARE/4)+(WIDHTSQUARE/8),ARCWIDHT,ARCWIDHT);
    break;
   case 3:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/4)+(WIDHTSQUARE/8),(WIDHTSQUARE/2),ARCWIDHT,ARCWIDHT);
    break;
   case 4:
    g.fillRoundRect(Coor[0],Coor[1],(WIDHTSQUARE/4)+(WIDHTSQUARE/8),(WIDHTSQUARE/2),ARCWIDHT,ARCWIDHT);
    break;
  }
 }
 
 
/* getCoordinate
* Return the coordonate of the top bottom point if the littke square to be displayed
*
*
**/
 
 public int[] getCoordinate(int a, int xx, int yy, int cc, int ww)
 {
  int xp,yp;
  int align = a;
  int x = xx;
  int y = yy;
  int c = cc;
  int w = ww;  
  xp = 0;
  yp = 0;
  switch (align)
  {
   case 1: // top
    xp = x + w/4;
    yp = y - w/4 -w/16;
    break;
   case 2: // Bottom
    xp = x + w/4;
    yp = y + w - w/16;
    break;
   case 3: // left
    xp = x -w/4 -w/16;
    yp = y +w/4;
    break;
   case 4: // right
    xp = x + w - w/16;
    yp = y + w/4;
   break;
  }
  int[] coord = new int[2];  
  coord[0] = xp;
  coord[1] = yp;
  return coord;    
 }
 
 
 public static void main (String[] args)
 {
  JFrame jf = new JFrame("Little Button Icon" );
  jf.getContentPane().setBackground(Color.white);
  jf.setBackground(Color.white);
  JComponent jc = (JComponent) jf.getContentPane();
  jc.setPreferredSize(new Dimension(200,200));
  ProcessorEditorButton peb1 = new ProcessorEditorButton(TOP,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb1);
  ProcessorEditorButton peb3 = new ProcessorEditorButton(LEFT,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb3);
  ProcessorEditorButton peb4 = new ProcessorEditorButton(RIGHT,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb4);
  ProcessorEditorButton peb2 = new ProcessorEditorButton(BOTTOM,COORDONATE,COORDONATE,COMPACTSQUARE,WIDHTSQUARE);
  jf.getContentPane().add(peb2);
  System.out.println(jf.getContentPane().getComponents().length);
   
  jf.pack();
  jf.setVisible(true);
  jf.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){ System.exit(0) ; }
  });
 }
}
 
 
 
 
ouais c comme meme un gros morceau, il  est compilable en direct j'import pas de classe a moi.si t'es motiv' wouatouwouatou,merci
aurel en fait j'ai pigé une partie de mon probleme, ma question est maintenant: comment qu'on fait pour rajouter des drawline, fillrect..., sans les mettre dans la fonction paint() de la classe?
le paint est appelé par le contructeur, et je voudrais ajouter un rectangle quand on appele une certaine fonction de la classe (genre addrectangle)  
est ce clair ?
 
Merci
aurel en fait j'
wouatouwouatou un peu limit ta description :D... code plz :jap:
aurel J'ai réalisé une classe X qui extends JComponent
dans cette classe y'a notemment le constructeur
et une fonction paint.
dans le main, pour le test, je crée une jframe et cinq objets de la classe X
losque je fait myjframe.getcontentpane().add<nom de mes cinq objes 1 par 1>, y'a un objet qui s'affiche pas.
le layout est celui par défaut (flowlayout).
 
je vois pas dou ca vient, si vou voulez le source, dites, l'est pas dur a comprendre

Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)