Rename packages since this project has a team now
[desert.git] / src / org / sourceforge / desert / DrawingBoard.java
bloba91b050c88b2ba3cb7dbd158d9a8a3a6cf2f3033
1 /*
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
11 * conditions:
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;
38 /**
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);
62 @Override
63 public void paint(final Graphics g) {
64 this.clearBuffer();
66 this.drawParticlesToBuffer();
68 this.drawBuffer(g);
71 /**
73 * @param deltaTime
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);
81 /**
83 * @param x
84 * <br>Should not be null
85 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
86 * @param y
87 * <br>Should not be null
88 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
89 * @param speedX
90 * <br>Should not be null
91 * <br>Range: <code>]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[</code>
92 * @param speedY
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);
119 * @param graphics
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 {
132 @Override
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 {
145 @Override
146 public final void mouseDragged(final MouseEvent event) {
147 this.addRandomParticle((double) event.getX(), (double) event.getY());
150 @Override
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;