新手求教java 多线程小球移动this.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
if(arg0.getKeyChar()==KeyEvent.VK_ENTER){
//干活
}
}
});
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 {
【移动的小球java代码 js小球移动】 private JButton jbtu = null;
private Ball02 ball = null;
Thread t = null;
public static void main(String[] args) {
new Test();
}
public Test() {
this.setTitle("移动移动的小球java代码的小球");
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; i20; i++) {
x = y += 3;
this.repaint();
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
滚动的小球 java源代码;
要制造那种效果只需要大约 30 行 Java 代码移动的小球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 ME中写的随机移动的小球,怎么改成让它从上到下移动?this.posX=(random.nextInt()1)%(this.getWidth()-20)+10;
this.posY=(random.nextInt()1)%(this.getHeight()-20)+10;
可以改成
this.posY=(posY+10)%(this.getHeight()-20)+10;
推荐阅读
- oracleblob导出jpg,oracle clob数据导出
- 视频号注销后多久可以再开通的简单介绍
- 如何制作课程网站模板,网页制作课程表教程
- 游戏达人益智,游戏达人是什么意思
- linux命令手动挂载 linux挂载方式
- 微信小程序怎么显示空格,微信小程序怎么做店铺
- 手机照片变成电子版怎么做,手机把照片变成电子版
- app制作网站有哪些,app制作平台哪个最好
- mysql读写分离怎么写 如何实现mysql读写分离