资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

运动的小球java代码 java如何实现运动的小球

滚动的小球 java源代码

;

创新互联建站长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为尤溪企业提供专业的成都做网站、成都网站建设,尤溪网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

要制造那种效果只需要大约 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多线程移动的小球

同一个Thread不能start两次

这样改下

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

@SuppressWarnings("serial")

public class MoveBall02 extends JFrame {

private JButton jbtu = null;

private Ball02 ball = null;

Thread t = null;

public static void main(String[] args) {

new Test();

}

public Test() {

this.setTitle("移动的小球");

this.setSize(600, 500);

this.setLocation(100, 100);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

ball = new Ball02(40, 40);

this.add(ball);

t = new Thread(ball);

jbtu = new JButton("点击移动小球");

this.add(jbtu, BorderLayout.SOUTH);

jbtu.addActionListener(new MyActionListener());

this.setVisible(true);

}

class MyActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == jbtu) {

// 响应鼠标点击事件

if (!t.isAlive()) {

t = new Thread(ball);

t.start();

}

}

}

}

}

@SuppressWarnings("serial")

class Ball02 extends JPanel implements Runnable {

private int x, y;

public Ball02(int x, int y) {

this.x = x;

this.y = y;

}

@Override

public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.RED);

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

}

public void run() {

try {

for (int i = 0; i  20; i++) {

x = y += 3;

this.repaint();

Thread.sleep(100);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

怎么用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代码 java如何实现运动的小球
转载来于:http://cdkjz.cn/article/doposhi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220