gpt4 book ai didi

java - 在屏幕上移动对象

转载 作者:行者123 更新时间:2023-12-01 07:19:55 25 4
gpt4 key购买 nike

我正在尝试在屏幕上同时移动两个球体。然而,它们并不是彼此独立地移动。相反,一个人依赖于另一个人的运动。请帮帮我。这是代码:-

绘制

    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);
}
}

球体

    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;
}
}

最佳答案

两个对象的 x/y 增量相同,例如,您需要将这些值包含在 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;
}

}
}

此外,您不应该在“绘制”过程中启动计时器,它们应该提前启动

关于java - 在屏幕上移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42550220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com