我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java小球滚动代码 java小球运动

java程序:编写一个程序,让一个小球在JFrame中滚动,当碰边缘时则选择一个角度返回.

05年写的,你修改一下吧

成都创新互联公司专业提供德阳机房托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买德阳机房托管服务,并享受7*24小时金牌售后服务。

/*

* 一个在窗体中来回运动的圆.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;

}

}

}

}

怎么用java模拟小球的圆周运动?

//简单的做个

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();

}

}

}

}

求一个Java动画,一个小球在屏幕上碰到边缘就反弹无限循环

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Panel;

public class Demo {

public static void main(String[] args) {

Frame w = new Frame();

w.setSize(300, 400);

CPanel mp = new CPanel();

w.add(mp);

w.setVisible(true);

Thread t = new Thread(mp);

t.start();

}

}

class CPanel extends Panel implements Runnable {

private static final long serialVersionUID = 3474337559197220434L;

int x = 30;

int y = 30;

boolean down = true;

public void paint(Graphics g) {

g.fillOval(x, y, 20, 20);

}

public void run() {

try {

while (true) {

if (down) {

y++;

if (y = 340) {

down = false;

}

} else {

y--;

if (y = 20) {

down = true;

}

}

repaint();

Thread.sleep(1);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

JAVA中用多线程实现小球滚动程序拜托各位了 3Q

我这们是嵌入在网页中的,你运行试试 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ball extends JApplet implements Runnable { private Thread blueBall; private boolean xUp,yUp,bouncing; private int x,y,xDx,yDy; private final int MAX_X = 200, MAX_Y = 200; public void init() { xUp = false; yUp = false; xDx = 1; yDy =1; bouncing = false; addMouseListener( new MouseListener(){ public void mousePressed(MouseEvent event){ creatBall(event); } //鼠标按下 public void mouseExited(MouseEvent event){} public void mouseClicked(MouseEvent event){} public void mouseReleased(MouseEvent event){} public void mouseEntered(MouseEvent event){} } ); setSize(MAX_X,MAX_Y); } private void creatBall(MouseEvent event){ if(blueBall == null){ x = event.getX(); y = event.getY();//获取按下的位置 blueBall = new Thread(this); bouncing = true; blueBall.start(); } } public void stop() { blueBall = null; } public void paint(Graphics g) { super.paint(g); if(bouncing){ g.setColor(Color.red); g.fillOval(x,y,20,20); } } public void run() { while(true) { try{ blueBall.sleep(20); }catch(InterruptedException exception){ System.err.println(exception.toString()); } if(xUp == true) x += xDx; else x -= xDx; if(yUp == true) y += yDy; else y -= yDy; if(y = 0){ yUp = true; yDy = (int) (Math.random() * 5 + 2); } else if(y = MAX_Y - 10){ yDy = (int) (Math.random() * 5 + 2); yUp = false; } if(x = 0){ xUp = true; xDx = (int) (Math.random() * 5 + 2); } else if(x = MAX_X - 10){ xDx = (int) (Math.random() * 5 + 2); xUp = false; } repaint(); } } }

采纳哦

滚动的小球 java源代码

;

要制造那种效果只需要大约 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( );

}

}


文章名称:java小球滚动代码 java小球运动
链接分享:http://mswzjz.cn/article/hjhcee.html

其他资讯