JAVA 자료실

두더지 게임(Simple...)

작성자 정보

  • 작성자 최고관리자
  • 작성일

컨텐츠 정보

본문


//DDGFrame.java -----------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Time;
class Rectangle {
 int x, y, w, h;
}
public class DDGFrame extends JFrame implements MouseListener, ActionListener  {
 JPanel textPanel;
 JTextField timeText;
 JTextField countText;
 MyPanel stagePanel;
 JButton startButton;
 
 Time startTime;
 Time currentTime;
 int count = 0;
 Rectangle r;
 class MyPanel extends JPanel {
  public void paintComponent(Graphics g) {
   super.paintComponents(g);
   if(count > 0)
    g.drawRect(r.x, r.y, r.w, r.h);
  }
 }
 
 public DDGFrame() {
  setTitle("두더지잡기");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  setLayout(new BorderLayout(10, 10));
  
  r = new Rectangle();
  
  textPanel = new JPanel();
  timeText = new JTextField();
  countText = new JTextField("" + count);
  textPanel.setLayout(new GridLayout(0, 2));
  textPanel.add(timeText);
  textPanel.add(countText);
  
  stagePanel = new MyPanel();
  stagePanel.setSize(500, 500);
  stagePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  stagePanel.addMouseListener(this);
  
  startButton = new JButton("Start");
  startButton.addActionListener(this);
  
  add(textPanel, BorderLayout.NORTH);
  add(stagePanel, BorderLayout.CENTER);
  add(startButton, BorderLayout.SOUTH);
  setSize(500, 600);
 }
 
 public static void main(String[] args) {
  DDGFrame f = new DDGFrame();
 }
 
 @Override
 public void actionPerformed(ActionEvent arg0) {
  startButton.setEnabled(false);
  count = 10;
  countText.setText("" + count);
  
  r.x = (int) (Math.random()*500);
  r.y = (int) (Math.random()*500);
  r.w = 10;
  r.h = 10;
  
  repaint();
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  if(count > 0)
   count--;
  countText.setText("" + count);
  r.x = (int) (Math.random()*500);
  r.y = (int) (Math.random()*500);
  r.w = 10;
  r.h = 10;
  
  repaint();
  
  if(count == 0) {
   startButton.setEnabled(true);
  }
  
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
}

관련자료

댓글 0
등록된 댓글이 없습니다.
알림 0