Lab3: Graphics and Painting
Most of the codes and activities source:
Introduction to Programming Using Java, Sixth Edition
Some important reminders about painting
- "Every component is responsible for drawing itself. If you want to use a standard component, you only have to add it to your applet or frame. You don't have to worry about painting it on the screen. That will happen automatically, since it already knows how to draw itself."
- "Sometimes, however, you do want to draw on a component. You
will have to do this whenever you want to
display something that is not included among
the standard, pre-defined component
classes. When you want to do this, you have to
define your own component class and provide a
method in that class for drawing the
component."
Activity
To make what is shown in demo, you could use the below structure of
the code: (This is just an example, you could create your own)
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MoodClass extends JPanel implements MouseListener, ActionListener{
public MoodClass(){
// TODO: MoodClass Constructor
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //all the basic behaviours are taking care of
// TODO: Do all your painings on the screen here
}
public static void main(String[] args) {
// TODO: Make your JFrame here
// TODO: Make an object of your MoodClass which is a JPanel
// TODO: Make the JFrame to show your content
}//main
}
Hints:
- What happens if the content of the components or your painings
needs to be changed in the middle of the program? You should
not call paintComponent() directly to make the change; this
method is meant to be called only by the system. Instead, you have
to inform the system that the component needs to be redrawn, and
let the system do its job by calling paintComponent(). You do this
by calling the component's repaint() method.
- The paintComponent() method must be smart enough to correctly
redraw all the paintings at any time. To make this possible, a
program should store data in its instance variables about the state
of the component. These variables should contain all the
information necessary to redraw the drawings completely. The
paintComponent() method should use the data in these variables to
decide what to draw. When the program wants to change the content
of the component, it should not simply draw the new content. It
should change the values of the relevant variables and call
repaint(). When the system calls paintComponent(), that method will
use the new values of the variables and will draw the component
with the desired modifications.
- To know the size of your JPanel your could call:
(your JPanel object).getWidth();
(your JPanel object).getHeight();
-
If you want to use properties of Graphics2D, you could change your
graphics to:
Graphics2D g2 = (Graphics2D) g;
-
There are different methods for drawing:
(Your Graphics2D object).drawString(...);
(Your Graphics2D object).fill(...);
(Your Graphics2D object).draw(...);
For this activity, you need to draw the following shapes:
Ellipse2D
Arc2D
Rectangle2D
Example: making a filled ellipse:
g2.fill(new Ellipse2D.Double(x, y, width , height));