按下按钮后如何绘制椭圆形?日食?

时间:2016-06-07 19:01:37

标签: java swing drawing

好的,我有一个游戏的标题画面(即游戏标题,播放按钮,自定义按钮,设置按钮和退出按钮)。

我所做的是使用pain组件绘制了一个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);    
  }
} 

1 个答案:

答案 0 :(得分:0)

 @Override
    public void paint(Graphics g) {
      drawCircle(x, y);
    }

覆盖绘画(...):

java jar myapp.war