我正在尝试学习Java上的一些动画,我想创建一个显示字符串并对该字符串进行动画处理的框架,以使其在箭头键控制的情况下在屏幕上移动并确保其停留在屏幕的边界内。
我已经用鼠标做了同样的事情,但是我不能用箭头键来做
package Draw;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DrawMouse extends JPanel implements MouseListener, MouseMotionListener, KeyListener
{
int x, y;
String s = "Drag this around";
boolean click, drag, mouseInside, insideR1, insideR2, insideR3;
//create 3 rectangle objects
Rectangle r1 = new Rectangle (20, 70, 80, 100);
Rectangle r2 = new Rectangle (110, 70, 80, 100);
Rectangle r3 = new Rectangle (200, 70, 80, 100);
// create the fonts once and use them when needed
Font bigFont = new Font ("Helvetica", Font.BOLD, 45);
Font size12 = new Font ("Helvetica", Font.BOLD, 12);
Font size14 = new Font ("Helvetica", Font.PLAIN, 14);
public DrawMouse ()
{ // add the listener to the panel
addMouseListener (this);
addMouseMotionListener (this);
this.addKeyListener (this);
setFocusable (true); // need this to set the focus of the mouse
}
//generates only one event for each pressed-released combination
public void mouseClicked (MouseEvent e)
{
// use this for click and release
}
public void mouseEntered (MouseEvent e)
{
mouseInside = true;
repaint ();
}
public void mouseExited (MouseEvent e)
{
mouseInside = false;
click = false;
repaint ();
}
public void mousePressed (MouseEvent e)
{
click = true;
drag = false;
repaint ();
}
public void mouseReleased (MouseEvent e)
{
click = false;
drag = false;
repaint ();
}
public void mouseDragged (MouseEvent e)
{
drag = true;
x = e.getX ();
y = e.getY ();
repaint ();
}
public void mouseMoved (MouseEvent e)
{
x = e.getX ();
y = e.getY ();
//"contains" is a method of the Rectangle class
if (r1.contains (x, y))
insideR1 = true;
else
insideR1 = false;
if (r2.contains (x, y))
insideR2 = true;
else
insideR2 = false;
if (r3.contains (x, y))
insideR3 = true;
else
insideR3 = false;
repaint ();
}
public void keyTyped (KeyEvent e)
{
s = s + e.getKeyChar (); // append the character to the string
}
public void keyReleased (KeyEvent e)
{
repaint ();
}
public void keyPressed (KeyEvent e)
{
char key = (char) e.getKeyCode ();
if (key == e.VK_ESCAPE)
System.exit (0);
else if (key == e.VK_ENTER) // ENTER clears the sentence
s = "";
}
public void drawStuff (Graphics g)
{
//if mouse was clicked, background magenta, otherwise black
if (click)
{
g.setColor (Color.magenta);
}
else
{
g.setColor (Color.black);
}
g.fillRect (0, 0, size ().width, size ().height);
//paint the 3 rectangles
g.setColor (Color.blue);
g.fillRect (r1.x, r1.y, r1.width, r1.height);
g.setColor (Color.green);
g.fillRect (r2.x, r2.y, r2.width, r2.height);
g.setColor (Color.red);
g.fillRect (r3.x, r3.y, r3.width, r3.height);
g.setFont (size12);
g.setColor (Color.white);
if (drag)
g.drawString (s, x, y);
else
g.drawString (s, 20, 200);
//draw the mouse coordinates
if (mouseInside)
{
g.setColor (Color.white);
g.drawString ("X: " + x, 30, 30);
g.drawString ("Y: " + y, 30, 50);
}
else
{
g.setFont (size14);
g.setColor (Color.orange);
g.drawString ("Move the mouse inside the window!", 40, 30);
}
if (insideR1)
{
g.setFont (bigFont);
g.setColor (Color.blue);
g.drawString ("BLUE", 120, 50);
g.setColor (Color.white);
g.drawRect (r1.x, r1.y, r1.width - 1, r1.height - 1);
}
if (insideR2)
{
g.setFont (bigFont);
g.setColor (Color.green);
g.drawString ("GREEN", 120, 50);
g.setColor (Color.white);
g.drawRect (r2.x, r2.y, r2.width - 1, r2.height - 1);
}
if (insideR3)
{
g.setFont (bigFont);
g.setColor (Color.red);
g.drawString ("RED", 120, 50);
g.setColor (Color.white);
g.drawRect (r3.x, r3.y, r3.width - 1, r3.height - 1);
}
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
drawStuff (g);
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("DrawMouse");
frame.getContentPane ().add (new DrawMouse ());
frame.setSize (300, 300);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
我期望具有相同的输出,但是使用箭头键而不是使用鼠标移动字符串