1 | /**
|
2 | *
|
3 | */
|
4 | package test;
|
5 |
|
6 | import java.awt.BorderLayout;
|
7 | import java.awt.Color;
|
8 | import java.awt.Component;
|
9 | import java.awt.Dimension;
|
10 | import java.awt.Graphics;
|
11 | import java.awt.Graphics2D;
|
12 | import java.awt.Point;
|
13 | import java.awt.RenderingHints;
|
14 | import java.awt.Shape;
|
15 | import java.awt.event.ActionEvent;
|
16 | import java.awt.event.ActionListener;
|
17 | import java.awt.event.MouseAdapter;
|
18 | import java.awt.event.MouseEvent;
|
19 | import java.awt.geom.Line2D;
|
20 | import java.util.ArrayList;
|
21 | import java.util.Collections;
|
22 | import java.util.List;
|
23 |
|
24 | import javax.swing.JButton;
|
25 | import javax.swing.JColorChooser;
|
26 | import javax.swing.JComponent;
|
27 | import javax.swing.JFrame;
|
28 | import javax.swing.JLabel;
|
29 | import javax.swing.JSpinner;
|
30 | import javax.swing.JToggleButton;
|
31 | import javax.swing.JToolBar;
|
32 | import javax.swing.SpinnerNumberModel;
|
33 | import javax.swing.event.ChangeEvent;
|
34 | import javax.swing.event.ChangeListener;
|
35 |
|
36 | /**
|
37 | * @author Christoph Läubrich
|
38 | */
|
39 | public class SimplePaint extends JComponent {
|
40 |
|
41 | private static final long serialVersionUID = -268370314148601306L;
|
42 |
|
43 | /* (non-Javadoc)
|
44 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
45 | */
|
46 | @Override
|
47 | protected void paintComponent(Graphics g) {
|
48 | Graphics2D graphics = (Graphics2D) g.create();
|
49 | try {
|
50 | Dimension dimension = getSize();
|
51 | if (isOpaque()) {
|
52 | g.setColor(Color.WHITE);
|
53 | g.fillRect(0, 0, dimension.width, dimension.height);
|
54 | }
|
55 | drawCanvas(graphics, dimension);
|
56 | } finally {
|
57 | graphics.dispose();
|
58 | }
|
59 | }
|
60 |
|
61 | private final List<PaintObject> paintObjects = new ArrayList<SimplePaint.PaintObject>();
|
62 |
|
63 | private PaintObject previewObject;
|
64 |
|
65 | /**
|
66 | * Zeichnet den Aktuellen Zustand auf das angegeben Grafikobjekt
|
67 | *
|
68 | * @param g
|
69 | * @param dimension
|
70 | */
|
71 | public void drawCanvas(Graphics2D g, Dimension dimension) {
|
72 | //Anti alias off
|
73 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
74 | g.scale(zoom, zoom);
|
75 | synchronized (paintObjects) {
|
76 | for (PaintObject object : paintObjects) {
|
77 | paintShape(g, object);
|
78 | }
|
79 | }
|
80 | if (previewObject != null) {
|
81 | paintShape(g, previewObject);
|
82 | }
|
83 | }
|
84 |
|
85 | protected void paintShape(Graphics2D g, PaintObject object) {
|
86 | g.setColor(object.getColor());
|
87 | g.draw(object.getShape());
|
88 | }
|
89 |
|
90 | public static class PaintObject {
|
91 | public PaintObject(Shape shape, Color color) {
|
92 | super();
|
93 | this.shape = shape;
|
94 | this.color = color;
|
95 | }
|
96 |
|
97 | private Shape shape;
|
98 | private Color color;
|
99 |
|
100 | public void setShape(Shape shape) {
|
101 | this.shape = shape;
|
102 | }
|
103 |
|
104 | public Shape getShape() {
|
105 | return shape;
|
106 | }
|
107 |
|
108 | public void setColor(Color color) {
|
109 | this.color = color;
|
110 | }
|
111 |
|
112 | public Color getColor() {
|
113 | return color;
|
114 | }
|
115 | }
|
116 |
|
117 | /**
|
118 | * @return the current value of paintObjects
|
119 | */
|
120 | public List<PaintObject> getPaintObjects() {
|
121 | synchronized (paintObjects) {
|
122 | return Collections.unmodifiableList(paintObjects);
|
123 | }
|
124 | }
|
125 |
|
126 | public void insertShape(PaintObject object) {
|
127 | synchronized (paintObjects) {
|
128 | paintObjects.add(object);
|
129 | }
|
130 | }
|
131 |
|
132 | public void setPreview(PaintObject object) {
|
133 | this.previewObject = object;
|
134 | }
|
135 |
|
136 | public void setZoomFactor(double zoom) {
|
137 | this.zoom = zoom;
|
138 | }
|
139 |
|
140 | public double getZoomFactor() {
|
141 | return this.zoom;
|
142 | }
|
143 |
|
144 | private static Color currentColor = Color.RED;
|
145 |
|
146 | private double zoom = 1.0;
|
147 |
|
148 | public static void main(String[] args) {
|
149 | SimplePaint paint = new SimplePaint();
|
150 | JFrame frame = new JFrame("Test");
|
151 | frame.add(getToolbar(paint), BorderLayout.NORTH);
|
152 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
153 | frame.add(paint);
|
154 | frame.setLocationRelativeTo(null);
|
155 | frame.setSize(800, 600);
|
156 | frame.setVisible(true);
|
157 | }
|
158 |
|
159 | /**
|
160 | * @return
|
161 | */
|
162 | private static Component getToolbar(final SimplePaint paint) {
|
163 | final JToolBar toolBar = new JToolBar("my Toolbar");
|
164 | final JButton button = new JButton("Choose Color");
|
165 | final JLabel colorLabel = new JLabel(" ");
|
166 | colorLabel.setOpaque(true);
|
167 | colorLabel.setBackground(currentColor);
|
168 | button.addActionListener(new ActionListener() {
|
169 |
|
170 | @Override
|
171 | public void actionPerformed(ActionEvent e) {
|
172 | currentColor = JColorChooser.showDialog(toolBar, "Choose Color", currentColor);
|
173 | colorLabel.setBackground(currentColor);
|
174 | }
|
175 | });
|
176 | final JToggleButton drawLine = new JToggleButton("draw line");
|
177 | drawLine.addActionListener(new ActionListener() {
|
178 |
|
179 | @Override
|
180 | public void actionPerformed(ActionEvent e) {
|
181 | paint.setZoomFactor(1);
|
182 | paint.repaint();
|
183 | }
|
184 | });
|
185 | MouseAdapter lineMouse = new MouseAdapter() {
|
186 |
|
187 | private Point start;
|
188 |
|
189 | @Override
|
190 | public void mouseReleased(MouseEvent e) {
|
191 | if (drawLine.isSelected()) {
|
192 | paint.insertShape(new PaintObject(new Line2D.Double(start, e.getPoint()), currentColor));
|
193 | paint.setPreview(null);
|
194 | paint.repaint();
|
195 | start = null;
|
196 | }
|
197 | }
|
198 |
|
199 | @Override
|
200 | public void mousePressed(MouseEvent e) {
|
201 | if (drawLine.isSelected()) {
|
202 | start = e.getPoint();
|
203 | }
|
204 | }
|
205 |
|
206 | /* (non-Javadoc)
|
207 | * @see java.awt.event.MouseAdapter#mouseDragged(java.awt.event.MouseEvent)
|
208 | */
|
209 | @Override
|
210 | public void mouseDragged(MouseEvent e) {
|
211 | if (drawLine.isSelected() && start != null) {
|
212 | final Line2D.Double shape = new Line2D.Double(start, e.getPoint());
|
213 | paint.setPreview(new PaintObject(shape, currentColor));
|
214 | paint.repaint();
|
215 | }
|
216 | }
|
217 |
|
218 | };
|
219 | paint.addMouseListener(lineMouse);
|
220 | paint.addMouseMotionListener(lineMouse);
|
221 | final JSpinner spinner = new JSpinner(new SpinnerNumberModel(100, 10, 400, 10));
|
222 | spinner.addChangeListener(new ChangeListener() {
|
223 |
|
224 | @Override
|
225 | public void stateChanged(ChangeEvent e) {
|
226 | drawLine.setSelected(false);
|
227 | paint.setZoomFactor(((Number) spinner.getValue()).doubleValue() / 100.0);
|
228 | paint.repaint();
|
229 | }
|
230 | });
|
231 | toolBar.add(colorLabel);
|
232 | toolBar.addSeparator();
|
233 | toolBar.add(button);
|
234 | toolBar.add(drawLine);
|
235 | toolBar.addSeparator();
|
236 | toolBar.add(new JLabel("Zoom"));
|
237 | toolBar.add(spinner);
|
238 | toolBar.addSeparator();
|
239 | return toolBar;
|
240 | }
|
241 |
|
242 | }
|