![](/img/trans.png)
[英]How do I add a keystroke inside of keyPressed(KeyEvent e) method to change color of ball? simple KeyListener demo
[英]How do I add keystroke Z to keyPressed(KeyEvent e) to jump/repaint the ball to a new random location? (KeyListener demo used.)
我的Java經驗有限,特別是當它與圖形有關時。 到目前為止,我對這個問題有很大的幫助。 它的入門代碼(在底部)在我在Eclipse中測試時工作得很好。 我在課堂上用它來教一個高中生。 按照下面評論中的說明,有人知道一個簡單的方法來擴展已經很簡單的程序,使球在按下Z按鈕后跳轉/移動/重新繪制到一個隨機的新位置? 我正在考慮將以下代碼添加到keyPressed(KeyEvent e)方法:
else if(keyCode == KeyEvent.VK_Z)
{
//Not sure what to add here. Just want to change X and Y coordinates to
//anything, so long as the program works and stays fairly simple
//for now.
//----> g.fillOval(x + radius, y + radius, 2 * radius, 2 * radius);
// this line above says "g can not be resolved".
}
。
任何提示程序簡單的提示或想法都將非常感激。 謝謝!
。
import java.awt.*;
import java.awt.event.*; // #1
import javax.swing.*;
/******************************************************************************
*
* KeyListenerDemo.java
* Demonstrates getting keyboard input using the KeyListener interface.
*
* Program 18: Extend this program by adding a few more keystroke commands:
* z (VK_Z) - Cause the ball to jump to a random new location.
* s (VK_S) - Make the ball smaller - multiply its diameter 1/2.
* b (VK_B) - Make the ball bigger - multiply its diameter by 2.
* c (VK_C) - Change the color (in any way you'd like).
*
* In addition, modify the program to ensure the following:
* - The ball goes all the way to the edge of the screen but stays
* completely on the screen.
* - If a doubled diameter doesn't fit, make it as large as possible.
* - Be sure the ball never completely disappears.
*
*****************************************************************************/
public class KeyListenerDemo extends JFrame
implements KeyListener // #2
{
// Class Scope Finals
private static final int SCREEN_WIDTH = 1000;
private static final int SCREEN_HEIGHT = 800;
private static final int START_RADIUS = 25;
private static final int START_X = 100;
private static final int START_Y = 100;
private static final int STEP_SIZE = 10;
// Class Scope Variables
private static int x = START_X; // x at center of the ball
private static int y = START_Y; // y at center of the ball
private static int radius = START_RADIUS; // radius of the ball
// Methods
/**
* Create the window and register this as a KeyListener
*
* @param args
*/
public static void main (String[] args)
{
// Set up the JFrame window.
KeyListenerDemo gp = new KeyListenerDemo();
gp.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
gp.setVisible(true);
gp.addKeyListener(gp); // #3
// If this class had a constructor and you moved this line into
// that constructor it could not refer to gp since that variable
// is local to this method. Instead you would write::
// addKeyListener(this);
}
/**
* Called when a key is first pressed
* Required for any KeyListener
*
* @param e Contains info about the key pressed
*/
public void keyPressed(KeyEvent e) // #4A
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
x = x - STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
x = x + STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_UP)
{
y = y - STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
y = y + STEP_SIZE;
}
repaint();
}
/**
* Called when typing of a key is completed
* Required for any KeyListener
*
* @param e Contains info about the key typed
*/
public void keyTyped(KeyEvent e) // #4B
{
}
/**
* Called when a key is released
* Required for any KeyListener
*
* @param e Contains info about the key released
*/
public void keyReleased(KeyEvent e) // #4C
{
}
/**
* paint - draw the figure
*
* @param g Graphics object to draw in
*/
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(Color.blue);
g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
嘗試做這樣的事情:
public void keyPressed(KeyEvent e){
if(e.getKeyCode == KeyEvent.VK_Z){
moveRand();
//Assuming that is the method for moving the ball randomly.
}
}
然后
public void moveRand(Graphics2D g){
g.drawOval(Random.nextInt() / window.getWidth() + 1, Random.nextInt() / window.getHight() + 1, ballWidth, ballHight);
}
希望這有幫助。
無法解析g
的原因是它在keyPressed()
方法中不存在。 g
是方法paint()
所謂參數。 這意味着您只能在paint()
方法中引用它。 您可以在方法paint()
之外聲明另一個名為g
變量,但它不會是相同的g
。 它甚至不必具有相同的類型。
也就是說,您不需要直接引用g
來解決您的問題。 paint()
方法已經處理繪制圓形。 您只需要更改圓圈的位置並告訴應用程序重新繪制它。 如果為x
和y
分配新值,然后調用repaint()
,則圓圈將移動。 請注意,已在keyPressed()
方法的末尾調用repaint()
。
這是一個例子。 將等於右側的代碼替換為可能適合您的問題的代碼。
else if(keyCode == KeyEvent.VK_Z)
{
x = x + radius;
y = y + radius;
}
如果你看看其他if / else子句,你會發現這也是他們正在做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.