[英]How to draw a Oval once a Button is Pressed? Eclipse?
好的,我有一個游戲的標題屏幕(例如,游戲標題,播放按鈕,自定義按鈕,設置按鈕和退出按鈕)。
我所做的是使用痛苦組件繪制了一個Circle,並且還使用了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);
}
}
Change your mouseClick(...) to:
int x, y;
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
覆蓋油漆(...):
@Override
public void paint(Graphics g) {
drawCircle(x, y);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.