Ask a question to Desklib · AI bot


Using Graphical Objects in Java: Splat Program Example

Added on -2019-09-16

The Splat program demonstrates the use of graphical objects in Java. Each circle is represented by its own object, which maintains its own state. The Circle class is defined in a way that can be used in other situations and programs.
| 5 pages
| 1704 words

Trusted by 2+ million users,
1000+ happy students everyday

LISTING 4.7//******************************************************************** // 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); } }
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 classis 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//******************************************************************** // 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,, 70, 35); circle2 = new Circle(50,, 30, 20); circle3 = new Circle(100, Color.cyan, 60, 85); circle4 = new Circle(45, Color.yellow, 170, 30); circle5 = new Circle(60,, 200, 60); setPreferredSize(new Dimension(300, 200)); setBackground(; } //----------------------------------------------------------------- // 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);

Found this document preview useful?

You are reading a preview
Upload your documents to download
Become a Desklib member to get accesss

Students who viewed this