Biblioteca Java - Blame information for rev 32
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
32 | mihai | 1 | /* |
2 | * To change this license header, choose License Headers in Project Properties. | ||
3 | * To change this template file, choose Tools | Templates | ||
4 | * and open the template in the editor. | ||
5 | */ | ||
6 | package guidemo; | ||
7 | |||
8 | import javax.swing.*; | ||
9 | import java.awt.*; | ||
10 | |||
11 | /** | ||
12 | * Example demonstrating drawPolyline(). | ||
13 | */ | ||
14 | public class SinExample extends JFrame { | ||
15 | |||
16 | public SinExample() { | ||
17 | |||
18 | this.setSize(new Dimension(300, 200)); | ||
19 | int width = getSize().width; | ||
20 | int height = getSize().height; | ||
21 | |||
22 | int num_points = 21; | ||
23 | |||
24 | // Create an instance of DrawingPanel | ||
25 | Polygon1Panel polygon1_panel | ||
26 | = new Polygon1Panel(width, height, num_points); | ||
27 | // Add the DrawingPanel to the contentPane. | ||
28 | add(polygon1_panel); | ||
29 | pack(); | ||
30 | setVisible(true); | ||
31 | |||
32 | } | ||
33 | |||
34 | public static void main(String[] args) { | ||
35 | new SinExample(); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Draw a polygon with drawPolyline() on this JPanel subclass. * | ||
41 | */ | ||
42 | class Polygon1Panel extends JPanel { | ||
43 | |||
44 | int fWidth, fHeight; | ||
45 | int fNumPoints; | ||
46 | double fFactor; | ||
47 | |||
48 | Polygon1Panel(int width, int height, int nPoints) { | ||
49 | fNumPoints = nPoints; | ||
50 | fWidth = width; | ||
51 | fHeight = height; | ||
52 | fFactor = 2.0 * Math.PI / fWidth; | ||
53 | this.setPreferredSize(new Dimension(width, height)); | ||
54 | } // ctor | ||
55 | |||
56 | public void paintComponent(Graphics g) { | ||
57 | // First paint background unless you will | ||
58 | // paint whole area yourself. | ||
59 | super.paintComponent(g); | ||
60 | |||
61 | // Create arrays of points for each | ||
62 | // segment of the polygon | ||
63 | int[] x = new int[fNumPoints]; | ||
64 | int[] y = new int[fNumPoints]; | ||
65 | |||
66 | // Select horizontal step size | ||
67 | double x_del = ((double) fWidth) / (fNumPoints - 1); | ||
68 | |||
69 | // Find coordinates of the display center | ||
70 | int x_offset = fWidth / 2; | ||
71 | int y_offset = fHeight / 2; | ||
72 | |||
73 | // Choose amplitude for the sine curve | ||
74 | int amp = (int) (y_offset * 0.9); | ||
75 | |||
76 | // Create a sine curve from a sequence | ||
77 | // of short line segments | ||
78 | for (int i = 0; i < fNumPoints; i++) { | ||
79 | x[i] = (int) (i * x_del); | ||
80 | y[i] = (int) (amp * Math.sin(fFactor * x[i])) | ||
81 | + y_offset; | ||
82 | } | ||
83 | |||
84 | // Set the line color to red | ||
85 | g.setColor(Color.red); | ||
86 | |||
87 | // Draw curve with single call to drawPolyline | ||
88 | g.drawPolyline(x, y, fNumPoints); | ||
89 | |||
90 | // Change the line color and draw the x-y axes | ||
91 | g.setColor(Color.green); | ||
92 | g.drawLine(0, y_offset, fWidth - 1, y_offset); | ||
93 | g.drawLine(x_offset, 0, x_offset, fHeight - 1); | ||
94 | |||
95 | } // paintComponent | ||
96 | |||
97 | } // class Polygon1Panel |