简体   繁体   中英

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. 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. So, It would be Good If you can hhelp me solve this issue.

My GamePanel Class:

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM