• Java基础之在窗口中绘图——绘制星星(StarApplet 1)


    Applet程序。

    可以把更复杂的几何形状定义为GeneralPath类型的对象。GeneralPath可以是直线、Quad2D曲线和Cubic2D曲线的结合体,甚至可以包含其他GeneralPath对象。

    1、绘制星星:

     1 import java.awt.geom.*;
     2 
     3 public class Star {
     4   // Return a path for a star at x,y
     5   public static GeneralPath starAt(float x, float y) {
     6     Point2D.Float point = new Point2D.Float(x, y);
     7     p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
     8     p.moveTo(point.x, point.y);
     9     p.lineTo(point.x + 20.0f, point.y - 5.0f);                         // Line from start to A
    10     point = (Point2D.Float)p.getCurrentPoint();
    11     p.lineTo(point.x + 5.0f, point.y - 20.0f);                         // Line from A to B
    12     point = (Point2D.Float)p.getCurrentPoint();
    13     p.lineTo(point.x + 5.0f, point.y + 20.0f);                         // Line from B to C
    14     point = (Point2D.Float)p.getCurrentPoint();
    15     p.lineTo(point.x + 20.0f, point.y + 5.0f);                         // Line from C to D
    16     point = (Point2D.Float)p.getCurrentPoint();
    17     p.lineTo(point.x - 20.0f, point.y + 5.0f);                         // Line from D to E
    18     point = (Point2D.Float)p.getCurrentPoint();
    19     p.lineTo(point.x - 5.0f, point.y + 20.0f);                         // Line from E to F
    20     point = (Point2D.Float)p.getCurrentPoint();
    21     p.lineTo(point.x - 5.0f, point.y - 20.0f);                         // Line from F to g
    22     p.closePath();                                                     // Line from G to start
    23     return p;                                                          // Return the path
    24   }
    25 
    26   private static GeneralPath p;                                        // Star path
    27 }

    2、定义用来绘制星星的Applet:

     1 import javax.swing.*;
     2 import java.awt.*;
     3 
     4 @SuppressWarnings("serial")
     5 public class StarApplet extends JApplet {
     6   // Initialize the applet
     7   @Override
     8   public void init() {
     9     StarPane pane = new StarPane();                                    // Pane containing stars
    10     getContentPane().add(pane);                                        // BorderLayout.CENTER is default position
    11   }
    12 
    13   // Class defining a pane on which to draw
    14   class StarPane extends JComponent {
    15     @Override
    16     public void paint(Graphics g) {
    17       Graphics2D g2D = (Graphics2D)g;
    18       float delta = 60f;                                               // Increment between stars
    19       float starty = 0f;                                               // Starting y position
    20 
    21       // Draw 3 rows of stars
    22       for(int yCount = 0 ; yCount < 3 ; yCount++) {
    23         starty += delta;                                               // Increment row position
    24         float startx = 0f;                                             // Start x position in a row
    25 
    26         // Draw a row of 4 stars
    27         for(int xCount = 0 ; xCount < 4 ; xCount++) {
    28           g2D.draw(Star.starAt(startx += delta, starty));
    29         }
    30       }
    31     }
    32   }
    33 }

    3、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    = "StarApplet.class"
     8                 width    = "360"
     9                 height    = "240"
    10                 >
    11             </applet>
    12         </center>
    13     </body>
    14 </html>
  • 相关阅读:
    svn 指定不更新目录
    C# 修改win环境变量 来加载dll库
    基本组件
    在SD卡上创建/删除文件夹 使用DDMS透视图管理SD卡
    linux中图形界面改成文本
    表格布局和线性布局
    文件操作
    静态库的生成和调用
    船载电子海图系统(E C S )概述
    GPS全球定位系统构成及原理
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3488394.html
Copyright © 2020-2023  润新知