以下是来自" Simon"的代码。我已经在运行中显示应该显示的正确段但是我的keyPress有问题,当我使用它时它不会使用箭头键点亮。不是一个伟大的编码器,我真的需要一些帮助。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Arc2D;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Simon extends JFrame implements KeyListener {
// public DrawStuff game;
public static Simon simon;
Graphics2D g2;
Graphics2D g3;
Graphics2D g4;
Graphics2D g5;
public JFrame frame = new JFrame();
public JPanel panel = new JPanel();
private ActionListener timerAction;
public Simon() {
frame = new JFrame();
panel = new JPanel();
// Sets up JFrame
frame.setTitle("Simon Says");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawStuff game = new DrawStuff();
panel.setLayout(new BorderLayout());
panel.add(game, BorderLayout.CENTER);
frame.add(panel);
frame.addKeyListener(this);
// game.setFocusable(true);
// game.requestFocus();
frame.setVisible(true);
}
public class DrawStuff extends JPanel {
public void paint(Graphics g) {
g2 = (Graphics2D) g;
g3 = (Graphics2D) g;
g4 = (Graphics2D) g;
g5 = (Graphics2D) g;
// assume d == 145 && e == 90
g2.setPaint(Color.BLUE.darker());
g2.fill(new Arc2D.Double(45, 45, 400, 400, 145, 90, Arc2D.PIE));
g3.setPaint(Color.RED.darker());
g3.fill(new Arc2D.Double(45, 45, 400, 400, 235, 90, Arc2D.PIE));
g4.setPaint(Color.GREEN.darker());
g4.fill(new Arc2D.Double(45, 45, 400, 400, 325, 90, Arc2D.PIE));
g5.setPaint(Color.YELLOW.darker());
g5.fill(new Arc2D.Double(45, 45, 400, 400, 55, 90, Arc2D.PIE));
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
g5.setPaint(Color.YELLOW);
//Color YELLOW = Color.WHITE;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
g2.setPaint(Color.BLUE.darker());
//Color BLUE = Color.WHITE;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
g3.setPaint(Color.RED.darker());
//Color RED = Color.WHITE;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
g4.setPaint(Color.GREEN.darker());
//Color GREEN = Color.WHITE;
}
panel.repaint();
}
public void keyReleased(KeyEvent e) {
/**
* if (e.getKeyCode() == KeyEvent.VK_UP) { Color YELLOW = Color.YELLOW;
* repaint(); }
*
* if (e.getKeyCode() == KeyEvent.VK_LEFT) { Color BLUE = Color.BLUE;
* repaint(); }
*
* if (e.getKeyCode() == KeyEvent.VK_DOWN) { Color RED = Color.RED;
* repaint(); }
*
* if (e.getKeyCode() == KeyEvent.VK_RIGHT) { Color GREEN = Color.GREEN;
* repaint(); }
**/
}
@Override
public void keyTyped(KeyEvent e) {
}
// TODO Auto-generated method sub
public static void main(String[] args) {
simon = new Simon();
}
}
答案 0 :(得分:3)
你在这里做了一些猜测,但这不会起作用。相反,我建议你:
其他问题:
paintComponent
方法而不是其paint
方法。 super.paintComponent(g);
,因为这将允许JPanel进行内务处理绘制,包括擦除旧的脏图像。有关我正在谈论的内容的示例,请参阅this answer of mine to a similar question。这将创建此GUI: