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 :

Mon camembert devient un PACMAN :(


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mars 2005
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 162
    Par défaut Mon camembert devient un PACMAN :(
    Bonjour!

    J'ai un camembert que je dessine avec un graph2D et arc2D.

    Je prends en parametre une liste que je dessine une premiere fois. Ensuite, je peux modifier cette liste et grace aux observer observable je veux redessiner mon camembert, mais il me vire la portion, me redessine tout, sans recalculer les valeurs, c'est pas cool... idem si j'ajoute, elle apparait pas dans le graphique ma nouvelle part

    Merci de votre aide, je joins le code source ici:

    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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
     
    import ...;
    /**
     * Cette classe permet de dessiner un camembert
     * @author keum
     *
     */
    public class PanelCamembert extends JPanel implements ComponentListener, MouseInputListener, Observer{
    	/**
             *
             */
    	private static final long serialVersionUID = 1L;
     
    	/** fenetre principale recuperee **/
    	MainHermesFrame hermesFrame;
     
    	/** donnees hermes recuperee de la fenetre principale **/
    	HermesData hermesData_;
     
    	private static final double PI_2 = 2 * Math.PI;
     
    	/** Shared color array.
             */
    	private static final Color[] TABLE_COLOR = new Color[12];
     
    	/** Static initializer.
             */
    	static {
    		TABLE_COLOR[0] = new Color(255, 0, 0);
    		TABLE_COLOR[1] = new Color(0, 255, 0);
    		TABLE_COLOR[2] = new Color(0,0,255);
    		TABLE_COLOR[3] = Color.gray;
    		TABLE_COLOR[4] = Color.yellow;
    		TABLE_COLOR[5] = Color.orange;
    		TABLE_COLOR[6] = Color.pink;
    		TABLE_COLOR[7] = Color.darkGray;
    		TABLE_COLOR[8] = new Color(255,255,19);
    		TABLE_COLOR[9] = new Color(255,25,255);
    		TABLE_COLOR[10] = new Color(120,12,120);
    		TABLE_COLOR[11] = new Color(78,65,12);
    	}
     
    	private static final Stroke DEFAULT_STROKE = new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    	private static final Stroke SELECTED_STROKE = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    	private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);
     
    	private Arc2D selectedArc = null;
    	private java.util.List<Arc2D> shapeList;
    	private Map<Arc2D, Number> arcToValueMap = new HashMap<Arc2D, Number>();
    	private Budget budget;
    	//le nombre de budgets dans notre vecteur
    	private int montantTotal = 0;
    	/** Creates a new instance.
             * @param values Initial values.
             */
    	public PanelCamembert(MainHermesFrame hd) {
    		super();
    		initialisation(hd);
    	}
     
    	private void initialisation(MainHermesFrame hd){
    		//recuperation des donnees
    		hermesData_=hd.hermesData_;
    		//ajouter le panel en temps qu'observer
    		hd.hermesData_.addObservers(this);
     
    		//recuperation de la fenetre principale pour faire suivre les events d un niveau
    		hermesFrame=hd;
    		budget = hermesData_.getBudget(HermesData.getMois(), HermesData.getAnnee());
    		for (int i=0;i< budget.getListeCategorie().size();i++){
    			montantTotal += budget.retourneCategorie(i).getMontantPrevisionnel();
    		}
    		// Le montant du bugdet i du vecteur
    		double montantBudget=0;
     
    		shapeList = new ArrayList<Arc2D>(getNbBudget());
     
    		for (int i = 0; i < budget.getListeCategorie().size(); i++) {
    			montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
    			Arc2D arc = new Arc2D.Double();
    			arc.setArcType(Arc2D.PIE);
    			shapeList.add(arc);
    			arcToValueMap.put(arc, montantBudget);
    			System.out.println(arcToValueMap.toString());
    		}
    		addComponentListener(this);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    	}
     
    	private int getNbBudget(){
    		return budget.getListeCategorie().size();
    	}
    	private void calculateShape() {
    		Dimension size = getSize();
    		Insets insets = getInsets();
    		// Get past (optional) panel border.
    		int width = (size.width - (insets.left + insets.right))/1;
    		int height = (size.height - (insets.top + insets.bottom))/1;
    		double diameter = Math.min(width, height) - 5;
    		// Don't bother if too small to draw.
    		if (diameter > 0) {
    			double theta = 0;
    			double extend = 0;
    			for (int i = 0; i < budget.getListeCategorie().size(); i++) {
    				double montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
    				System.out.println("Montant Budget = "+ montantBudget+"\n");
    				Arc2D arc = shapeList.get(i);
    				arc.setFrame(insets.left + (width - diameter) / 2.0, insets.top + (height - diameter) / 2.0, diameter, diameter);
    				extend = 360 * montantBudget / montantTotal;
    				arc.setAngleStart(theta);
    				arc.setAngleExtent(extend);
    				theta += extend;
    			}
    		}	
    	}	
     
    	/**
             * {@inheritDoc}
             */
    	@Override protected void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Dimension size = getSize();
    		Insets insets = getInsets();
    		// Get past (optional) panel border.
    		int width = (size.width - (insets.left + insets.right))/1;
    		int height = (size.height - (insets.top + insets.bottom))/1;
    		double diameter = Math.min(width, height) - 5;
     
    		if (diameter > 0) {
    			Graphics2D g2d = (Graphics2D) g;
    			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    			g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    			g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    			for (int i = 0; i < budget.getListeCategorie().size(); i++) {
    				Arc2D arc = shapeList.get(i);
    				Color color = TABLE_COLOR[i];
    				g2d.setPaint(color);
    				g2d.fill(arc);
    				if (arc != selectedArc) {
    					g2d.setStroke(DEFAULT_STROKE);
    					g2d.setPaint(getForeground());
    					g2d.draw(arc);
    				}
    			}
    			// 	Draw selection.
    			if (selectedArc != null) {
    				System.out.println("On a selectionné un arc\n");
    				g2d.setStroke(SELECTED_STROKE);
    				g2d.setPaint(getForeground().brighter().brighter());
    				g2d.draw(selectedArc);
    				g2d.setPaintMode();
    			}
    			//        	Draw labels.
    			double labelPosition = 2 / 3.0 * diameter / 2.0;
    			g2d.setPaint(getForeground());
    			double montantBudget = 0;
    			for (int i = 0; i <budget.getListeCategorie().size(); i++) {
    				Arc2D arc = shapeList.get(i);
    				montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
    				double theta = arc.getAngleStart() + arc.getAngleExtent() / 2.0;
    				theta = PI_2 * theta / 360.0;
    				double x = insets.left + width / 2.0 + labelPosition * Math.cos(theta);
    				double y = insets.top + height / 2.0 + labelPosition * Math.sin( -theta);
    				Font font = DEFAULT_FONT;
    				if (arc == selectedArc) {
    					font = font.deriveFont(14f);
    				}
    				String label = String.valueOf(montantBudget);
    				Rectangle2D stringBounds = font.getStringBounds(label, g2d.getFontRenderContext());
    				LineMetrics lineMetrics = font.getLineMetrics(label, g2d.getFontRenderContext());
    				g2d.setFont(font);
    				g2d.setXORMode(Color.WHITE);
    				g2d.drawString(label, (float) (x - stringBounds.getWidth() / 2.0), (float) (y + lineMetrics.getAscent() / 2.0));
    				g2d.setPaintMode();
    			}
    		}
    	}
     
    	/**{@inheritDoc}
             */
    	public void componentResized(ComponentEvent event) {
    		System.out.println("on est dans componentResized\n");
    		calculateShape();
    		repaint();
    	}
     
    	/**{@inheritDoc}
             */
    	public void mouseMoved(MouseEvent event) {
    		selectedArc = null;
    		String tooltip = null;
    		for (Arc2D arc : shapeList) {
    			if (arc.contains(event.getX(), event.getY())) {
    				selectedArc = arc;
    				Number montantBudget = arcToValueMap.get(arc);
    				System.out.println("mouseMoved montantBudget = "+montantBudget);
    				tooltip = String.valueOf(montantBudget);
    				break;
    			}
    		}
    		setToolTipText(tooltip);
    		repaint();
    	}
     
    	public void componentHidden(ComponentEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void componentMoved(ComponentEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void componentShown(ComponentEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseClicked(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseDragged(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/**methode appelee lors de modification du modele **/
    	public void update(Observable arg0, Object arg1) {
    		repaint();
    	}
    }
    Je ne sais pas si c'est à cause de la liste ou de la Map, j'ai fait un clear sur la liste mais ca me fout tout en l'air...

    Merci de votre aide

  2. #2
    Membre confirmé
    Inscrit en
    Mars 2005
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 162
    Par défaut
    Personne ne sait pourquoi lors de l'appel de repaint, si un élément a été supprimé dans la liste budget, il ne recalcule pas tout à partir de la liste modifiée?

  3. #3
    Membre confirmé
    Inscrit en
    Mars 2005
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 162
    Par défaut
    J'ai enfin compris qu'il fallait que je définisse mon montant total dans une fonction hors du constructeur... il fallait juste que je dorme

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

Discussions similaires

  1. [JpGraph] rendre mon camembert plus petit afin de voir mes légendes
    Par vandeyy dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 07/02/2008, 11h27
  2. La couleur de mon ecran devient violet et vert
    Par amoros64 dans le forum Périphériques
    Réponses: 4
    Dernier message: 12/12/2007, 18h22
  3. Mon PC devient de plus en plus lent
    Par Lusito dans le forum Windows XP
    Réponses: 11
    Dernier message: 27/09/2007, 10h59
  4. [IDE][VSC++Express]Pourquoi parfois mon écran devient noir?
    Par olivierm79 dans le forum Visual Studio
    Réponses: 4
    Dernier message: 27/04/2006, 13h14
  5. [AWT]Mon applet devient grise au boût d'un
    Par jimmy36 dans le forum Applets
    Réponses: 3
    Dernier message: 30/03/2006, 16h01

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