我试图在屏幕上同时移动两个球体。但是,它们并没有相互独立。相反,一个人依赖于另一个人的运动。请帮帮我。这是代码: -
Draw
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Draw extends JPanel implements ActionListener
{
Timer timer[] = new Timer[2];
int velX = 2, velY = 2;
Sphere sphere[] = new Sphere[2];
public Draw()
{
for(int i=0;i<2;i++)
{
sphere[i] = new Sphere();
timer[i] = new Timer(5,this);
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<2;i++)
drawing(g,sphere[i].color,sphere[i].radius);
}
public void drawing(Graphics g,Color color, int radius)
{
for(int i=0;i<2;i++){
g.setColor(color);
g.fillOval(sphere[i].x,sphere[i].y,radius*2,radius*2);
timer[i].start();
}
}
public void actionPerformed(ActionEvent evt)
{
for(int i=0;i<2;i++){
if(sphere[i].x<0 || sphere[i].x>970)
{
velX = -velX;
}
if(sphere[i].y<0 || sphere[i].y>650)
{
velY = -velY;
}
sphere[i].x += velX;
sphere[i].y += velY;
repaint();
}
}
public static void main(String args[])
{
JFrame jf = new JFrame("Renderer");
jf.getContentPane().add(new Draw(),BorderLayout.CENTER);
jf.setBounds(0,0,1024,720);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Sphere
import java.awt.Color;
class Sphere
{
public int x;
public int y;
public int radius;
Color color;
public Sphere()
{
this.x = (int)(Math.random()*800);
this.y = (int)(Math.random()*600);
this.radius = (int)(Math.random()*50);
this.color = new Color((int)(Math.random()*255),
(int)(Math.random()*255),(int)(Math.random()*255));
}
public Sphere(int x,int y,int radius, Color color)
{
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
}
答案 0 :(得分:3)
两个对象的x / y delta相同,您需要在Sphere
对象中包含这些值,例如
class Sphere {
int velX;
int velY;
public int x;
public int y;
public int radius;
Color color;
public Sphere(int xDelta, int yDelta) {
velX = xDelta;
velY = yDelta;
this.x = (int) (Math.random() * 800);
this.y = (int) (Math.random() * 600);
this.radius = (int) (Math.random() * 50);
this.color = new Color((int) (Math.random() * 255),
(int) (Math.random() * 255), (int) (Math.random() * 255));
}
void move(Dimension size) {
x += velX;
y += velY;
if (x < 0) {
x = 0;
velX *= -1;
} else if (x + (radius * 2) > size.width) {
x = size.width - (radius * 2);
velX *= -1;
}
if (y < 0) {
y = 0;
velY *= -1;
} else if (y + (radius * 2) > size.height) {
y = size.height - (radius * 2);
velY *= -1;
}
}
public Sphere(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
}
然后,在您的actionPerformed
方法中,您只需调用update ...
@Override
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < 2; i++) {
sphere[i].move(getSize());
}
repaint();
}
此外,你不需要两个Timer
,你只需要一个,每次勾选时都可以用来更新这两个球......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
class Draw extends JPanel implements ActionListener {
Timer timer;
Sphere sphere[] = new Sphere[2];
public Draw() {
Random rnd = new Random();
for (int i = 0; i < 2; i++) {
sphere[i] = new Sphere(rnd.nextInt(4) + 1, rnd.nextInt(4) + 1);
}
timer = new Timer(16, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 2; i++) {
drawing(g, sphere[i].color, sphere[i].radius);
}
}
public void drawing(Graphics g, Color color, int radius) {
for (int i = 0; i < 2; i++) {
g.setColor(color);
g.fillOval(sphere[i].x, sphere[i].y, radius * 2, radius * 2);
}
}
@Override
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < 2; i++) {
sphere[i].move(getSize());
}
repaint();
}
public static void main(String args[]) {
JFrame jf = new JFrame("Renderer");
jf.getContentPane().add(new Draw(), BorderLayout.CENTER);
jf.setBounds(0, 0, 1024, 720);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
class Sphere {
int velX;
int velY;
public int x;
public int y;
public int radius;
Color color;
public Sphere(int xDelta, int yDelta) {
velX = xDelta;
velY = yDelta;
this.x = (int) (Math.random() * 800);
this.y = (int) (Math.random() * 600);
this.radius = (int) (Math.random() * 50);
this.color = new Color((int) (Math.random() * 255),
(int) (Math.random() * 255), (int) (Math.random() * 255));
}
void move(Dimension size) {
x += velX;
y += velY;
if (x < 0) {
x = 0;
velX *= -1;
} else if (x + (radius * 2) > size.width) {
x = size.width - (radius * 2);
velX *= -1;
}
if (y < 0) {
y = 0;
velY *= -1;
} else if (y + (radius * 2) > size.height) {
y = size.height - (radius * 2);
velY *= -1;
}
}
public Sphere(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
}
}
另外,你不应该在“paint”传递中启动计时器,它们应该在手前启动
答案 1 :(得分:2)
两个球体的速度相同,所以它们的动作会相似。
int velX = 2, velY = 2;
如果在球体创建中为每个球员设置了随机速度,则移动将会有所不同。
答案 2 :(得分:2)
将velX
和velY
移至Sphere
课程。因此每个Sphere
将保持自己的速度。然后球体将彼此独立移动。
不需要两个Timer
。我把它清理干净了。
我还清理了paintComponent
方法。不需要在两个地方循环。
这是我尝试过的,它似乎对我有用:
public class Draw extends JPanel implements ActionListener {
Sphere sphere[] = new Sphere[2];
public Draw() {
for (int i = 0; i < 2; i++) {
sphere[i] = new Sphere();
}
Timer timer = new Timer(5, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawing(g);
}
public void drawing(Graphics g) {
for (int i = 0; i < 2; i++) {
g.setColor(sphere[i].color);
g.fillOval(sphere[i].x, sphere[i].y, sphere[i].radius * 2, sphere[i].radius * 2);
}
}
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < 2; i++) {
if (sphere[i].x < 0 || sphere[i].x > 970) {
sphere[i].velX = -sphere[i].velX;
}
if (sphere[i].y < 0 || sphere[i].y > 650) {
sphere[i].velY = -sphere[i].velY;
}
sphere[i].x += sphere[i].velX;
sphere[i].y += sphere[i].velY;
repaint();
}
}
public static void main(String args[]) {
JFrame jf = new JFrame("Renderer");
jf.getContentPane().add(new Draw(), BorderLayout.CENTER);
jf.setBounds(0, 0, 1024, 720);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class Sphere {
public int x;
public int y;
public int radius;
Color color;
int velX;
int velY;
public Sphere() {
this.x = (int) (Math.random() * 800);
this.y = (int) (Math.random() * 600);
this.radius = (int) (Math.random() * 50);
this.color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
velX = new Random().nextInt()%5 + 2;
velY = new Random().nextInt()%5 + 2;
}
public Sphere(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
}
希望这有帮助!