2 * Copyright (c) 2010 The Desert team
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 package org
.sourceforge
.desert
;
28 import java
.awt
.Canvas
;
29 import java
.awt
.Graphics
;
30 import java
.awt
.event
.ComponentAdapter
;
31 import java
.awt
.event
.ComponentEvent
;
32 import java
.awt
.event
.MouseAdapter
;
33 import java
.awt
.event
.MouseEvent
;
34 import java
.awt
.image
.BufferedImage
;
35 import java
.util
.ArrayList
;
36 import java
.util
.List
;
40 * @author codistmonk (creation 2010-04-13)
42 public class DrawingBoard
extends Canvas
{
44 private final List
<Particle
> particles
;
46 private final ParticleUpdater particleUpdater
;
48 private BufferedImage buffer
;
50 public DrawingBoard() {
51 this.particles
= new ArrayList
<Particle
>();
52 this.particleUpdater
= new ParticleUpdater(this.particles
);
54 this.addComponentListener(this.new ResizeHandler());
56 final MouseHandler mouseHandler
= this.new MouseHandler();
58 this.addMouseListener(mouseHandler
);
59 this.addMouseMotionListener(mouseHandler
);
63 public void paint(final Graphics g
) {
66 this.drawParticlesToBuffer();
74 * <br>Should not be null
75 * <br>Range: <code>]0 .. Double.POSITIVE_INFINITY[</code>
77 public final void update(final Double deltaTime
) {
78 this.particleUpdater
.update(deltaTime
);
84 * <br>Should not be null
85 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
87 * <br>Should not be null
88 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
90 * <br>Should not be null
91 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
93 * <br>Should not be null
94 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
96 final void addParticle(final Double x
, final Double y
, final Double speedX
, final Double speedY
) {
97 this.particles
.add(new Particle(x
, y
, speedX
, speedY
));
100 final void resizeBuffer() {
101 this.buffer
= new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage
.TYPE_3BYTE_BGR
);
102 this.particleUpdater
.setBounds(this.getBounds());
105 private final void clearBuffer() {
106 this.buffer
.createGraphics().clearRect(0, 0, this.buffer
.getWidth(), this.buffer
.getHeight());
109 private final void drawParticlesToBuffer() {
110 final Graphics bufferGraphics
= this.buffer
.createGraphics();
112 for (final Particle particle
: this.particles
) {
113 particle
.draw(bufferGraphics
);
120 * <br>Should not be null
122 private final void drawBuffer(final Graphics graphics
) {
123 graphics
.drawImage(this.buffer
, 0, 0, this);
128 * @author codistmonk (creation 2010-04-13)
130 private final class ResizeHandler
extends ComponentAdapter
{
133 public final void componentResized(final ComponentEvent event
) {
134 DrawingBoard
.this.resizeBuffer();
141 * @author codistmonk (creation 2010-04-13)
143 private final class MouseHandler
extends MouseAdapter
{
146 public final void mouseDragged(final MouseEvent event
) {
147 this.addRandomParticle((double) event
.getX(), (double) event
.getY());
151 public final void mousePressed(final MouseEvent event
) {
152 this.addRandomParticle((double) event
.getX(), (double) event
.getY());
155 private final void addRandomParticle(final Double x
, final Double y
) {
156 final Double orientation
= Math
.random() * 2.0 * Math
.PI
;
158 DrawingBoard
.this.addParticle(x
, y
, Math
.cos(orientation
) * INITIAL_SPEED
, Math
.sin(orientation
) * INITIAL_SPEED
);
163 private static final Double INITIAL_SPEED
= 4.0;