Splat Program Implementation

Verified

Added on  2019/09/16

|5
|1704
|173
Practical Assignment
AI Summary
This practical assignment involves implementing the "Splat" program in Java, which demonstrates fundamental object-oriented programming concepts. The program draws multiple circles on a panel, each circle represented by its own object of the `Circle` class. The `Circle` class encapsulates data (diameter, color, x, y coordinates) and methods to draw itself. The `SplatPanel` class manages an array of `Circle` objects and uses the `paintComponent` method to draw them. The main `Splat` class sets up the graphical user interface (GUI) frame. This assignment showcases object encapsulation, instance data, and method calls, reinforcing core OOP principles. The provided code listings (4.7, 4.8, and 4.9) detail the implementation of the `Splat`, `SplatPanel`, and `Circle` classes respectively. The assignment focuses on understanding how each class interacts and contributes to the overall functionality of the program.
Document Page
LISTING 4.7
//********************************************************************
// Splat.java Author: Lewis/Loftus
//
// Demonstrates the use of graphical objects.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class Splat
{
//-----------------------------------------------------------------
// Presents a collection of circles.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Splat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SplatPanel());
frame.pack();
frame.setVisible(true);
}
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Let’s look at another example. The Splat class shown in Listing 4.7 contains a main method that
creates and displays the frame for the program. Visually, this program simply draws a few filled
circles. The interesting thing about this program is not what it does, but how it does it—each
circle drawn in this program is represented by its own object.
The main method instantiates a SplatPanel object and adds it to the frame. The SplatPanel class
is shown in Listing 4.8. Like the SmilingFacePanel class in the previous example, the SplatPanel
class is derived from JPanel. It holds as instance data five Circle objects, which are instantiated
in the constructor.
LISTING 4.8
//********************************************************************
// SplatPanel.java Author: Lewis/Loftus
//
// Demonstrates the use of graphical objects.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class SplatPanel extends JPanel
{
private Circle circle1, circle2, circle3, circle4, circle5;
//-----------------------------------------------------------------
// Constructor: Creates five Circle objects.
//-----------------------------------------------------------------
public SplatPanel()
{
circle1 = new Circle(30, Color.red, 70, 35);
circle2 = new Circle(50, Color.green, 30, 20);
circle3 = new Circle(100, Color.cyan, 60, 85);
circle4 = new Circle(45, Color.yellow, 170, 30);
circle5 = new Circle(60, Color.blue, 200, 60);
setPreferredSize(new Dimension(300, 200));
setBackground(Color.black);
}
//-----------------------------------------------------------------
// Draws this panel by requesting that each circle draw itself.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
Document Page
circle4.draw(page);
circle5.draw(page);
}
}
The paintComponent method in the SplatPanel class draws the panel by calling the draw
method of each circle. Essentially, the SplatPanel class asks each circle to draw itself.
The Circle class is shown in Listing 4.9. It defines instance data to store the size of the circle, its
(x, y) location, and its color. These values are set using the constructor, and the class contains
all the appropriate accessor and mutator methods. The draw method of the Circle class simply
draws the circle based on the values of its instance data (its current state).
LISTING 4.9
//********************************************************************
// Circle.java Author: Lewis/Loftus
//
// Represents a circle with a particular position, size, and color.
//********************************************************************
import java.awt.*;
public class Circle
{
private int diameter, x, y;
private Color color;
//-----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
//-----------------------------------------------------------------
public Circle(int size, Color shade, int upperX, int upperY)
{
diameter = size;
color = shade;
x = upperX;
y = upperY;
}
//-----------------------------------------------------------------
// Draws this circle in the specified graphics context.
//-----------------------------------------------------------------
public void draw(Graphics page)
{
page.setColor(color);
page.fillOval(x, y, diameter, diameter);
Document Page
}
//-----------------------------------------------------------------
// Diameter mutator.
//-----------------------------------------------------------------
public void setDiameter(int size)
{
diameter = size;
}
//-----------------------------------------------------------------
// Color mutator.
//-----------------------------------------------------------------
public void setColor(Color shade)
{
color = shade;
}
//-----------------------------------------------------------------
// X mutator.
//-----------------------------------------------------------------
public void setX(int upperX)
{
x = upperX;
}
//-----------------------------------------------------------------
// Y mutator.
//-----------------------------------------------------------------
public void setY(int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
// Diameter accessor.
//-----------------------------------------------------------------
public int getDiameter()
{
return diameter;
}
//-----------------------------------------------------------------
// Color accessor.
//-----------------------------------------------------------------
public Color getColor()
{
return color;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
//-----------------------------------------------------------------
// X accessor.
//-----------------------------------------------------------------
public int getX()
{
return x;
}
//-----------------------------------------------------------------
// Y accessor.
//-----------------------------------------------------------------
public int getY()
{
return y;
}
}
The Splat program embodies fundamental object-oriented thinking. Each circle manages itself
and will draw itself in whatever graphics context you pass it. Each Circle object maintains its
own state. The Circle class is defined in a way that can be used in other situations and
programs.
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon