「java球摇晃」java实现球的移动

博主:adminadmin 2023-01-23 08:18:09 294

今天给各位分享java球摇晃的知识,其中也会对java实现球的移动进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

怎么用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用repaint方法在窗格内实现小球的来回碰撞,怎么在窗格边缘改变小球的运动方向呢?

public class DrawBall extends JFrame {

int x, y, width, height;

Color c;

int incX = 10;//X方向增量

int incY = 10;//Y方向增量

public DrawBall() {

super("寂寞高手不寂寞");

setSize(800, 600);

setVisible(true);

x = 0;

y = 0;

width = height = 50;

c = new Color(255, 0, 0);

}

public static void main(String[] args) {

DrawBall a = new DrawBall();

}

public void paint(Graphics g) {

Container pane = getContentPane();

Graphics pg = pane.getGraphics();

pg.setColor(Color.WHITE);

pg.fillRect(0, 0, pane.getWidth(), pane.getHeight());

//从这里开始改变小球的运动方向

if (x+width pane.getWidth() || x 0) {//X边界判断

incX *= -1; //增量方向反转

}

if (y+height pane.getHeight() || y 0) {//Y边界判断

incY *= -1;//增量方向反转

}

x = x + incX;

y = y + incY;

pg.setColor(c);

pg.fillOval(x, y, width, height);

try {

Thread.sleep(100);

} catch (InterruptedException E) {

};

repaint();

}

}

这个JAVA程序的小球怎么不会动

我给你改为了  碰到边界后会朝相反的方向移动  这里必须要用线程

通过repaint〉〉〉paint

public class Pratice

{

public Pratice()

{

JFrame f = new JFrame("my app");

MyPanel mp = new MyPanel();

f.setLocation(300, 300);

f.setSize(320, 340);

f.add(mp);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Thread thread = new Thread(() -

{

while (true)

{

try

{

mp.repaint();

Thread.sleep(100);

} catch (InterruptedException e)

{

e.printStackTrace();

}

}

});

thread.start();

}

public static void main(String[] args)

{

new Pratice();

}

}

@SuppressWarnings("serial")

class MyPanel extends Panel

{

private int x, y, r, vx, vy;

int bounce = -1;

public MyPanel()

{

x = 20;

y = 20;

r = 100;

vx = 20;

vy = 20;

}

public void paint(Graphics g)

{

g.setColor(Color.BLUE);

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

x += vx;

y += vy;

if (x = 0)

{

x = 0;

vx *= bounce;

} else if (x + r  300)

{

x = 300 - r;

vx *= bounce;

}

if (y = 0)

{

y = 0;

vy *= bounce;

} else if (y + r = 300)

{

y = 300 - r;

vy *= bounce;

}

}

}

用JAVA实现多线程编写,使得许多小球在界面内循环跳动

下面这段代码应该符合你的需求

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class BounceThread

{ public static void main(String[] args)

{ JFrame frame = new BounceThreadFrame();

frame.show();

}

}

class BounceThreadFrame extends JFrame

{ public BounceThreadFrame()

{ setSize(300, 200);

setTitle("Bounce");

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ System.exit(0);

}

} );

Container contentPane = getContentPane();

canvas = new JPanel();

contentPane.add(canvas, "Center");

JPanel p = new JPanel();

addButton(p, "Start",

new ActionListener()

{ public void actionPerformed(ActionEvent evt)

{ Ball b = new Ball(canvas);

b.start();

}

});

addButton(p, "Close",

new ActionListener()

{ public void actionPerformed(ActionEvent evt)

{ canvas.setVisible(false);

System.exit(0);

}

});

contentPane.add(p, "South");

}

public void addButton(Container c, String title,

ActionListener a)

{ JButton b = new JButton(title);

c.add(b);

b.addActionListener(a);

}

private JPanel canvas;

}

class Ball extends Thread

{ public Ball(JPanel b) { box = b; }

public void draw()

{ Graphics g = box.getGraphics();

g.fillOval(x, y, XSIZE, YSIZE);

g.dispose();

}

public void move()

{ if (!box.isVisible()) return;

Graphics g = box.getGraphics();

g.setXORMode(box.getBackground());

g.fillOval(x, y, XSIZE, YSIZE);

x += dx;

y += dy;

Dimension d = box.getSize();

if (x 0)

{ x = 0; dx = -dx; }

if (x + XSIZE = d.width)

{ x = d.width - XSIZE; dx = -dx; }

if (y 0)

{ y = 0; dy = -dy; }

if (y + YSIZE = d.height)

{ y = d.height - YSIZE; dy = -dy; }

g.fillOval(x, y, XSIZE, YSIZE);

g.dispose();

}

public void run()

{ try

{ draw();

for (int i = 1; i = 1000; i++)

{ move();

sleep(5);

}

}

catch(InterruptedException e) {}

}

private JPanel box;

private static final int XSIZE = 10;

private static final int YSIZE = 10;

private int x = 0;

private int y = 0;

private int dx = 2;

private int dy = 2;

}

java球摇晃的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现球的移动、java球摇晃的信息别忘了在本站进行查找喔。