gpt4 book ai didi

java - 如何使用 JFrame/JComponent 通过一次按键移动由多个对象组成的图形

转载 作者:行者123 更新时间:2023-12-01 09:17:15 25 4
gpt4 key购买 nike

我是一个真正的初学者,所以请原谅我在这方面的愚蠢。我正在尝试创建一个程序,该程序将根据输入 validator 的单个按键沿连续方向移动图形,直到它超出框架。这是我到目前为止所拥有的:

具有主要方法的框架构建类:

import javax.swing.JFrame;
import javax.swing.Timer;

public class EmptyFrameViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("Alien Faces");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

FaceComponent face1 = new FaceComponent();
frame.add(face1);
frame.setVisible(true);

FaceComponentTwo face2 = new FaceComponentTwo();
frame.add(face2);
frame.setVisible(true);

}

这是绘画课:

}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;

/*
* This component will draw an "Alien" face
*/

public class FaceComponent extends JComponent
{

public void paintComponent(Graphics g)
{
//Recover Graphics2D
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

//Construct the alien face

//Draw the head
Ellipse2D.Double head = new Ellipse2D.Double (5, 80, 100, 150);
g2.draw(head);

//Draw the set of eyes
g2.setColor(Color.GREEN);
Rectangle eye = new Rectangle(25, 140, 15, 15);
g2.fill(eye);
eye.translate(50, 0);
g2.fill(eye);

//Draw the mouth
Line2D.Double mouth = new Line2D.Double(30, 180, 80, 180);
g2.setColor(Color.RED);
g2.draw(mouth);

//Draw the greeting
g2.setColor(Color.BLUE);
g2.drawString("Hello, World!", 20, 245);
}


}

这是 Movement 类的框架,但我觉得需要进行大量修改:

import javax.swing.*;


/*
* This class will invoke the methods that move the
* objects in different directions.
*/

public class Movement extends FaceComponent
{
//List of fields that are used within this class.
int x = 0, y = 0, velX = 7, velY = 7;


//Constructor, that creates two brand new instances of the alien faces.
public Movement()
{
FaceComponent face1 = new FaceComponent();
FaceComponentTwo face2 = new FaceComponentTwo();
repaint();
}

//List of methods that will be invoking different movements which are based on the user's inputs.


//Method that will move the first face(left) to the right until it is out of frame.
public void moveRight(int x, int y)
{

}


//Method that will move the second face(right) to the left until it is out of frame.
public void moveLeft(int x, int y)
{
while(x > 0 || x <= 550)
{
x += velX;
repaint();
}
}


//Method that will move the first face(left) up until it is out of frame.
public void moveUp(int x, int y)
{

}


//Method that will move the second face(right) until it is out of frame.
public void moveDown(int x, int y)
{

}


//Method that will move the first face(left) in a downwards-right angle until it is out of frame.
public void moveDownRight(int x, int y)
{

}


//Method that will move the second face(right) in a downwards-left angle until it is out of frame.
public void moveDownLeft(int x, int y)
{

}


}

可能是以错误的方式来解决这个问题的,而且我之前曾多次问过这个问题,但我没有以正确的方式提出问题。原谅我的无知,感谢任何帮助。

最佳答案

首先,您可能想要学习如何在按下按键时移动组件。查看 Motion Using the Keyboard 中的 MotionWithKeyBindings 示例代码有关如何使用键绑定(bind)执行此操作的示例。

一旦理解了这一点,您将需要修改Action,因为您现在需要使用Swing Timer来安排连续事件。因此,您无需使用 Action 直接更改组件的位置,而是启动 Timer。然后,当Timer触发时,您将更改组件的位置。

查看 Swing 教程中 How to Use Swing Timers 的部分了解更多信息和示例。

您还可以查看:Update a Label with a Swing Timer有关 Swing 计时器的简单示例。

关于java - 如何使用 JFrame/JComponent 通过一次按键移动由多个对象组成的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453251/

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