gpt4 book ai didi

java - 椭圆形移动程序。椭圆不动,有什么我可以在这里炼化的吗?

转载 作者:行者123 更新时间:2023-11-29 04:13:26 25 4
gpt4 key购买 nike

我的代码在这里需要一点帮助。我遵循了创建键盘事件的过程,如果我向下按箭头键则向下移动我的椭圆形,如果我向上按箭头键则向上移动等等。但是,我一直无法这样做。我可以从这里的代码中改进什么吗?还计划在两个按钮中实现它,即此处的右翻译左翻译 按钮。

提前致谢! Here is the example UI that I've made so far.

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class page309_4 extends JFrame { //implements ActionListener
private JButton btnLeftMvmt, btnRightMvmt;
int oval_x = 150;
int oval_y = 150;

public page309_4(){
setTitle("Oval Mover");
setSize(600, 150);
setLayout(new BorderLayout());
JPanel panel1, panel2;

panel1 = new JPanel();
panel2 = new JPanel();

panel1.setSize(500,300);
panel1.add(new MyPanel());
panel1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_UP: oval_y += 10; break;
case KeyEvent.VK_DOWN: oval_y -= 10; break;
case KeyEvent.VK_LEFT: oval_x -= 10; break;
case KeyEvent.VK_RIGHT: oval_x += 10; break;
}
}
});
panel1.setBackground(Color.YELLOW);

btnLeftMvmt = new JButton("Left Translation");
btnRightMvmt = new JButton("Right Translation");
btnLeftMvmt.addActionListener(bleft -> {


});
btnRightMvmt.addActionListener(bright -> {


});


panel2.add(btnLeftMvmt);
panel2.add(btnRightMvmt);


add(panel1);
add(panel2, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);

}

class MyPanel extends JPanel {

public MyPanel() {
setPreferredSize(new Dimension(600, 150));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
int oval_x = (getWidth() - 150) / 2;
int oval_y = (getHeight() - 150) / 2;
g.fillOval(oval_x, oval_y, 150, 150);
}
}

public static void main (String[] args) {
// TODO Auto-generated method stub
page309_4 f = new page309_4();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

最佳答案

让我们从...开始

@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
int oval_x = (getWidth() - 150) / 2;
int oval_y = (getHeight() - 150) / 2;
g.fillOval(oval_x, oval_y, 150, 150);
}
}

x/y 位置是在方法中定义的,因此无论您做什么,它都不会改变。

话虽如此,MyPanel 应该负责直接管理 x/y 位置,这意味着 oval_xoval_y 变量应该是其中定义。然后,您应该定义可以更改这些值的功能,例如...

  class MyPanel extends JPanel {

private int oval_x = 150;
private int oval_y = 150;

public MyPanel() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 150);
}

public void moveVerticallyBy(int delta) {
oval_y += delta;
repaint();
}

public void moveHorizontallyBy(int delta) {
oval_x += delta;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
g.fillOval(oval_x, oval_y, 150, 150);
}
}

这是封装的基本概念

接下来,我强​​烈建议使用 key bindings APIKeyListener 之上,因为它将解决与焦点相关的问题。

例如……

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Page309_4 extends JFrame { //implements ActionListener

private JButton btnLeftMvmt, btnRightMvmt;
private MyPanel myPanel;

public Page309_4() {
setTitle("Oval Mover");
setSize(600, 150);
setLayout(new BorderLayout());
JPanel panel1, panel2;

panel2 = new JPanel();

myPanel = new MyPanel();
InputMap im = myPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = myPanel.getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Pressed.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Pressed.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Pressed.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Pressed.right");

am.put("Pressed.up", new VerticalMovementAction(-10, myPanel));
am.put("Pressed.down", new VerticalMovementAction(10, myPanel));
am.put("Pressed.left", new HorizontalMovementAction(-10, myPanel));
am.put("Pressed.right", new HorizontalMovementAction(10, myPanel));

btnLeftMvmt = new JButton("Left Translation");
btnRightMvmt = new JButton("Right Translation");
btnLeftMvmt.addActionListener(bleft -> {

});
btnRightMvmt.addActionListener(bright -> {

});

panel2.add(btnLeftMvmt);
panel2.add(btnRightMvmt);

add(myPanel);
add(panel2, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);

}

public class VerticalMovementAction extends AbstractAction {

private int delta;
private MyPanel myPanel;

public VerticalMovementAction(int delta, MyPanel myPanel) {
this.delta = delta;
this.myPanel = myPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
myPanel.moveVerticallyBy(delta);
}

}

public class HorizontalMovementAction extends AbstractAction {

private int delta;
private MyPanel myPanel;

public HorizontalMovementAction(int delta, MyPanel myPanel) {
this.delta = delta;
this.myPanel = myPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
myPanel.moveHorizontallyBy(delta);
}

}

class MyPanel extends JPanel {

private int oval_x = (600 - 150) / 2;
private int oval_y = (300 - 150) / 2;

public MyPanel() {
setBackground(Color.YELLOW);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 300);
}

public void moveVerticallyBy(int delta) {
oval_y += delta;
repaint();
}

public void moveHorizontallyBy(int delta) {
oval_x += delta;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
g.fillOval(oval_x, oval_y, 150, 150);
}
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Page309_4 f = new Page309_4();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}

关于java - 椭圆形移动程序。椭圆不动,有什么我可以在这里炼化的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732564/

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