重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联专注于东洲网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供东洲营销型网站建设,东洲网站制作、东洲网页设计、东洲网站官网定制、小程序制作服务,打造东洲网络公司原创品牌,更为您提供东洲网站排名全网营销落地服务。
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
public class PinBall
{
private final int TABLE_WIDTH = 300;//桌面宽度
private final int TABLE_HEIGHT = 400;//桌面高度
private final int RACKET_Y = 340;//球拍的垂直位置
private final int RACKET_HEIGHT = 20;//球拍高度
private final int RACKET_WIDTH = 60;//球拍宽度
private final int BALL_SIZE = 16;//球的大小
private Frame f = new Frame("弹球游戏");//实例化一个窗口
Random rand = new Random();//实例化一个随机数生成器
private int ySpeed = 10;//小球的纵向运动数度、
private double xyRate = rand.nextDouble() - 0.5;//返回一个-0.5到0.5之间的比率用控制小球运动方向
private int xSpeed = (int)(ySpeed*xyRate*2);//这个横向速度在-10到10之间,产生左右摆动运动效果
private int ballX = rand.nextInt(200)+20;//小球开始的横坐标位置,200表示产生0到100之间的随机数
private int ballY = rand.nextInt(10)+20;//小球开始的纵坐标位置
private int racketX = rand.nextInt(200);//球拍开始时的横坐标位置
private MyCanvas tableArea = new MyCanvas();//实力化一个画布工具,集成Canvas类
private String shape = "";//保存需要绘制图形的字符串属性
Timer timer;//声明一个时间变量
private boolean isLose = false;//表示游戏是否结束
public void init()
{
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));//定义画布大小
f.add(tableArea);//添加画布到窗口
KeyAdapter keyProcessor = new KeyAdapter()//实例化一个键盘监听事件适配器
{
public void keyPressed(KeyEvent ke)//重写适配器里面的按下某键盘方法
{
if(ke.getKeyCode()==KeyEvent.VK_LEFT)//按下键盘左键时
{
if(racketX 0)//球拍左边框不能出画布的左边框
racketX -=10;//按一左键次向左移动10个像素
}
if(ke.getKeyCode()==KeyEvent.VK_RIGHT)//按下键盘右键时
{
if(racketX TABLE_WIDTH - RACKET_WIDTH)//球拍右边框不能出画布的右边框
racketX +=10;//按一次右键移动向右移动10个像素
}
}
};
f.addKeyListener(keyProcessor);//给窗口添加键盘监听器
tableArea.addKeyListener(keyProcessor);//给画布添加键盘监听器
ActionListener taskPerformer = new ActionListener()//这里是实例化了一个监听接口,这个接口里面只有一个方法
{
public void actionPerformed(ActionEvent evt)//重写这个接口里面的方法,判断小球的位置
{
if(ballX=0 || ballX=TABLE_WIDTH-BALL_SIZE)//保证小球横向上在画布之内运动
{
xSpeed = -xSpeed;//触发反方向运动
}
if(ballY=RACKET_Y-BALL_SIZE(ballXracketX||ballXracketX+RACKET_WIDTH))//出了球拍的可击打范围
{
timer.stop();//停止对监听器的触发
isLose=true;//将标志isLose变量置为true
tableArea.repaint();//调用画布的重绘方法
}
else if(ballY=0||(ballY=RACKET_Y-BALL_SIZEballYracketXballX=racketX+RACKET_WIDTH))//小球在球拍之内,而其到达球拍的高度
{
ySpeed=-ySpeed;//上下方向改变,小球反弹
}
ballY+=ySpeed;//小球的坐标在纵向上增加
ballX+=xSpeed;//小球的坐标在横向上的增加
tableArea.repaint();//调用画布的重绘方法3
}
};
timer = new Timer(100,taskPerformer);//每隔0.1秒运行一次监听器
timer.start();//计时器开始运行
f.addWindowListener(new MyListener());//关闭窗口事件
f.pack();//设置窗口最佳大小
f.setVisible(true);//显示窗口
}
class MyListener extends WindowAdapter//关闭窗口的类
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)//程序入口
{
new PinBall().init();//调用PinBall类里面的init()方法
}
class MyCanvas extends Canvas//建一个集成Canvas类的类
{
public void paint(Graphics g)//重写父类的绘图方法
{
if(isLose)//如果isLose为真,则在画布里打印“游戏已结束”
{
g.setColor(new Color(255,0,0));//当前颜色
g.setFont(new Font("黑体",Font.BOLD,30));//字体名称,样式,大小
g.drawString("游戏已结束!",50,200);//按坐标绘制文字图形
}
else//负责
{
g.setColor(new Color(240,240,80));//当前颜色
g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE);//填充颜色,根据坐标和长宽填充圆形
g.setColor(new Color(80,80,200));//当前颜色
g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);//填充颜色,根据坐标和长宽填充矩形
}
}
}
}
//简单的做个
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Circle extends JFrame {
public Circle(){
super();
CirclePanel panel=new CirclePanel();
add(panel, "Center");
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new Circle();
}
class CirclePanel extends JPanel{
public static final double PI=Math.PI;
private int degree=0;
private int axisx;
private int axisy;
public CirclePanel(){
setSize(500, 500);
axisx=getWidth()/2;
axisy=getHeight()/2;
setVisible(true);
Timer timer=new Timer(10,new TimerListener());
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(axisx, axisy, 2, 2);
g.drawOval((int)(axisx-100+5), (int)(axisy-100+5), 200, 200);
g.fillOval(-(int)(100*Math.sin(PI*degree/180))+axisx,
(int)(100*Math.cos(PI*degree/180))+axisy, 10, 10);
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
degree += 1;
repaint();
}
}
}
}
05年写的,你修改一下吧
/*
* 一个在窗体中来回运动的圆.java
*
* Created on 2005年10月5日, 下午1:02
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package javaapplication1;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.applet.*;
import java.awt.*;
import java.math.*;
/**
*
* @author Bachelorlrz
*/
public class 在窗体中来回运动的圆 extends java.applet.Applet implements java.lang.Runnable {
int cx,cy,c1x,c1y; //圆的坐标
int cw,ch; //圆的宽高
int bx,by,bw,bh; //背景的坐标和宽高
int dx,dx1; //圆的运动
int r,rx,ry; //圆的半径和位置
int r1,r1x,r1y;
Thread u_thread;
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
rx=150; ry=160;
bw=500; bh=400;
r1x=bw/2-cw*2; r1y=bh/2-ch;
r=60; r1=200;
cx =rx; cy =ry;
c1x=r1x; c1y=r1y;
cw=30; ch=30;
bx=2; by=2;
dx=1; dx1=2;
// TODO start asynchronous download of heavy resources
}
// TODO overwrite start(), stop() and destroy() methods
public void update(java.awt.Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawRect(bx,by,bw,bh);
g.setColor(Color.BLACK);
g.fillRect(bx+2,by+2,bw-4,bh-4);
g.drawString("在窗体中来回运动的圆", bw/2-60, bh/2);
if (cxrx-r || cxrx+r) {
dx = -dx;
}
if (c1xr1x-r1 || c1xr1x+r1) {
dx1 = -dx1;
}
cx =cx+dx;
cy =(int)(dx*Math.sqrt(r*r-(cx-rx)*(cx-rx)))+ry;
c1x =c1x+dx1;
c1y =(int)(dx1/2*Math.sqrt(r1*r1-(c1x-r1x)*(c1x-r1x))/2);
// g.drawArc(cx, cy, cw, ch, 0, 360);
for(int i=0;i8;i++){
if (i%5 == 0){
g.setColor(Color.black);
}else if ( i%5== 1) {
g.setColor(Color.GREEN);
}else if(i%5==2){
g.setColor(Color.RED);
}else if( i%5 ==3){
g.setColor(Color.pink);
}else {
g.setColor(Color.orange);
}
g.drawLine(bx,by, cx+10,cy+i+10);
g.drawLine(bx+bw,by+bh,cx+10,cy+i+10);
g.drawLine(bx+bw,by, cx+10,cy+i+10);
g.drawLine(bx,by+bh, cx+10,cy+i+10);
g.drawLine(bx,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by+bh,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx,by+bh,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx,by+bh, c1x+10,c1y+r1y+i+10);
g.drawLine(bx,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawArc(cx+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(-cx+bw-bx-cw+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(c1x+i, c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(r1x+r1+cw-c1x+i, -c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
}
}
public void start(){
if (u_thread == null)
{
u_thread = new Thread(this);
u_thread.start();
}
}
public void run() {
while(true){
repaint();
try{
u_thread.sleep(10);
}
catch (InterruptedException e){
return;
}
}
}
}
;
要制造那种效果只需要大约 30 行 Java 代码:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class RollingBall extends JPanel {
Ellipse2D.Float ball = new Ellipse2D.Float( -100, 100, 50, 50 );
public void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;
// Draw the ball
g2.fill( ball );
// Draw the rotating ellipse by skewing the Device Space
double angdeg = // One rotation per ball's travelling over its perimeter
ball.x++ % ( Math.PI * ball.width ) / ( Math.PI * ball.width ) * 360;
g2.rotate( Math.toRadians( angdeg ), ball.getCenterX( ), ball.getCenterY( ) );
g2.scale( .5, 1 );
g2.translate( ball.getCenterX( ), 0 );
g2.setColor( Color.gray );
g2.fill( ball );
}
public void roll( ) throws Exception {
while( true ) {
repaint( );
Thread.sleep( 8 );
}
}
public static void main( String[ ] args ) throws Exception {
JFrame f = new JFrame( );
RollingBall rb = new RollingBall( );
f.setSize( 999, 185 );
f.getContentPane( ).add( rb );
f.setVisible( true );
rb.roll( );
}
}