Search

'JAVA 공던지기 게임'에 해당되는 글 1건

  1. 2014.01.16 JAVA 공던지기 게임

JAVA 공던지기 게임

Programming/JAVA,JSP 2014. 1. 16. 16:49 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

// 그림이 그려지는 컴포넌트를 정의
class MyComponent extends JComponent {

 public int x = 30;
 public int y = 420;
 
 public boolean wire = true;

 public void paint(Graphics g) {
  g.setColor(new Color(167, 114, 48));
  g.drawLine(100, 500, 100, 450);
  g.drawLine(100, 450, 75, 400);
  g.drawLine(100, 450, 125, 400);

  if( wire )
  {
   g.setColor(new Color(0, 255, 0));
   g.drawLine(75, 400, x + 25, y + 25);
   g.drawLine(125, 400, x + 25, y + 25);
  }
  g.setColor(new Color(255, 0, 0));
  g.fillOval(x, y, 50, 50);
 }
}

// 프레임 컴포넌트를 상속받아서 정의
public class StarFrame extends JFrame implements ActionListener,
  MouseMotionListener, MouseListener {
 public static final int WIDTH = 800;
 public static final int HEIGHT = 600;

 JButton next = new JButton("Next");
 MyComponent c;

 public StarFrame() {
  setTitle("MyFrame");
  setSize(WIDTH, HEIGHT);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);

  // MyComponent 객체 생성하여 프레임에 추가
  c = new MyComponent();
  next.addActionListener(this);

  add(c, BorderLayout.CENTER);
  add(next, BorderLayout.SOUTH);

  addMouseMotionListener(this);
  addMouseListener(this);
 }

 public static void main(String[] args) {
  StarFrame frame = new StarFrame();
 }

 @Override
 public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
 }

 @Override
 public void mouseDragged(MouseEvent arg0) {
  // TODO Auto-generated method stub
  if (arg0.getX() < 140 && arg0.getY() > 400) {

   c.x = arg0.getX()-30;
   c.y = arg0.getY()-60;
   c.repaint();
  }

 }

 @Override
 public void mouseMoved(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseClicked(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseEntered(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseExited(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }

 boolean mouse = false;
 int startX;
 int startY;
 int endX;
 int endY;

 @Override
 public void mousePressed(MouseEvent arg0) {
  // TODO Auto-generated method stub
  if (arg0.getX() < 140 && arg0.getY() > 400) {
   if (mouse == false) {

    c.wire = true;
    c.x = arg0.getX()-30;
    c.y = arg0.getY()-60;
    c.repaint();
    startX = arg0.getX();
    startY = arg0.getY();
    mouse = true;
    
   }
  }
 }

 @Override
 public void mouseReleased(MouseEvent arg0) {
  // TODO Auto-generated method stub

  if (mouse == true) {
   endX = arg0.getX();
   endY = arg0.getY();
   System.out.println("power X : " + (startX - endX));
   System.out.println("power Y : " + (startY - endY));
   mouse = false;
   c.wire = false;
   if( ft != null)
   {
    ft.stop();
   }
   ft = new FlyThread((startX - endX), (startY - endY));
   ft.start();
  }
 }
 FlyThread ft;
 class FlyThread extends Thread
 {
  int powerX;
  int powerY;
  FlyThread(int powerX, int powerY)
  {
   this.powerX = powerX;
   this.powerY = powerY;
  }
  @Override
  public void run() {
   while(true)
   {
    if( c.x > 800)
    {
     break;
    }
    if( c.y > 600)
    {
     break;
    }
    c.x += powerX/10;
    c.y += powerY/10;
    powerY += 1;
    try {
     sleep(10);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    c.repaint();
    
   }
  }
 }

}

'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA File 실습  (0) 2014.01.17
JAVA 공던지기 게임 완성  (0) 2014.01.16
JAVA 가계부 #2  (0) 2014.01.14
JAVA 가계부  (0) 2014.01.14
JAVA 쿵쿵따 게임 #2 중복 탐지  (0) 2014.01.09