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

📄 bounceselfish.java

📁 sun公司开发的,java2核心技术,卷II:高级性能,包括一系列的高级java应用技术,如数据库德连接,高级swing,多线程,软件本地化等等,本文件中则包含该书中的所用实例,配合该书使用,使对ja
💻 JAVA
字号:
/**
 * @version 1.20 1999-04-25
 * @author Cay Horstmann
 */

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class BounceSelfish
{  public static void main(String[] args)
   {  JFrame frame = new BounceSelfishFrame();
      frame.show();
   }
}

class BounceSelfishFrame extends JFrame
{  public BounceSelfishFrame()
   {  setSize(300, 200);
      setTitle("Bounce");

      addWindowListener(new WindowAdapter()
         {  public void windowClosing(WindowEvent e)
            {  System.exit(0);
            }
         } );

      Container contentPane = getContentPane();
      canvas = new JPanel();
      contentPane.add(canvas, "Center");
      JPanel p = new JPanel();
      addButton(p, "Start",
         new ActionListener()
         {  public void actionPerformed(ActionEvent evt)
            {  Ball b = new Ball(canvas, Color.black);
               b.setPriority(Thread.NORM_PRIORITY);
               b.start();
            }
         });

      addButton(p, "Selfish",
         new ActionListener()
         {  public void actionPerformed(ActionEvent evt)
            {  Ball b = new SelfishBall(canvas, Color.blue);
               b.setPriority(Thread.NORM_PRIORITY + 2);
               b.start();
            }
         });

      addButton(p, "Close",
         new ActionListener()
         {  public void actionPerformed(ActionEvent evt)
            {  canvas.setVisible(false);
               System.exit(0);
            }
         });
      contentPane.add(p, "South");
   }

   public void addButton(Container c, String title,
      ActionListener a)
   {  JButton b = new JButton(title);
      c.add(b);
      b.addActionListener(a);
   }

   private JPanel canvas;
}

class Ball extends Thread
{  public Ball(JPanel b, Color c) { box = b; color = c; }

   public void draw()
   {  Graphics g = box.getGraphics();
      g.setColor(color);
      g.fillOval(x, y, XSIZE, YSIZE);
      g.dispose();
   }

   public void move()
   {  if (!box.isVisible()) return;
      Graphics g = box.getGraphics();
      g.setColor(color);
      g.setXORMode(box.getBackground());
      g.fillOval(x, y, XSIZE, YSIZE);
      x += dx;
      y += dy;
      Dimension d = box.getSize();
      if (x < 0)
      { x = 0; dx = -dx; }
      if (x + XSIZE >= d.width)
      { x = d.width - XSIZE; dx = -dx; }
      if (y < 0)
      { y = 0; dy = -dy; }
      if (y + YSIZE >= d.height)
      { y = d.height - YSIZE; dy = -dy; }
      g.fillOval(x, y, XSIZE, YSIZE);
      g.dispose();
   }

   public void run()
   {  try
      {  draw();
         for (int i = 1; i <= 1000; i++)
         {  move();
            sleep(5);
         }
      }
      catch(InterruptedException e) {}
   }

   private JPanel box;
   private static final int XSIZE = 10;
   private static final int YSIZE = 10;
   private int x = 0;
   private int y = 0;
   private int dx = 2;
   private int dy = 2;
   private Color color;
}

class SelfishBall extends Ball
{  public SelfishBall(JPanel b, Color c) { super(b, c); }

   public void run()
   {  draw();
      for (int i = 1; i <= 1000; i++)
      {  move();
         long t = System.currentTimeMillis();
         while (System.currentTimeMillis() < t + 5)
            ;
      }
   }
}

⌨️ 快捷键说明

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