Java: Example - Circles
|
|
This applet/application on the left draws random circles where the mouse is clicked. Try it. The main program shows how to write a program that can be run as both an application and an applet. A subclass of JComponent is defined to listen to mouse clicks and draw random circles. The third file is used to represent and draw a circle. |
The applet / main program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// File : CircleMain.java - Applet/main program
// Purpose: Illustrate the following:
// * A program that is both an applet and an application.
// * How to do graphics by subclassing JComponent.
// * Implementing a mouse listener.
// Tag: <applet code="CircleMain.class" width="200" height="200"></applet>
// Author : Fred Swartz - 2006-11-30 - Placed in public domain.
import java.awt.*;
import javax.swing.*;
///////////////////////////////////////////////////////////// CircleMain
public class CircleMain extends JApplet {
//====================================================== constructor
public CircleMain() {
//... Set layout and add our new component.
setLayout(new BorderLayout());
add(new CirclePanel(200), BorderLayout.CENTER);
}
//============================================================= main
public static void main(String[] args) {
JFrame window = new JFrame("Circles");
window.setContentPane(new CircleMain());
window.pack();
window.setLocationRelativeTo(null); // Center window.
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
}
}
|
Defining our own graphics component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// File : CirclePanel.java - Draws random circles where mouse clicked.
// Purpose: Show how to define a component to display graphics and
// use a mouse listener.
//
// Issues : What's a little strange is that this component simply records
// the generated circles in an ArrayList in itself.
// What would normally happen is that the user interaction would
// change a "model", the logic of the program. The model would
// be a separate object that represented the logical structure
// that the user is manipulating. The closest that this small
// brain-dead application comes to a model is the ArrayList of
// circles.
// Author : Fred Swartz - 2006-11-30 - Placed in the public domain.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//////////////////////////////////////////////////////////////// CirclePanel
public class CirclePanel extends JComponent implements MouseListener {
//=============================================================== fields
private int _size; // Height/width of component.
private ArrayList<Circle> _circles = new ArrayList<Circle>();
//========================================================== constructor
public CirclePanel(int size) {
_size = size;
setPreferredSize(new Dimension(_size, _size));
addMouseListener(this); // This component listens to mouse events.
}
//======================================================== paintComponent
@Override public void paintComponent(Graphics g) {
//... Draw background.
g.setColor(Color.WHITE);
g.fillRect(0, 0, _size, _size);
//... Draw each of the circles.
for (Circle c : _circles) {
c.draw(g);
}
}
//====================================================== mouse listeners
//... Ignore all mouse events except mouseClicked.
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
//... Generate random color and size circle at mouse click point.
Color randColor = new Color((int)(256*Math.random()),
(int)(256*Math.random()),
(int)(256*Math.random()));
int randRadius = (int) (_size/4 * Math.random());
Circle c = new Circle(e.getPoint(), randRadius, randColor);
//... Save this circle for painting later.
_circles.add(c);
//... Cause screen to be repainted.
repaint();
}
}
|
A class for representing a circle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// File : Circle.java - Stores and draws a circle.
// Purpose: Shows a simple class that saves and draws a circle.
// Author : Fred Swartz - 2006-11-30 - Placed in public domain.
import java.awt.*;
///////////////////////////////////////////////////////////////// Circle
class Circle {
//=========================================================== fields
int _x; // x coord of bounding rect upper left corner.
int _y; // y coord of bounding rect upper left corner.
int _diameter; // Height and width of bounding rectangle.
Color _color;
//====================================================== constructor
Circle(Point center, int radius, Color color) {
//... Change user oriented parameters into more useful values.
_x = center.x - radius;
_y = center.y - radius;
_diameter = 2 * radius;
_color = color;
}
//============================================================= draw
void draw(Graphics g) {
//... Should we save and restore the previous color?
g.setColor(_color);
g.fillOval(_x, _y, _diameter, _diameter);
}
}
|