Search

'게임'에 해당되는 글 2건

  1. 2014.01.16 JAVA 공던지기 게임 완성
  2. 2013.08.13 JAVA 타자연습 프로그램

JAVA 공던지기 게임 완성

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

package com.tistory.tansanc;

 

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

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.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

 

// 그림이 그려지는 컴포넌트를 정의

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

 

       g.setColor(new Color(0, 0, 255));

       g.drawOval(400, 150, 100, 50);

       // x : 400 ~ 500

       // y : 150 ~ 200

       g.drawLine(500, 150, 500, 600);

 

       g.setFont(new Font("Monospaced", Font.BOLD, 20));

      g.drawString("점수 : " + point, 10, 100);

       if (x + 25 > 400 && x + 25 < 500 && y + 25 > 150 && y + 25 < 200) {

          pointBool = true;

       } else {

          if (pointBool == true) {

             point++;

 

          }

          pointBool = false;

       }

       Image image;

       try {

          //image = ImageIO.read(this.getClass().getResource("angry.png"));

          image= Toolkit.getDefaultToolkit().getImage("angry.png");

          int w = image.getWidth(null);

          int h = image.getHeight(null);

          g.drawImage(image, x, y, 50, 50, null);

       } catch (Exception e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

       }

 

      

   }

 

   boolean pointBool = false;

   int point = 0;

}

 

// 프레임 컴포넌트를 상속받아서 정의

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 || c.x < -50) {

                 break;

             }

             if (c.y > 600 || c.y < -50) {

                 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 실습 #1  (0) 2014.01.17
JAVA File 실습  (0) 2014.01.17
JAVA 공던지기 게임  (0) 2014.01.16
JAVA 가계부 #2  (0) 2014.01.14
JAVA 가계부  (0) 2014.01.14

JAVA 타자연습 프로그램

Programming/JAVA,JSP 2013. 8. 13. 12:18 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tansanc.tistory;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class MyFrame extends JFrame implements KeyListener
{
	JLabel staticText;
	JTextField inputText;
	JLabel resultText;
	long start;
	long end;
	String problemArray[] =
	{ "JAVA", "안녕하세요" };
	ArrayList problemList = new ArrayList();

	public MyFrame()
	{
		problemList.add("JAVA");
		problemList.add("안녕하세요");
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("이벤트 예제");
		staticText = new JLabel("Hello World");
		add(staticText, BorderLayout.NORTH);
		inputText = new JTextField("");
		add(inputText, BorderLayout.CENTER);
		resultText = new JLabel("Hello World");
		add(resultText, BorderLayout.SOUTH);

		inputText.addKeyListener(this);

		setVisible(true);
		start = System.currentTimeMillis();
	}

	@Override
	public void keyPressed(KeyEvent e)
	{
	}

	@Override
	public void keyReleased(KeyEvent e)
	{

	}

	@Override
	public void keyTyped(KeyEvent e)
	{
		if (staticText.getText().equals(inputText.getText()))
		{
			Random r = new Random();
			end = System.currentTimeMillis();
			resultText.setText("정답입니다. " + inputText.getText().length()
					/ ((end - start) / 1000.0 / 60.0));
			staticText.setText(problemList.get(r.nextInt(2)));
			inputText.setText("");
		}
	}
}

public class Test
{
	public static void main(String[] args)
	{
		MyFrame t = new MyFrame();
	}
}

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

JAVA 계산기 프로그램 부분완성  (0) 2013.08.16
JAVA 계산기 레이아웃  (0) 2013.08.16
JAVA Console Token 구현  (0) 2013.08.07
JAVA 제네릭을 사용한 Store Class  (0) 2013.08.07
JAVA DRAG 가능한 Component 만들기  (0) 2013.08.03