好吧,我有一个游戏的标题屏幕(即游戏标题,播放按钮,自定义按钮,设置按钮和退出按钮)。如何在按下按钮时绘制椭圆形?蚀?
我做的是我画了一个使用痛苦组件的圆,并且还使用了keyListener,这样一旦按下箭头键,它就会朝着那个方向。 它所做的是,一旦我运行代码,它会向我显示我的标题屏幕以及我绘制的球。
但是,这不是我卡在。我需要帮助的是我不想在标题屏幕上看到球,因为我运行代码。当我按下播放按钮而不是当我打开游戏或运行游戏时,我想让球出现。
我已经尝试使用actionListener但没有工作。所以,如果你能帮助我解决这个问题,那将是好事。
我的GamePanel类:
package patel.Jainam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel implements ActionListener, KeyListener {
Timer tm = new Timer(5,this);
int x = 0;
int y = 0;
int velX = 0;
int velY = 0;
private static final long serialVersionUID = 1L;
private JLabel welcomeScreen;
private JButton playButton;
private JButton custButton;
private JButton settButton;
private JButton quitButton;
private JButton backButton;
public GamePanel() {
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
setBackground(Color.black);
welcomeScreen = new JLabel (" Fall Down 4 ", SwingConstants.CENTER);
welcomeScreen.setFont(new Font("Times New Roman", Font.ITALIC, 100));
welcomeScreen.setForeground(Color.white);
add(welcomeScreen);
welcomeScreen.setVisible(true);
playButton = new JButton (" Play ");
playButton.setFont(new Font("Times New Roman", Font.ITALIC, 90));
playButton.setBackground(Color.black);
playButton.setForeground(Color.GREEN);
add(playButton);
playButton.setVisible(true);
playButton.addActionListener(new drawBallListener());
custButton = new JButton (" Customize ");
custButton.setFont(new Font("Times New Roman", Font.ITALIC, 95));
custButton.setBackground(Color.black);
custButton.setForeground(Color.GREEN);
add(custButton);
custButton.setVisible(true);
settButton = new JButton (" Settings ");
settButton.setFont(new Font("Times New Roman", Font.ITALIC, 60));
settButton.setBackground(Color.black);
settButton.setForeground(Color.GREEN);
add(settButton);
settButton.setVisible(true);
quitButton = new JButton (" Quit ");
quitButton.setFont(new Font("Times New Roman", Font.ITALIC, 60));
quitButton.setBackground(Color.black);
quitButton.setForeground(Color.GREEN);
add(quitButton);
quitButton.setVisible(true);
backButton = new JButton (" Go Back ");
backButton.setFont(new Font("Times New Roman", Font.ITALIC, 50));
backButton.setBackground(Color.black);
backButton.setForeground(Color.GREEN);
add(backButton);
backButton.setVisible(false);
}
private class drawBallListener implements ActionListener {
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, 50, 30);
g.setVisible(false);
}
}
@Override
public void keyPressed(KeyEvent e) {
int ballMovement = 5;
int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT){
velX = -ballMovement;
velY = 0;
} else if (c == KeyEvent.VK_RIGHT){
velX = ballMovement;
velY = 0;
}
}
@Override
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public void actionPerformed(ActionEvent e) {
if(x < 0){
velX = 0;
x = 0;
}
if (x > 600){
velX = 0;
x = 600;
}
if (y < 0){
velY = 0;
y = 0;
}
if (y > 499){
velY = 0;
y = 499;
}
x = x + velX;
y = y + velY;
repaint();
}
}
我的司机:
package patel.Jainam;
import javax.swing.*;
public class driver {
public static void main(String[] args) {
JFrame frame = new JFrame(" Fall Down 4 ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GamePanel());
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(728, 500);
}
}
此代码甚至不会编译。为什么在ActionListener中有一个paintComponent()方法? g.setVisible(false)是什么意思? '在paintComponent方法应该做什么?图形中没有这种方法。 – FredK
ohh ..当我删除球会留在那里。 –
为什么没有一个标志确定什么时候应该被绘画并且按下按钮时改变它的状态?如果您需要绘制多个对象,则需要某种列表,其中包含为paintComponent绘制它们所需的信息 – MadProgrammer