如何使用 Java 中的 Key Listener 在 JDialog Window 中将一个图像绘制到另一个图像上?
How can I draw an Image onto another Image in the JDialog Window using Key Listener in Java?
- TL;DR:
App currently looks like IMAGE1, I want to show another .png file after key press, so it looks like in IMAGE2. How can I draw second image on my background image?
- 详细问题描述:
我 运行 在尝试将一张图片叠加在另一张图片上时遇到了麻烦。
我加载了背景图像,它在我的 JDialog 中可见 window:
我还为我的 Window 实现了 Window、鼠标和按键侦听器,因此它会在鼠标离开 Window 或单击鼠标时关闭 - 完美运行。
但是我想在用户按下特定键时显示另一个图像(window左上角的小图标(key listener 也已经工作了),所以它看起来或多或少像这样:
也有可能我以后需要显示更多的图标,但是问题是,我画了一张图片后,另一张是不可见的(和看起来像图像 1)。你能指导我怎样才能达到这样的效果吗?也许我根本不应该使用 conentPane?
主要Window代码:
public class MychoWindow extends JDialog {
private Container contentPane = this.getContentPane();
(... variables etc ...)
public MychoWindow() {
// Set the main properties of the Window:
setTitle(WINDOW_TITLE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Draw the background image:
drawBackgroundImage();
// drawCatDefenseIcon();
// Show the Window:
centerWindowOnTheScreen();
setVisible(true);
// Set starting Variables:
setWindowCenterX(getLocationOnScreen().x + WINDOW_WIDTH / 2);
setWindowCenterY(getLocationOnScreen().y + WINDOW_HEIGHT / 2);
// Add various Listeners:
addWindowListener(new WindowListener());
addMouseListener(new MouseListener());
addKeyListener(new KeyboardListener());
}
}
背景绘制方法:
private void drawBackgroundImage() {
// Load Image from file:
ImageIcon imageIcon = new ImageIcon(MychoWindow.class.getResource(BACKGROUND_IMAGE_URL));
// Transform it to Image to be able to resize it, and then transform it back to use it in JLabel:
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(WINDOW_WIDTH - IMAGE_FRAME_SIZE, WINDOW_HEIGHT - IMAGE_FRAME_SIZE, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
imageIcon = new ImageIcon(newimg);
// Add prepared image to the Label which is added to content pane:
JLabel label = new JLabel(imageIcon);
// Add it to container:
contentPane.add(label);
}
图标绘制方法:
public void drawCatDefenseIcon() throws IOException {
// TODO: SOMEHOW MAKE IT WORK
(I tried to use the BufferedImage and Graphics2d here,
I also tried the approach from above - drawBackgroundImage() method,
but I just could not get it to work.)
}
Key Listener - 以防万一:
public class KeyboardListener extends KeyAdapter {
@Override
public void keyReleased(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_K || event.getKeyCode() == KeyEvent.VK_C) {
MychoWindow.setCatDefenseState(!MychoWindow.getCatDefenseState());
System.out.println("SHOW THAT KEY WAS PRESSED");
}
}
}
在附加的 IMAGE2 上实现可见效果的最佳方法是什么?
提前致谢!
一种方法是在背景标签中添加第二个标签。
类似于:
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new FlowLayout() );
然后当你想添加你使用的第二个图标时:
JLabel onTop = new JLabel( new ImageIcon(...) );
background.removeAll();
background.add( onTop );
background.revalidate();
另一种方法是将现有图标替换为 Compound Icon。
那么逻辑可能是这样的:
Icon original = background.getIcon();
Icon onTop = new ImageIcon(...);
CompoundIcon icon = new CompoundIcon(CompoundIcon.Axis.Z_AXIS, 0, CompoundIcon.LEFT, CompoundIcon.TOP, original, onTop);
background.setIcon( icon );
使用一张图片作为背景图片,将另一张图片放入图片框。它会帮助你
例如:
图片框有图像和背景图像属性
要设置背景图片,您必须设置 pictureBox1.BackgroundImage=您的图片;
和图像 属性 pictureBox1.Image=您的图像;
- TL;DR:
App currently looks like IMAGE1, I want to show another .png file after key press, so it looks like in IMAGE2. How can I draw second image on my background image?
- 详细问题描述:
我 运行 在尝试将一张图片叠加在另一张图片上时遇到了麻烦。
我加载了背景图像,它在我的 JDialog 中可见 window:
我还为我的 Window 实现了 Window、鼠标和按键侦听器,因此它会在鼠标离开 Window 或单击鼠标时关闭 - 完美运行。
但是我想在用户按下特定键时显示另一个图像(window左上角的小图标(key listener 也已经工作了),所以它看起来或多或少像这样:
也有可能我以后需要显示更多的图标,但是问题是,我画了一张图片后,另一张是不可见的(和看起来像图像 1)。你能指导我怎样才能达到这样的效果吗?也许我根本不应该使用 conentPane?
主要Window代码:
public class MychoWindow extends JDialog { private Container contentPane = this.getContentPane(); (... variables etc ...) public MychoWindow() { // Set the main properties of the Window: setTitle(WINDOW_TITLE); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setResizable(false); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // Draw the background image: drawBackgroundImage(); // drawCatDefenseIcon(); // Show the Window: centerWindowOnTheScreen(); setVisible(true); // Set starting Variables: setWindowCenterX(getLocationOnScreen().x + WINDOW_WIDTH / 2); setWindowCenterY(getLocationOnScreen().y + WINDOW_HEIGHT / 2); // Add various Listeners: addWindowListener(new WindowListener()); addMouseListener(new MouseListener()); addKeyListener(new KeyboardListener()); } }
背景绘制方法:
private void drawBackgroundImage() { // Load Image from file: ImageIcon imageIcon = new ImageIcon(MychoWindow.class.getResource(BACKGROUND_IMAGE_URL)); // Transform it to Image to be able to resize it, and then transform it back to use it in JLabel: Image image = imageIcon.getImage(); Image newimg = image.getScaledInstance(WINDOW_WIDTH - IMAGE_FRAME_SIZE, WINDOW_HEIGHT - IMAGE_FRAME_SIZE, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way imageIcon = new ImageIcon(newimg); // Add prepared image to the Label which is added to content pane: JLabel label = new JLabel(imageIcon); // Add it to container: contentPane.add(label); }
图标绘制方法:
public void drawCatDefenseIcon() throws IOException { // TODO: SOMEHOW MAKE IT WORK (I tried to use the BufferedImage and Graphics2d here, I also tried the approach from above - drawBackgroundImage() method, but I just could not get it to work.) }
Key Listener - 以防万一:
public class KeyboardListener extends KeyAdapter { @Override public void keyReleased(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_K || event.getKeyCode() == KeyEvent.VK_C) { MychoWindow.setCatDefenseState(!MychoWindow.getCatDefenseState()); System.out.println("SHOW THAT KEY WAS PRESSED"); } } }
在附加的 IMAGE2 上实现可见效果的最佳方法是什么?
提前致谢!
一种方法是在背景标签中添加第二个标签。
类似于:
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new FlowLayout() );
然后当你想添加你使用的第二个图标时:
JLabel onTop = new JLabel( new ImageIcon(...) );
background.removeAll();
background.add( onTop );
background.revalidate();
另一种方法是将现有图标替换为 Compound Icon。
那么逻辑可能是这样的:
Icon original = background.getIcon();
Icon onTop = new ImageIcon(...);
CompoundIcon icon = new CompoundIcon(CompoundIcon.Axis.Z_AXIS, 0, CompoundIcon.LEFT, CompoundIcon.TOP, original, onTop);
background.setIcon( icon );
使用一张图片作为背景图片,将另一张图片放入图片框。它会帮助你 例如:
图片框有图像和背景图像属性
要设置背景图片,您必须设置 pictureBox1.BackgroundImage=您的图片;
和图像 属性 pictureBox1.Image=您的图像;