⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boxespanel.java

📁 Java 程序设计教程(第五版)EXAMPLESchap05源码
💻 JAVA
字号:
//********************************************************************
//  BoxesPanel.java       Author: Lewis/Loftus
//  
//  Demonstrates the use of conditionals and loops to guide drawing.
//********************************************************************

import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;

public class BoxesPanel extends JPanel
{
   private final int NUM_BOXES = 50, THICKNESS = 5, MAX_SIDE = 50;
   private final int MAX_X = 350, MAX_Y = 250;
   private Random generator;

   //-----------------------------------------------------------------
   //  Sets up the drawing panel.
   //-----------------------------------------------------------------
   public BoxesPanel ()
   {
      generator = new Random();

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 300));
   }

   //-----------------------------------------------------------------
   //  Paints boxes of random width and height in a random location.
   //  Narrow or short boxes are highlighted with a fill color.
   //-----------------------------------------------------------------
   public void paintComponent(Graphics page)
   {
      super.paintComponent (page);

      int x, y, width, height;

      for (int count = 0; count < NUM_BOXES; count++)
      {
         x = generator.nextInt(MAX_X) + 1;
         y = generator.nextInt(MAX_Y) + 1;

         width = generator.nextInt(MAX_SIDE) + 1;
         height = generator.nextInt(MAX_SIDE) + 1;

         if (width <= THICKNESS)  // check for narrow box
         {
            page.setColor (Color.yellow);
            page.fillRect (x, y, width, height);
         }
         else
            if (height <= THICKNESS)  // check for short box
            {
               page.setColor (Color.green);
               page.fillRect (x, y, width, height);
            }
            else
            {
               page.setColor (Color.white);
               page.drawRect (x, y, width, height);
            }
      }
   }
}

⌨️ 快捷键说明

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