gpt4 book ai didi

java - 如何将值传递给菜单项单击时的旋转方法?

转载 作者:行者123 更新时间:2023-11-30 04:17:03 26 4
gpt4 key购买 nike

我正在开发旋转图像的应用程序。当用户单击菜单项时,图像应该旋转。现在我已经实现了键盘监听器,其中当用户按从右到左的按钮时,它会移动,但我想更改该方法并希望根据菜单项单击。

现在它将度数变量传递给旋转方法,现在我想自定义它,并且当用户单击菜单项时它会传递值。我不知道该怎么办。

我的代码:

public class RotateIMGn extends JPanel {
private static final long serialVersionUID = 1L;
ImageIcon image = new ImageIcon("D://Workspace//ScaleImage//src//images//img.png");
JLabel label = new JLabel(image);
JPanel rotationPanel;
final int WIDTH = 350;
final int HEIGHT = 500;
double degrees;

public RotateIMGn() {

setPreferredSize(new Dimension(446, 500));

setFocusable(true);

addKeyListener(new KeyboardListener());
rotationPanel = new JPanel();
rotationPanel = new turningCanvas();
rotationPanel.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
add(rotationPanel);

JMenuBar menuBar = new JMenuBar();
add(menuBar);

JMenu mnFile = new JMenu("Rotate");
menuBar.add(mnFile);

ImageIcon icon90 = createImageIcon("/images/images_Right.png");
JMenuItem mntmTR90 = new JMenuItem("Rotate 90+", icon90);

mntmTR90.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
try {

} catch (Exception e) {
e.printStackTrace();
}

}
});

mnFile.add(mntmTR90);

ImageIcon icon180 = createImageIcon("/images/images_Vertical.png");
JMenuItem mntmRT180 = new JMenuItem("Rotate 180+", icon180);
mnFile.add(mntmRT180);

JSeparator separator = new JSeparator();
mnFile.add(separator);

ImageIcon micon90 = createImageIcon("/images/images_Left.png");
JMenuItem mntmTRM90 = new JMenuItem("Rotate 90-", micon90);
mnFile.add(mntmTRM90);

ImageIcon micon180 = createImageIcon("/images/images_Horizontal.png");
JMenuItem mntmRTM180 = new JMenuItem("Rotate 180-", micon180);
mnFile.add(mntmRTM180);
rotationPanel.setBounds(WIDTH / 2, HEIGHT / 2,
rotationPanel.getPreferredSize().width,

rotationPanel.getPreferredSize().height);
degrees = 0;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
}

public class turningCanvas extends JPanel {
private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(degrees), image.getIconWidth() / 2,
image.getIconHeight() / 2);
image.paintIcon(this, g2d, 0, 0);

}
}

public class KeyboardListener implements KeyListener {
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_LEFT) {
degrees--;
repaint();
}
if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
degrees++;
repaint();
}
}

public void keyTyped(KeyEvent event) {
}

public void keyReleased(KeyEvent event) {
}
}

public static void main(String[] args) {
RotateIMGn test = new RotateIMGn();
JFrame frame = new JFrame();
frame.setContentPane(test);
frame.pack();
frame.setVisible(true);
}

protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = RotateIMGn.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}

anyone's idea will help me a lot so...

最佳答案

对菜单项使用 Swing Action 而不是 String。然后,在 actionPerformed 方法中,更新 levels 变量,就像您对 KeyListener 进行操作一样。

类似于:

ImageIcon icon90 = createImageIcon("/images/images_Right.png");
JMenuItem mntmTR90 = new JMenuItem(new AbstractAction("Rotate 90+", icon90) {
public void actionPerformed(ActionEvent e) {
degrees += 90;
repaint();
}
});

关于java - 如何将值传递给菜单项单击时的旋转方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com