小球滚动的java代码 java显示多个跳动的小球

滚动的小球 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中用多线程实现小球滚动程序拜托各位了 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程序:编写一个程序,让一个小球在JFrame中滚动,当碰边缘时则选择一个角度返回.

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;

}

}

}

}

怎么用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 编程题

第三题的答案

package image;

import java.awt.EventQueue;

public class Test {

private JFrame frame;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Test window = new Test();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public Test() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 466);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();

panel.setBackground(Color.YELLOW);

panel.setForeground(Color.BLACK);

panel.setBounds(10, 10, 414, 138);

frame.getContentPane().add(panel);

JButton btnNewButton_1 = new JButton("New button");

panel.add(btnNewButton_1);

JButton btnNewButton = new JButton("New button");

panel.add(btnNewButton);

JPanel panel_1 = new JPanel();

panel_1.setBackground(Color.GREEN);

panel_1.setBounds(10, 158, 414, 259);

frame.getContentPane().add(panel_1);

JButton btnNewButton_3 = new JButton("New button");

panel_1.add(btnNewButton_3);

JButton btnNewButton_2 = new JButton("New button");

panel_1.add(btnNewButton_2);

}

}

如何用Java绘图做出让小球在背景上运动的效果

java是编程语言里比较难学的一门,如果有心从事编程方向的工作,最好到专业机构学习并有更多的项目实践,更贴近市场,这样更有利于将来的发展。


分享题目:小球滚动的java代码 java显示多个跳动的小球
浏览地址:http://azwzsj.com/article/docdijd.html