Students who complete this lab will demonstrate that they can:
|
In this lab, you will integrate the following Processing application into the Java GUI framework. Start by
re-familiarizing yourself with Processing applications, focusing in particular on this application: Particles. You can run this program by putting it in a folder named
Take note of the following features of this Processing program:
|
![]() |
Java GUI applications can be implemented using the Java Foundation Classes (JFC).
The exercises in this lab tend to require detailed implementation and configuration. Use the examples from the text and the lectures as guides.
|
Create an appropriate package for lab 11 and build a new class named
Use the following algorithm:
This basic GUI code can largely be copied from application to another. See the text and the lectures for further examples. Save this code and continue to extend it from one exercise to the next in this lab. |
![]() |
Because Processing is Java-based and Processing applications are basically Java widgets, it is possible to integrate a Processing application within a Java GUI application. You can do this using the procedure outlined in the text and demonstrated in the lectures.
Integrate the Processing application shown above into your “empty” JFrame GUI as follows:
ParticlePanel that extends PApplet. You will also need
to add an appropriate import statement.
setup() and draw()
methods, and private for the utility methods).
setup()
to this new constructor.
ParticleController:
ParticlePanel, which you created in the previous
step.
ParticlePanel
default constructor, start the Processing mechanism (using init()) and add the new panel object to
the JFrame (using add()).
Run the application in your Java IDE to verify that it runs as it did when it ran in the Processing IDE.
Processing does not provide very useful control widgets for its graphical applications. Java GUIs do.
|
Add “Pause” and “Restart” buttons to the interface and program them to pause and restart the animation. The result should look something like the image shown on the right.
The text and lectures provide detailed examples of how to do this. When finished, run the application to verify that the new pause and restart functions work properly. |
![]() |
For more information on JFC capabilities, see the following two reference sources:
- This tutorial provides a visual guide to the look and feel of the Swing GUI components and a set of tutorials on
how to use them.
- This is the definitive reference for the application programmers interface (API) to all of
Java’s classes. To use this API reference, you can search for the class you’re interested in (e.g., JButton
) in the “All Classes” list and read the documentation on the constructor methods and instance methods
that it provides. This reference is formatted using JavaDoc.
Graphical applications frequently require the use of supplemental, graphical classes. For example, the current
particles application supports a single particle, described by individual instance variables in the
ParticlePanel
class (e.g.,
particleX
and
particleY
) and specialized methods (i.e.,
renderParticle()
and
moveParticle()
). It would be useful to have a separate particle class that encapsulates these attributes and behaviors for a single
particle object. This would allow us to model multiple particles and to model their interactions (e.g., collisions).
In this section, you will build such a class.
Extract the elements required to represent and model particle objects from the
ParticlePanel
into their own
Particle
class. Do this as follows:
Particle class.
ParticlePanel class into the new
Particle class:
drawParticle() and moveParticle() methods - Rename these methods as
appropriate for inclusion in the Particle class, e.g., as render() and move().
render() to receive a PApplet from the calling program and use this
object to perform the Processing-specific methods in the definition.
ParticlePanel to:
Particle object as an instance variable
Particle class’s move() and render() methods in the
draw() method (using move(SIZE, SIZE) and render(this) respectively).
this, an identifier that refers to the current object, in
this case the ParticlePanel object. This so-called “magic pen” allows the Particle
object’s render() to draw on the panel's sketch canvas.
ParticlePanel should keep the following:
Rerun the application to verify that it still behaves in the same way as it did before extracting the
Particle
class.
The previous exercise gives another example of refactoring; that is, re-implementing an application in an alternate way that produces the same behavior. The goal of refactoring is either to improve the efficiency or understandability of the code or to prepare for future development.
Now that you have a particle class, it is much easier to modify the particles application to support multiple particles.
|
Modify the
Note here that the
|
![]() |
Now we’d like to modify the particles to interact with one another by bouncing off one another. This requires
that you change both the
Particle
class, whose objects will need to know when they are “hitting” other objects, and the
ParticlePanel
, which must ask each particle that is hitting another particle to “bounce” off of that particle.
|
Modify the application to model colliding particles, as shown to the right. Do this by making the following modifications:
Your application should now implement multiple, interacting particles. |
![]() |
Submit the final version of your particle collider; the intermediate steps along the way are not required. We will grade this exercise according to the following criteria:
ParticleController - This class should construct the interface shown above and implement
its behavior as specified.
ParticlePanel - This class should faithfully copy the behavior of the original
Processing application.
Particle - This class should encapsulate the required particle attributes and behaviors.