我是一个真正的初学者,所以原谅我对此的愚蠢。我正在尝试创建一个程序,它将在连续方向上移动图形,直到它基于输入验证器的单键按下而超出帧。这就是我到目前为止所做的:
使用main方法的框架构建类:
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)
{
}
}
可能以错误的方式解决这个问题,我之前已经不同时间问过这个问题,但我还没有以正确的方式提出问题。原谅我的无知,任何帮助都表示赞赏。
答案 0 :(得分:1)
首先,您可能需要先了解如何在按下某个键时移动组件。请查看Motion Using the Keyboard中的MotionWithKeyBindings
示例代码,了解如何使用key Bindings
执行此操作。
了解了这一点后,您需要修改Action
,因为您现在需要使用Swing Timer
来安排连续事件。因此,不是让Action
直接更改组件的位置,而是启动Timer
。然后当Timer
触发时,您将更改组件的位置。
查看How to Use Swing Timers上Swing教程中的部分,了解更多信息和示例。
您还可以查看:Update a Label with a Swing Timer以获取Swing Timer的简单示例。