1
package latexDraw.ui.components;
3
import java.awt.BasicStroke;
5
import java.awt.Dimension;
6
import java.awt.Graphics;
7
import java.awt.Graphics2D;
8
import java.awt.RenderingHints;
9
import java.awt.event.ItemEvent;
10
import java.awt.event.ItemListener;
11
import java.awt.event.MouseEvent;
12
import java.awt.event.MouseListener;
13
import java.awt.geom.Line2D;
15
import javax.swing.ButtonGroup;
16
import javax.swing.JCheckBoxMenuItem;
17
import javax.swing.JComboBox;
18
import javax.swing.JComponent;
19
import javax.swing.JPopupMenu;
21
import latexDraw.lang.LaTeXDrawLang;
22
import latexDraw.psTricks.PSTricksConstants;
23
import latexDraw.ui.DrawPanel;
24
import latexDraw.ui.dialog.ExceptionFrameDialog;
28
* This class defines the X-scale.<br>
30
* This file is part of LaTeXDraw<br>
31
* Copyright (c) 2005-2008 Arnaud BLOUIN<br>
33
* LaTeXDraw is free software; you can redistribute it and/or modify
34
* it under the terms of the GNU General Public License as published by
35
* the Free Software Foundation; either version 2 of the License, or
36
* (at your option) any later version.<br>
38
* LaTeXDraw is distributed without any warranty; without even the
39
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
40
* PURPOSE. See the GNU General Public License for more details.<br>
43
* @author Arnaud BLOUIN<br>
46
public class XScale extends JComponent implements MouseListener,ItemListener
48
private static final long serialVersionUID = 1L;
50
/** The drawPanel which contains the pixels per centimetre */
51
private DrawPanel drawPanel;
53
/** The other scale : the Y-scale (Useful for a good display) */
54
private YScale yScale;
56
/** The size of the lines in axes */
57
public static final int SIZE = 10;
59
/** The popup menu which allows to set the type of measure (inch, cm, ...) */
60
private JPopupMenu popupMenu;
62
/** The label of the centimetre menu */
63
public static final String LABEL_CM = LaTeXDrawLang.getOthersString("XScale.cm"); //$NON-NLS-1$
65
/** The label of the inch menu */
66
public static final String LABEL_INCH = LaTeXDrawLang.getOthersString("XScale.inch"); //$NON-NLS-1$
68
/** The unit of length by default */
69
public static final String DEFAULT_UNIT = LABEL_CM;
71
/** The menu for centimetres */
72
protected JCheckBoxMenuItem cmMenu;
74
/** The menu for inches */
75
protected JCheckBoxMenuItem inchMenu;
81
* @param drawP The drawPanel which contains the pixels per centimetre.
82
* @param y The YScale bounds to the XScale.
83
* @throws IllegalArgumentException If drawP or y is null.
85
public XScale(DrawPanel drawP, YScale y)
89
if(drawP==null || y==null)
90
throw new IllegalArgumentException();
92
setPreferredSize(new Dimension(500, SIZE));
96
/* Creation of the popup menu */
97
ButtonGroup buttonGroup = new ButtonGroup();
98
popupMenu = new JPopupMenu();
99
cmMenu = new JCheckBoxMenuItem(LABEL_CM);
100
cmMenu.setActionCommand(LABEL_CM);
101
cmMenu.addItemListener(this);
102
inchMenu = new JCheckBoxMenuItem(LABEL_INCH);
103
inchMenu.addItemListener(this);
104
inchMenu.setActionCommand(LABEL_INCH);
105
cmMenu.setSelected(true);
106
popupMenu.add(cmMenu);
107
popupMenu.add(inchMenu);
108
buttonGroup.add(cmMenu);
109
buttonGroup.add(inchMenu);
110
addMouseListener(this);
111
setDoubleBuffered(true);
119
* @param y The new Y-scale.
120
* @throws IllegalArgumentException If y is null.
122
public void setYScale(YScale y)
125
throw new IllegalArgumentException();
134
* Allows to access to the popup menu.
135
* @return The popup menu.
137
public JPopupMenu getPopupMenu()
146
* Allows to know the X-coordinates of the start of the X-Scale.
147
* @return The X-coordinates of the start of the X-Scale.
149
public float getStart()
156
if(yScale.isVisible())
167
public void paintComponent(Graphics g)
171
Dimension d = getSize();
172
double zoom = drawPanel.getZoom();
173
double width = d.getWidth()*(1/zoom), i, j, start, cpt;
174
double pixPerCm = drawPanel.getPixelsPerCm();
175
Graphics2D g2 = (Graphics2D)g;
177
g2.scale(zoom, zoom);
178
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
179
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
180
g2.setStroke(new BasicStroke(0, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
182
Color formerCol = g2.getColor();
183
g2.setColor(Color.BLACK);
188
if(yScale.isVisible())
192
if(inchMenu.isSelected())
193
pixPerCm*=PSTricksConstants.INCH_VAL_CM;
195
if(pixPerCm/10.!=(int)(pixPerCm/10.))
197
double tmp = pixPerCm/10.;
198
tmp = tmp-(int)tmp>0.5 ? ((int)tmp)+1 : (int)tmp;
204
double pixPerCm10 = pixPerCm/10.;
206
for(i=start/zoom+pixPerCm10; i<width; i+=pixPerCm)
207
for(j=i, cpt=1; cpt<10; j+=pixPerCm10, cpt++)
208
g2.draw(new Line2D.Double(j,(SIZE/2.)/zoom,j,SIZE/zoom));
211
for(i=start/zoom; i<width;i+=pixPerCm)
212
g2.draw(new Line2D.Double(i, 0., i, SIZE/zoom));
214
g2.setColor(formerCol);
218
ex.printStackTrace();
219
ExceptionFrameDialog.showExceptionDialog(ex);
227
* Defines the actions that it must be realised by a right click of the mouse.
228
* @param e Corresponds to the click of the mouse.
230
public void mouseClickedRightButton(MouseEvent e)
233
popupMenu.show(e.getComponent(), e.getX(), e.getY());
240
* Defines the actions that it must be realised by a click of the mouse.
243
public void mouseClicked(MouseEvent e)
245
switch(e.getButton())
247
case MouseEvent.BUTTON1 :
250
case MouseEvent.BUTTON2 :
253
case MouseEvent.BUTTON3 :
254
mouseClickedRightButton(e);
265
public void mousePressed(MouseEvent e)
267
/* No action to do. */
271
public void mouseReleased(MouseEvent e)
273
/* No action to do. */
277
public void mouseEntered(MouseEvent e)
279
/* No action to do. */
284
public void mouseExited(MouseEvent e)
286
/* No action to do. */
291
public void itemStateChanged(ItemEvent e)
293
Object o = e.getSource();
295
if(o instanceof JCheckBoxMenuItem || o instanceof JComboBox)
297
boolean isInCm = true;
299
if(o instanceof JCheckBoxMenuItem)
300
drawPanel.getDraw().setUnitForPixelsPerCm(
301
((JCheckBoxMenuItem)o).getActionCommand());
303
if(o instanceof JComboBox)
305
if(!(((String)((JComboBox)o).getSelectedItem())).equals(LABEL_CM))
307
cmMenu.setSelected(isInCm);
308
inchMenu.setSelected(!isInCm);
310
drawPanel.getDraw().setUnitForPixelsPerCm(
311
((String)((JComboBox)o).getSelectedItem()));
323
* Sets the unit of length (and to change the scale of the XScale and to check the good menu).
324
* @param unitForPixelsPerCm The unitForPixelsPerCm to set.
325
* @throws IllegalArgumentException If unitForPixelsPerCm is not valid.
327
public void setUnitForPixelsPerCm(String unitForPixelsPerCm)
329
if(unitForPixelsPerCm==null || (!unitForPixelsPerCm.equals(LABEL_CM) && !unitForPixelsPerCm.equals(LABEL_INCH)))
330
throw new IllegalArgumentException();
332
boolean inCm = unitForPixelsPerCm.equals(LABEL_CM);
334
cmMenu.setSelected(inCm);
335
inchMenu.setSelected(!inCm);