카테고리 없음

포물선 그리기

bo97037 2014. 12. 11. 16:24

package ex;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;


class GraphTestPanel extends JPanel implements Runnable, KeyListener, ActionListener
{
 int i = 90;
 int o=0;
 JTextField tx = new JTextField(5);
 JButton jb = new JButton("dff");
 
 
 Thread th=null;
 @Override
 public void paintComponent(Graphics g){
  g.drawLine(0, 250, 500, 250);
  g.drawLine(250, 0, 250, 500);
 
  if(i<180)
  {i++;g.fillOval(i, (int)(Math.sin(i*Math.PI/90)*o+250),5,5);}
 }
 
 
 
 
 
 
 public GraphTestPanel(){
  
  
  

 
      
 
     this.add(tx);
  this.add(jb);
 
 

  
  
  this.setSize(520, 545);
  

 
 jb.addActionListener(this);
 tx.addKeyListener(this);
  
  
 }
 
 
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   while(i < 180){
   Thread.sleep(30);
 
   repaint();}i=90;
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 

 


 public void keyPressed(KeyEvent e) {
  // TODO Auto-generated method stub
  if(e.getKeyCode()==KeyEvent.VK_ENTER)
  {
   o=Integer.parseInt(tx.getText());
       
        th=new Thread(this);
        th.start();
    
  }
 }

 @Override
 public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

 

 


 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
        o=(int)(Math.random()*90);
        System.out.println(o);
        th=new Thread(this);
        th.start();
 }
 

 
 
 
 
}


public class GraphTest extends JFrame {

 
 public GraphTest(){
  this.setSize(600, 600);
  this.setDefaultCloseOperation(3);
  this.add(new GraphTestPanel());
  this.setVisible(true);
 
  
  
 }
 
 

 
 
 public static void main(String[] args){
  
  new GraphTest();
  
 
 
 }
 
 
 
 
}