1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class PictureFrame extends JFrame { 5: public void paint(Graphics g) { 6: setBackground(Color.white); // set the background of the frame 7: int x = 20; 8: int y = 50; 9: int w = 200; // width 10: int h = 100; // height 11: g.setColor(Color.black); // set the color to black; 12: g.drawRect(x, y, w, h); 13: g.setColor(Color.green); 14: g.fillOval(x, y, w, h); 15: g.setColor(Color.blue); 16: g.drawLine(x, y, x+w, y+h); 17: g.drawLine(x+w, y, x, y+h); 18: } 19: 20: public static void main(String[] args) { 21: PictureFrame f = new PictureFrame(); 22: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23: f.setSize(300,200); 24: f.setVisible(true); 25: } 26:} 27: 28:The program
PictureFrame
illustrates the basic idea about drawing
pictures.
paint(Graphics g)
. The paint method describes how
you want to paint your component.
setBackground(Color color)
sets the background color of the
componenet.
paint
method provides both
a context for painting and methods for performing the painting.
The methods, which we discuss in detail a little later, have names such as
drawImage, drawString, drawRect, and fillRect. Graphics
is java.awt.Graphics
.
g.setColor(color)
to set the color.
Color
is java.awt.Color
.
Color
class represents color in real life.
A color consists of 3 components :red, green and blue
r,g,b represent red, green and blue components.Color(int r, int g, int b)
Color(float r, float g, float b)
final static Color
variables predefined.
To access them, use
Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray,
Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink,
Color.red, Color.white, Color.yellow
.
g.setColor(Color color)
. And the color has
effect on the next drawing object.
Graphics
class defines methods for painting the following kinds of shapes:
drawLine(int x1, int y1, int x2, int y2)
:
Draws a line, using the current color, between the points (x1, y1) and (x2, y2)
in this graphics context's coordinate system.
drawRect(int x, int y, int width, int height)
:
Draws the outline of the specified rectangle.
The left and right edges of the rectangle are at x and x + width.
The top and bottom edges are at y and y + height.
fillRect(int x, int y, int width, int height)
:
Fills the specified rectangle.
The left and right edges of the rectangle are at x and x + width - 1.
The top and bottom edges are at y and y + height - 1.
The resulting rectangle covers an area width pixels wide by height pixels tall.
drawOval(int x, int y, int width, int height)
:
Draws the outline of an oval.
The result is a circle or ellipse that fits within the rectangle
specified by the x, y, width, and height arguments.
The oval covers an area that is width + 1 pixels wide and height + 1 pixels tall.
fillOval(int x, int y, int width, int height)
:
Fills an oval bounded by the specified rectangle.
The result is a circle or ellipse that fits within the rectangle
specified by the x, y, width, and height arguments.
The oval covers an area that is width + 1 pixels wide and height + 1 pixels tall.
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified
rectangle. fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class PictureFrame2 extends JFrame { 5: public void paint(Graphics g) { 6: setBackground(Color.white); // set the background of the frame 7: g.setColor(Color.black); // set the color to black; 8: g.drawRect(50, 50, 300, 100); 9: g.setColor(Color.green); 10: g.drawArc(50,50,300,100, 0, 135); 11: g.drawLine(50,50,200,100); 12: g.setColor(Color.red); 13: g.fillArc(50,50,300,100, 225,90); 14: } 15: 16: public static void main(String[] args) { 17: PictureFrame2 f = new PictureFrame2(); 18: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19: f.setSize(400,200); 20: f.setVisible(true); 21: } 22:}
1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class PictureFrame3 extends JFrame { 5: public void paint(Graphics g) { 6: setBackground(Color.white); // set the background of the frame 7: g.setColor(new Color(10,60,200)); 8: g.setFont(new Font("Dialog", Font.PLAIN, 20)); 9: g.drawString("PIC20A", 10, 50); 10: g.setColor(Color.red); 11: g.setFont(new Font("Serif", Font.ITALIC,30)); 12: g.drawString("PIC20A",10,140); 13: } 14: 15: public static void main(String[] args) { 16: PictureFrame3 f = new PictureFrame3(); 17: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18: f.setSize(150,150); 19: f.setVisible(true); 20: } 21:}
g.setColor(color)
to set the color.
g.setFont(Font font)
te set a fontg.drawString
to set the stringFont
is java.awt.Font
. It represents
fonts.
Font(String name, int style, int size)
Creates a new Font from the specified name, style and point size.
drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics context's current font and color
The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.
In our code, we use
g.setFont(new Font("Dialog", Font.PLAIN, 20));
This is equivalent to
Font font = new Font("Dialog", Font.PLAIN, 20); g.setFont(font);
Since we are not going to refer to the font
, it make the code
shorter by using the first method. The new
create an instance of
the object.