gpt4 book ai didi

java - 让两个物体在 SwingBot 中移动

转载 作者:行者123 更新时间:2023-12-03 21:27:41 24 4
gpt4 key购买 nike

我试图让它在按下命令时两个形状都移动。我的问题是:如何让蓝色多边形与黄色矩形一起移动?我似乎无法弄清楚,无论我做什么。任何帮助表示赞赏!谢谢!

编辑:删除了定时器代码(它是为了不同的东西)

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Original {

public static void main(String[] args) {
// contruction of new JFrame object
JFrame frame = new JFrame();
// mutators
frame.setSize(400,400);
frame.setTitle("SwingBot");
// program ends when window closes
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Robot r = new Robot();
frame.add(r);
// voila!
frame.setVisible(true);
// your Scanner-based command loop goes here
int noend = 0;
System.out.println("Type a Command:");
while(noend == 0)
{
Scanner input = new Scanner(System.in);
String command = input.next();
if(command.equals("left"))
r.moveBot(-10,0);

if(command.equals("right"))
r.moveBot(10,0);

if(command.equals("down"))
r.moveBot(0,10);

if(command.equals("up"))
r.moveBot(0,-10);
}
// call methods on the Robot instance like w.moveBot(10,10) in response to
// user input
}

public static class Robot extends JComponent
{
private Rectangle rect = new Rectangle(20,60);
private Polygon poly = new Polygon();

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// set the color
g2.setColor(Color.ORANGE);
// draw the shape`
g2.fill(rect);
int xPoly[] = {75, 125, 170, 170, 200, 105, 60};
int yPoly[] = {75, 50, 88, 111, 125, 180, 150};
poly = new Polygon(xPoly, yPoly, xPoly.length);
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
}

public void moveBot(int x, int y)
{
// move the rectangle
rect.translate(x,y);
poly.translate(x,y);
// redraw the window
repaint();
}
}
}

最佳答案

建议:

  • 不要在 paintComponent 方法中创建形状。
  • 在创建它们时给它们提供可以改变的变量
  • Timer 或按键中,或者您尝试移动它们的任何地方,更改这些变量并调用 repaint()
  • 对于多个对象,您可以有一个接口(interface),例如Shape,其中每个Shape 都有一个move()可以调用的方法。
  • 遍历这些对象的数据结构并调用它们的 move() 方法,然后 repaint()`
  • 一个Shape可以有一个drawShape(Graphics g)方法,你可以在中循环遍历Shape的数据结构>paintComponent 方法并调用drawShape(g)

可以看到this answer举个例子。

enter image description here


更新

这是我上面提到的所有要点的示例。

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class MoveShape extends JPanel {

List<Shape> shapes;

public MoveShape() {
shapes = createShapeList();

InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("RIGHT"), "moveRight");
ActionMap am = getActionMap();
am.put("moveRight", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
for (Shape sh : shapes) {
sh.moveRight();
repaint();
}
}
});
}

private List<Shape> createShapeList() {
List<Shape> list = new ArrayList<>();
int xPoly[] = {75, 125, 170, 170, 200, 105, 60};
int yPoly[] = {75, 50, 88, 111, 125, 180, 150};
list.add(new MyPolygon(xPoly, yPoly, 6));
list.add(new MyRectangle(75, 250, 150, 150));
return list;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape sh : shapes) {
sh.drawShape(g);
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(450, 450);
}

public class MyRectangle implements Shape {

int x, y, width, height;

public MyRectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public void drawShape(Graphics g) {
g.fillRect(x, y, width, height);
}

@Override
public void moveRight() {
x += INCREMENT;
}

}

public class MyPolygon implements Shape {

int[] xPoints;
int[] yPoints;
int numPoints;

public MyPolygon(int[] xPoints, int[] yPoints, int numPoints) {
this.xPoints = xPoints;
this.yPoints = yPoints;
this.numPoints = numPoints;
}

@Override
public void drawShape(Graphics g) {
g.fillPolygon(xPoints, yPoints, numPoints);
}

@Override
public void moveRight() {
for (int i = 0; i < xPoints.length; i++) {

xPoints[i] += INCREMENT;

}
}
}

public interface Shape {

public static final int INCREMENT = 5;

public void drawShape(Graphics g);

public void moveRight();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Move Shapes");
frame.add(new MoveShape());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

关于java - 让两个物体在 SwingBot 中移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059165/

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