我正在尝试修改此程序,以便绘制城堡的图像,并且我可以使用向上和向下箭头键缩放此图像。我无法让keylistener工作,程序运行但它没有响应按键。任何帮助将不胜感激,谢谢。
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
public class DrawImage extends JFrame implements KeyListener {
int scale = 1;
private Image image;
enter code here
public static void main(String[] args) {
new DrawImage();
}
public DrawImage() {
super("DrawImage");
setSize(600,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
image = tk.getImage(getURL("castle.png"));
addKeyListener(this);
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
AffineTransform trans = new AffineTransform();
trans.scale(scale, scale);
System.out.println("scale: " + scale);
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getSize().width, getSize().height);
g2d.setTransform(trans);
g2d.drawImage(image, 0, 40, this);
addKeyListener(this);
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) {
scale++;
}
if (key == KeyEvent.VK_DOWN) {
scale--;
}
}
}
答案 0 :(得分:8)
我无法让keylistener工作,程序运行但它没有响应按键。
你的常见问题经常在这里提出,几乎总是问题是由于缺乏焦点 - 被收听的组件没有当前的焦点,并且焦点对于KeyListener的工作至关重要。
简短的回答是给予正在收听焦点的组件。
更好的答案是不使用KeyListeners,而是使用Key Bindings来实现这样的项目。
修改强>
其他问题:
paintComponent(Graphics g)
覆盖中进行绘制。super.paintComponent(g)
覆盖内的paintComponent(Graphics g)
。 编辑2
你永远不想忽略异常,因为你的代码在这里显示,因为这是编程相当于驾驶盲人:
try {
url = this.getClass().getResource(filename);
} catch (Exception e) {
}
希望这不是您的生产代码的外观,您只是为了简洁而忽略了发布代码中的异常,但我们很多人都明白这就像在粉笔板上听到指甲一样。
编辑3
有关KeyListeners与Key Bindings的更多信息:假设您的代码与KeyListener一起使用,那么假设您向GUI添加了任何其他可聚焦组件,并且它们通过用户交互以某种方式获得了焦点,那么您的KeyBindings将不再起作用。如果您使用Key Bindings正确完成了这一操作,那么这不是问题。
编辑4
你真的希望你的scale字段是double,而不是int。并且你真的不想增加和减少它,而是你想要乘以它并将它除以一些乘数常数,比如1.2作为例子。每当您更改比例时,您还需要致电repaint()
。
编辑5
请查看两个示例程序,第一个名为DrawImagePanelKeyListener.java,使用KeyListener,而第二个名为DrawImagePanelKeyBindings,使用Key Bindings。它们都应按预期编译,运行和运行:当您按向上或向下箭头键时,显示的图像会缩小并增大。当按下JButton时,可以看到它们的行为差异。按下按钮,查看您的密钥响应会发生什么。当具有KeyListener的组件失去焦点时,其KeyListener将停止工作,但对于使用键绑定的组件则不同。
可以解决这个问题的一个问题可能是阻止所有其他组件获得焦点,但对于大多数GUI而言,这是不切实际或不可取的。
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.net.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class DrawImagePanelKeyListener extends JPanel implements KeyListener {
public static final String IMAGE_PATH = "https://duke.kenai.com/"
+ "nyanya/.Midsize/NyaNya.jpg.png";
private static final double MULTIPLIER = 1.2;
double scale = 1.0;
private Image image;
private Dimension initialSize = new Dimension(0, 0);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DrawImagePanelKeyListener drawImage = new DrawImagePanelKeyListener();
drawImage.add(new JButton("Foo"));
JFrame frame = new JFrame("Draw Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawImage);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public DrawImagePanelKeyListener() {
setFocusable(true);
requestFocusInWindow();
URL imgURL;
try {
imgURL = new URL(IMAGE_PATH);
image = ImageIO.read(imgURL);
initialSize = new Dimension(image.getWidth(this),
image.getHeight(this));
addKeyListener(this);
setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return initialSize;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getSize().width, getSize().height);
Graphics2D g2d = (Graphics2D) g.create();
g2d.scale(scale, scale);
g2d.drawImage(image, 0, 0, this);
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) {
scale *= MULTIPLIER;
}
if (key == KeyEvent.VK_DOWN) {
scale /= MULTIPLIER;
}
repaint();
}
}
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.net.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class DrawImagePanelKeyBindings extends JPanel {
public static final String IMAGE_PATH = "https://duke.kenai.com/"
+ "nyanya/.Midsize/NyaNya.jpg.png";
private static final double MULTIPLIER = 1.2;
double scale = 1.0;
private Image image;
private Dimension initialSize = new Dimension(0, 0);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DrawImagePanelKeyBindings drawImage = new DrawImagePanelKeyBindings();
drawImage.add(new JButton("Foo"));
JFrame frame = new JFrame("Draw Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawImage);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public DrawImagePanelKeyBindings() {
setBindings();
URL imgURL;
try {
imgURL = new URL(IMAGE_PATH);
image = ImageIO.read(imgURL);
initialSize = new Dimension(image.getWidth(this),
image.getHeight(this));
setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setBindings() {
int condition = WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
final KeyStroke[] keyStrokes = {
KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)
};
for (final KeyStroke keyStroke : keyStrokes) {
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent evt) {
myKeyPressed(keyStroke.getKeyCode());
}
});
}
}
@Override
public Dimension getPreferredSize() {
return initialSize;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getSize().width, getSize().height);
Graphics2D g2d = (Graphics2D) g.create();
g2d.scale(scale, scale);
g2d.drawImage(image, 0, 0, this);
}
public void myKeyPressed(int key) {
if (key == KeyEvent.VK_UP) {
scale *= MULTIPLIER;
}
if (key == KeyEvent.VK_DOWN) {
scale /= MULTIPLIER;
}
repaint();
}
}