panel2dtest.java

来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 124 行

JAVA
124
字号
     import java.applet.*;
     import java.awt.*;

     public class Panel2DTest extends Applet implements Runnable
     {
          // 动画线程
          private Thread animation;

          private BufferedGraphics offscreen;

          // 用于容纳其他组件的Panel2D对象数组
          private Panel2D[] panels;

          // 摆放上述Panel2D对象的坐标
          private Vector2D[] panelPos = { new Vector2D.Double(10, 10), new Vector2D.Double(200, 100) };
          private final int NUM_PANELS = panelPos.length;

          public void init()
          {               
               // 创建一个摆放透明按钮的图像组
               ButtonImageGroup biGroup = new ButtonImageGroup(3, "xpbuttons.gif");
               biGroup.init(this);

               // 为单选按钮创建一个图像组
               ButtonImageGroup rbGroup = new ButtonImageGroup(2, "radio2.gif");
               rbGroup.init(this);

               // 记住设置observer属性,这样面板可以访问它的背景图像的宽和高
               AnimationStrip.observer = this;

               // 按钮,单选按钮和标签,用来创建组件来添加到我们的面板上
               Button2D button;
               RadioButton2D radioButton;
               Label2D label;

               Font font = new Font("Helvetica", Font.PLAIN, 16);

               // 为面板创建背景图像
               Image img = new ImageLoader(this, "panel.gif", true).getImage();

               // 创建面板,并把它们添加到画面上
               panels = new Panel2D[NUM_PANELS];
               for(int i = 0; i < NUM_PANELS; i++)
               {
                    // 用背景图像和索引给定的位置创建面板
                    panels[i] = new Panel2D(img, panelPos[i]);

                    // 在面板上添加单选按钮和两个一般按钮                                                           
                    radioButton = new RadioButton2D(null, rbGroup, null);
                    panels[i].add(radioButton, 95, 95);
                    radioButton.setSelected(true);
                    addMouseListener(radioButton);

                    label = new Label2D(font, "Java!", Color.WHITE);
                    button = new Button2D(label, biGroup);
                    panels[i].add(button, 25, 25);
                    label.centerOn(button.getBounds(), (Graphics2D) getGraphics());
                    addMouseListener(button);
                    addMouseMotionListener(button);

                    label = new Label2D(font, "Java!", Color.WHITE);
                    button = new Button2D(label, biGroup);
                    panels[i].add(button, 130, 185);
                    label.centerOn(button.getBounds(), (Graphics2D) getGraphics());
                    addMouseListener(button);
                    addMouseMotionListener(button);
               }

               offscreen = new BufferedGraphics(this);

          }   // init


          public void start()
          {
              // 启动动画线程
              animation = new Thread(this);
              animation.start();
          }

          public void stop() 
          {
              animation = null;
          }

          public void run() 
          {
               Thread t = Thread.currentThread();
               while (t == animation)
               {
                    repaint();

                    try
                    {     Thread.sleep(10);
                    }
                    catch(InterruptedException e) 
                    {     break;
                    }
                }              

           }   // run

          
           public void update(Graphics g)
           {
               paint(g);
           }
 
           public void paint(Graphics g) 
           {
               Graphics2D bg = (Graphics2D)offscreen.getValidGraphics();
               bg.setPaint(Color.BLACK);
               bg.fillRect(0, 0, getSize().width, getSize().height);

               // 绘制面板
               for(int i = 0; i < NUM_PANELS; i++)
               {
                    panels[i].paint(bg);
               }
               
               g.drawImage(offscreen.getBuffer(), 0, 0, this);
           }    // paint
        
     }     // Panel2DTest   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?