Project

General

Profile

GraphicsOpsExamples.java

Greg Shah, 08/26/2015 09:21 AM

Download (4.81 KB)

 
1
import javax.swing.*;
2
import java.awt.event.*;
3
import java.awt.*;
4

    
5
public class GraphicsOpsExamples
6
{
7
   public static void main(String[] args)
8
   {
9
      SwingUtilities.invokeLater(new Runnable()
10
      {
11
         public void run()
12
         {
13
            final int ht = 600;
14
            final int wd = 600;
15
            
16
            JFrame frame = new JFrame("Graphics Operations");
17
            frame.setSize(wd, ht);
18
            frame.setLocation(150, 150);
19
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20
            
21
            frame.setContentPane(new JComponent()
22
            {
23
               public void paint(Graphics g)
24
               {
25
                  Graphics2D g2 = (Graphics2D) g;
26
                  
27
                  // draw Y axis tick marks
28
                  for (int t = 0; t < ht; t++)
29
                  {
30
                     // major every 10 pixels
31
                     if ((t % 10) == 0)
32
                     {
33
                        g2.drawLine(0, t, 5, t);
34
                     }
35
                     // minor every 2 pixels
36
                     if ((t % 2) == 0)
37
                     {
38
                        g2.drawLine(0, t, 2, t);
39
                     }
40
                  }
41
                  
42
                  // draw X axis tick marks (can't start at 0 because the Y ticks are there)
43
                  for (int t = 10; t < wd; t++)
44
                  {
45
                     // major every 10 pixels
46
                     if ((t % 10) == 0)
47
                     {
48
                        g2.drawLine(t, 0, t, 5);
49
                     }
50
                     // minor every 2 pixels
51
                     if ((t % 2) == 0)
52
                     {
53
                        g2.drawLine(t, 0, t, 2);
54
                     }
55
                  }
56
                  
57
                  g2.drawLine(10, 10, 590, 30);
58
                  g2.drawRect(40, 40, 55, 30);
59
                  g2.fillRect(150, 40, 55, 30);
60
                  g2.drawRoundRect(40, 90, 55, 30, 5, 5);
61
                  g2.fillRoundRect(40, 130, 55, 30, 5, 5);
62
                  g2.drawRoundRect(110, 90, 55, 30, 8, 8);
63
                  g2.fillRoundRect(110, 130, 55, 30, 8, 8);
64
                  g2.drawRoundRect(180, 90, 55, 30, 10, 10);
65
                  g2.fillRoundRect(180, 130, 55, 30, 10, 10);
66
                  g2.drawRoundRect(250, 90, 55, 30, 15, 15);
67
                  g2.fillRoundRect(250, 130, 55, 30, 15, 15);
68
                  g2.drawRoundRect(320, 90, 55, 30, 20, 20);
69
                  g2.fillRoundRect(320, 130, 55, 30, 20, 20);
70
                  g2.drawRoundRect(390, 90, 55, 30, 25, 25);
71
                  g2.fillRoundRect(390, 130, 55, 30, 25, 25);
72
                  g2.drawRoundRect(460, 90, 55, 30, 30, 30);
73
                  g2.fillRoundRect(460, 130, 55, 30, 30, 30);
74
                  // g2.setColor(Color.GREEN);
75
                  g2.setColor(new Color(153, 180, 209));
76
                  g2.draw3DRect(40, 175, 75, 30, true);
77
                  g2.draw3DRect(130, 175, 150, 60, true);
78
                  g2.fill3DRect(295, 175, 75, 30, true);
79
                  g2.fill3DRect(395, 175, 150, 60, true);
80
                  g2.draw3DRect(40, 250, 75, 30, false);
81
                  g2.draw3DRect(130, 250, 150, 60, false);
82
                  g2.fill3DRect(295, 250, 75, 30, false);
83
                  g2.fill3DRect(395, 250, 150, 60, false);
84
                  g2.setColor(Color.BLACK);
85
                  g2.draw3DRect(40, 325, 75, 30, false);
86
                  g2.draw3DRect(130, 325, 150, 60, false);
87
                  g2.fill3DRect(295, 325, 75, 30, false);
88
                  g2.fill3DRect(395, 325, 150, 60, false);
89
                  
90
                  int xPoints[] = new int[] { 40, 80, 40 };
91
                  int yPoints[] = new int[] { 400, 420, 440 };
92
                  g2.fillPolygon(xPoints, yPoints, 3);
93
                  
94
                  xPoints = new int[] { 140, 100, 140 };
95
                  yPoints = new int[] { 440, 420, 400 };
96
                  g2.fillPolygon(xPoints, yPoints, 3);
97
                  
98
                  xPoints = new int[] { 160, 180, 200 };
99
                  yPoints = new int[] { 400, 440, 400 };
100
                  g2.fillPolygon(xPoints, yPoints, 3);
101
                  
102
                  xPoints = new int[] { 220, 240, 260 };
103
                  yPoints = new int[] { 440, 400, 440 };
104
                  g2.fillPolygon(xPoints, yPoints, 3);
105
                  
106
                  xPoints = new int[] { 280, 300, 320, 310, 290 };
107
                  yPoints = new int[] { 420, 440, 420, 400, 400 };
108
                  g2.fillPolygon(xPoints, yPoints, 5);
109
                  
110
                  xPoints = new int[] { 340, 360, 380, 370, 350 };
111
                  yPoints = new int[] { 420, 400, 420, 440, 440 };
112
                  g2.fillPolygon(xPoints, yPoints, 5);
113
               }
114
            });
115
            
116
            frame.setVisible(true);
117
         }
118
      });
119
   }
120
}