Java基础之在窗口中绘图——渐变填充(GradientApplet 1)

Applet程序。

 1 import javax.swing.*;
 2 import java.awt.*;
 3 import java.awt.geom.*;
 4 
 5 @SuppressWarnings("serial")
 6 public class GradientApplet extends JApplet {
 7   // Initialize the applet
 8     @Override
 9     public void init() {
10     GradientPane pane = new GradientPane();                            // Pane containing filled rectangles
11     getContentPane().add(pane);                                        // BorderLayout.CENTER is default position
12   }
13 
14   // Class defining a pane on which to draw
15   class GradientPane extends JComponent {
16       @Override
17       public void paint(Graphics g) {
18       Graphics2D g2D = (Graphics2D)g;
19 
20       Point2D.Float p1 = new Point2D.Float(150.f, 75.f);               // Gradient line start
21       Point2D.Float p2 = new Point2D.Float(250.f, 75.f);               // Gradient line end
22       float width = 300;
23       float height = 50;
24       GradientPaint g1 = new GradientPaint(p1, Color.WHITE,
25                                            p2, Color.DARK_GRAY,
26                                            true);                      // Cyclic gradient
27       Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x-100, p1.y-25, width,height);
28       g2D.setPaint(g1);                                                // Gradient color fill
29       g2D.fill(rect1);                                                 // Fill the rectangle
30       g2D.setPaint(Color.BLACK);                                       // Outline in black
31       g2D.draw(rect1);                                                 // Fill the rectangle
32       g2D.draw(new Line2D.Float(p1, p2));
33       g2D.drawString("Cyclic Gradient Paint", p1.x-100, p1.y-50);
34       g2D.drawString("p1", p1.x-20, p1.y);
35       g2D.drawString("p2", p2.x+10, p2.y);
36 
37       p1.setLocation(150, 200);
38       p2.setLocation(250, 200);
39       GradientPaint g2 = new GradientPaint(p1, Color.WHITE,
40                                            p2, Color.DARK_GRAY,
41                                            false);                     // Acyclic gradient
42       rect1.setRect(p1.x-100, p1.y-25, width, height);
43       g2D.setPaint(g2);                                                // Gradient color fill
44       g2D.fill(rect1);                                                 // Fill the rectangle
45       g2D.setPaint(Color.BLACK);                                       // Outline in black
46       g2D.draw(rect1);                                                 // Fill the rectangle
47       g2D.draw(new Line2D.Float(p1, p2));
48       g2D.drawString("Acyclic Gradient Paint", p1.x-100, p1.y-50);
49       g2D.drawString("p1", p1.x-20, p1.y);
50       g2D.drawString("p2", p2.x+10, p2.y);
51     }
52   }
53 }

HTML文件:

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 
 3 <html>     <head>  </head>
 4     <body bgcolor="000000">
 5         <center>
 6             <applet
 7                 code    = "GradientApplet.class"
 8                 width    = "400"
 9                 height    = "280"
10                 >
11             </applet>
12         </center>
13     </body>
14 </html>
原文地址:https://www.cnblogs.com/mannixiang/p/3488399.html