The Splat program demonstrates the use of graphical objects, creating and displaying a frame with multiple filled circles. The program uses object-oriented thinking, where each circle is an instance of the Circle class, which manages its own state (position, size, color) and can draw itself in any graphics context.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
LISTING 4.7 //******************************************************************** // Splat.javaAuthor: 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); } }
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
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.javaAuthor: 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);
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.javaAuthor: 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);
} //----------------------------------------------------------------- // 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;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
} //----------------------------------------------------------------- // 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.