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

📄 ball.java

📁 平时自己上课时候做的JAV实验代码
💻 JAVA
字号:


package javaapplication7;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Ball extends Thread
{
    MainPanel mainpanel;
    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;
    
    
    /** Creates a new instance of Ball */
    public Ball(MainPanel b) 
    {
        mainpanel=b;
    }
    
    public void draw()
    {
        Graphics g=mainpanel.getGraphics();
        g.fillOval(x,y,XSIZE,YSIZE);
        g.dispose();
     }
    public void move()
    {
        if(!mainpanel.isVisible())
            return;
        x+=dx;
        y+=dy;
        Dimension d=mainpanel.getSize();
        if(x<0){x=0;dx=-dx;}
        if(y<0){y=0;dy=-dy;}
        if(x+XSIZE>=d.width){x=d.width-XSIZE;dx=-dx;}
        if(y+YSIZE>=d.height){y=d.height-YSIZE;dy=-dy;}
        mainpanel.repaint();
    }
    
    public void run(){
        try
        {
            draw();
            for(int i=0;i<=1000;i++)
            {
                move();
                sleep(50);
            }
        }
        catch(Exception e){}
    }
    
}

⌨️ 快捷键说明

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