More examples
Smile.java
You can get the program here .
1:import javax.swing.*;
2:import java.awt.*;
3:
4:public class Smile extends JFrame {
5: public void paint(Graphics g) {
6: setBackground(new Color(110,200,130)); // just create some random color
7:
8: // draw the face
9: g.setColor(Color.yellow);
10: g.fillOval(150,150,200,190);
11:
12: // draw the boundary of the face
13: g.setColor(Color.black);
14: g.drawOval(150,150,200,190);
15:
16: //draw the eyes
17: g.setColor(Color.black);
18: g.fillOval(195,220,10,10);
19: g.fillOval(295,220,10,10);
20:
21: //draw the month
22: g.setColor(Color.black);
23: g.drawArc(200,250,100,50,-10,-160);
24:
25: //draw 2 red spots
26: g.setColor(Color.red);
27: g.fillOval(170, 265, 20,20);
28: g.fillOval(310, 265, 20,20);
29: }
30:
31: public static void main(String[] args) {
32: Smile f = new Smile();
33: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34: f.setSize(500,500);
35: f.setVisible(true);
36: }
37:}
Smile2.java
You can get the program here .
1:import javax.swing.*;
2:import java.awt.*;
3:
4:public class Smile2 extends JFrame {
5: public void paint(Graphics g) {
6: setBackground(new Color(110,200,130)); // just create some random color
7: Color rectColor = Color.blue;
8:
9: // draw the face
10: g.setColor(Color.yellow);
11: g.fillOval(150,150,200,190);
12: // draw the boundary of the face
13: g.setColor(Color.black);
14: g.drawOval(150,150,200,190);
15: // draw the rectangle for the face
16: g.setColor(rectColor);
17: g.drawRect(150,150,200,190);
18:
19: //draw the eyes
20: g.setColor(Color.black);
21: g.fillOval(195,220,10,10);
22: g.fillOval(295,220,10,10);
23: // draw the rectange for the eyes
24: g.setColor(rectColor);
25: g.drawRect(195,220,10,10);
26: g.drawRect(295,220,10,10);
27:
28: //draw the month
29: g.setColor(Color.black);
30: g.drawArc(200,250,100,50,-10,-160);
31: // draw the rectangle for the month
32: g.setColor(rectColor);
33: g.drawRect(200,250,100,50);
34:
35: //draw 2 red spots
36: g.setColor(Color.red);
37: g.fillOval(170, 265, 20,20);
38: g.fillOval(310, 265, 20,20);
39: // draw the rectangle for the red spots
40: g.setColor(rectColor);
41: g.drawRect(170, 265, 20,20);
42: g.drawRect(310, 265, 20,20);
43: }
44:
45: public static void main(String[] args) {
46: Smile2 f = new Smile2();
47: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48: f.setSize(500,500);
49: f.setVisible(true);
50: }
51:}
FunFont.java
You can get the program here .
1:import javax.swing.*;
2:import java.awt.*;
3:
4:public class FunFont extends JFrame {
5: private Font font = new Font("Serif", Font.PLAIN, 100);
6: private String string = "USA";
7: private Point point = new Point(200,300);
8: private Color color = Color.green;
9:
10: public void paint(Graphics g) {
11: setBackground(Color.pink);
12: g.setFont(font);
13: int x = point.x;
14: int y = point.y;
15: g.setColor(color.darker());
16: int thinkness = 8;
17: for(int i = 0; i < thinkness; i++) {
18: g.drawString(string, x+i,y+i);
19: }
20: g.setColor(color);
21: g.drawString(string,x+thinkness,y+thinkness);
22: }
23:
24: public static void main(String[] args) {
25: FunFont f = new FunFont();
26: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27: f.setSize(550,550);
28: f.setVisible(true);
29: }
30:}
FunFont2.java
You can get the program here .
1:import javax.swing.*;
2:import java.awt.*;
3:
4:public class FunFont2 extends JFrame {
5: private Font font = new Font("Serif", Font.BOLD, 140);
6: private String string = "IBM";
7: private Point point = new Point(50,150);
8: private Color color = Color.white;
9: private Color backColor = Color.blue;
10: private int stripeThinkness = 5;
11:
12: public void paint(Graphics g) {
13: setBackground(backColor);
14: g.setFont(font);
15: g.setColor(color);
16: int x = point.x;
17: int y = point.y;
18: g.drawString(string,x,y);
19: drawStripe(g);
20: }
21:
22: private void drawStripe(Graphics g) {
23: Dimension dim = getSize();
24: int height = dim.height;
25: int width = dim.width;
26: g.setColor(backColor);
27: for(int h = 0; h < height; h+=(2*stripeThinkness)) {
28: g.fillRect(0,h,width,stripeThinkness);
29: }
30: }
31:
32:
33: public static void main(String[] args) {
34: FunFont2 f = new FunFont2();
35: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36: f.setSize(350,200);
37: f.setVisible(true);
38: }
39:}