[英]How to draw a Oval once a Button is Pressed? Eclipse?
Okay So, I have a title screen for my game (ie, Title of the Game, play Button, customize Button, settings Button and Quit Button). 好的,我有一个游戏的标题屏幕(例如,游戏标题,播放按钮,自定义按钮,设置按钮和退出按钮)。
What I did is I have drawn a Circle using the pain Component and have also used the keyListener so that Once an arrow key is pressed it will go to that direction. 我所做的是使用痛苦组件绘制了一个Circle,并且还使用了keyListener,这样一旦按下箭头键,它将朝该方向移动。 What it is doing is that Once I Run the Code, It will show me my Title Screen and Also the Ball that I have drawn.
它的作用是,一旦我运行代码,它将显示我的标题屏幕以及我绘制的球。
But that is not what I am stuck On. 但这不是我坚持的目标。 What I need help in is I don't want to see the ball on the Title screen as sooon as I run the Code.
我需要帮助的是,我不希望在运行代码时这么快就看到标题屏幕上的球。 I want the ball to appear when I press the Play Button not when I open the game or run it.
我希望球在我按下“播放”按钮时出现,而不是在我打开或运行游戏时出现。
I have tried using actionListener but that Hasn't worked. 我尝试使用actionListener,但是没有用。 So, It would be Good If you can hhelp me solve this issue.
因此,如果您能帮助我解决此问题,那就太好了。
My GamePanel Class: 我的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();
}
}
My Driver: 我的司机:
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);
}
}
Change your mouseClick(...) to:
int x, y;
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
Override paint(...): 覆盖油漆(...):
@Override
public void paint(Graphics g) {
drawCircle(x, y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.