Java wav 재생

JAVA/소스 2015. 1. 10. 01:35

import java.io.File;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.Clip;

public class Test {


/**

* @param args

*/

public static void main(String[] args) {

File Clap =new File("test.wav");

PlaySound(Clap);

}

static void PlaySound(File Sound)

{

try{

Clip clip = AudioSystem.getClip();

clip.open(AudioSystem.getAudioInputStream(Sound));

clip.start();

Thread.sleep(clip.getMicrosecondLength()/1000);

}

catch(Exception e)

{

}

}


}

'JAVA > 소스' 카테고리의 다른 글

자바 텍스트 한글자씩 읽기  (0) 2014.10.07
자바 피보나치  (0) 2014.01.13
자바 팩토리얼  (0) 2014.01.13
자바 구조 예제  (0) 2014.01.13
Hello Java  (0) 2014.01.13

자바 텍스트 한글자씩 읽기

JAVA/소스 2014. 10. 7. 19:58

import java.io.*;


public class Graph {


public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

try {

FileReader reader = new FileReader("sample1.inp");

int ch;

while((ch=reader.read())!=-1)

{

System.out.println((char)ch);

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}


}



'JAVA > 소스' 카테고리의 다른 글

Java wav 재생  (0) 2015.01.10
자바 피보나치  (0) 2014.01.13
자바 팩토리얼  (0) 2014.01.13
자바 구조 예제  (0) 2014.01.13
Hello Java  (0) 2014.01.13

자바 피보나치

JAVA/소스 2014. 1. 13. 00:07


public class Fibonacci {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

int su1=0,su2=1;

int count=10;


System.out.println("Fibonacci : "+su1);

System.out.println("Fibonacci : "+su2);

for(int i=3;i<=count;++i)

{

sum=su1+su2;

System.out.println("Fibonacci : "+sum);

su1=su2;

su2=sum;

}

}

}



'JAVA > 소스' 카테고리의 다른 글

Java wav 재생  (0) 2015.01.10
자바 텍스트 한글자씩 읽기  (0) 2014.10.07
자바 팩토리얼  (0) 2014.01.13
자바 구조 예제  (0) 2014.01.13
Hello Java  (0) 2014.01.13

자바 팩토리얼

JAVA/소스 2014. 1. 13. 00:07


public class Factorial {


public static void main(String [] ar)

{

int sum=1;

int count=5;

for(int i=2;i<=count;++i)

{

sum*=i;

}

System.out.println("Factorial : "+ sum);

}

}



'JAVA > 소스' 카테고리의 다른 글

자바 텍스트 한글자씩 읽기  (0) 2014.10.07
자바 피보나치  (0) 2014.01.13
자바 구조 예제  (0) 2014.01.13
Hello Java  (0) 2014.01.13
열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03

자바 구조 예제

JAVA/소스 2014. 1. 13. 00:06

//클래스

public class Hello2 {


//메소드

public static int sum(int n,int m)

{

return n+m;

}

//main()메소드에서 실행시작

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=20;

int s;

char a;

s=sum(i,10); //메소드 호출

a='?';

System.out.println(a); // a 출력

System.out.println("Hello2"); //"Hello2" 문자열 출력

System.out.println(s); //정수 s값 출력

}


}



'JAVA > 소스' 카테고리의 다른 글

자바 피보나치  (0) 2014.01.13
자바 팩토리얼  (0) 2014.01.13
Hello Java  (0) 2014.01.13
열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03
열혈강의 자바 22장 과제 2  (3) 2012.01.11

Hello Java

JAVA/소스 2014. 1. 13. 00:04


public class Hello {

public static void main(String [] ar)

{

System.out.printf("Hello Java!");

}

}



'JAVA > 소스' 카테고리의 다른 글

자바 팩토리얼  (0) 2014.01.13
자바 구조 예제  (0) 2014.01.13
열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03
열혈강의 자바 22장 과제 2  (3) 2012.01.11
열혈강의 자바 22장 과제 1  (1) 2012.01.11

열혈강의 자바 24장 과제 1 [미완성]

JAVA/소스 2012. 2. 3. 20:21

//기본틀만 대충 잡아낫는데... 언제 완성할지...딴거 할게 너무 많으...
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
class Ex1 extends JFrame implements ActionListener{
 private Container con;
 private JPanel p1=new JPanel();
 private JPanel p2=new JPanel();
 private JPanel p3=new JPanel();
 private JPanel p4=new JPanel();
 private JPanel p5=new JPanel();
 private JPanel p6=new JPanel();
 private JPanel p7=new JPanel();
 private JPanel p8=new JPanel();
 private JPanel p9=new JPanel();
 private JPanel p10=new JPanel();
 private JPanel p11=new JPanel();
 private JPanel p12=new JPanel();
 private JPanel p13=new JPanel();
 private JPanel p14=new JPanel();
 private JPanel p15=new JPanel();
 private JPanel p16=new JPanel();
 private JPanel p17=new JPanel();
 
 private CardLayout cl=new CardLayout();
 
 private JTextField tf1=new JTextField();
 private JTextField tf2=new JTextField();
 private JTextField tf3=new JTextField(30);
 private JTextField tf4=new JTextField(30);
 private JTextField tf5=new JTextField(5);
 
 private JTextArea tfa1=new JTextArea();
 
 private JList list1=new JList();
 
 
 private JButton bt1=new JButton("회원가입");
 private JButton bt2=new JButton("로그인");
 private JButton bt3=new JButton("대화시작");
 private JButton bt4=new JButton("전송");
 private JButton bt5=new JButton("종료");
 private JRadioButton rb1=new JRadioButton("귓속말 설정");
 private JRadioButton rb2=new JRadioButton("귓속말 해체");
 private ButtonGroup bg1=new ButtonGroup();
 
 
 private ImageIcon im1 =new ImageIcon("background.jpg");
 private JLabel jl1=new JLabel(im1);
 private JLabel jl2=new JLabel("명");
 public Ex1(String title){
  super(title);
  this.init();
  this.start();
  this.setSize(600,400);
  Dimension screen =Toolkit.getDefaultToolkit().getScreenSize(); //화면 크기 구함
  Dimension frm=super.getSize(); //f사이즈
  //f를 중앙에 출력하기위해 x,y좌표 구함
  int xpos =(int)(screen.getWidth()/2 -frm.getWidth()/2);
  int ypos =(int)(screen.getHeight()/2 -frm.getHeight()/2);
  this.setLocation(xpos,ypos);
  this.setResizable(false);
  this.setVisible(true);
  
 }
 public void init(){
  con=this.getContentPane();
  con.setLayout(cl);
  p1.setLayout(new BorderLayout());
  p1.add("North",p2);
  p2.setLayout(new BorderLayout());
  p2.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED), "Login Module"));
  tf1.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "ID"));
  p2.add("North",tf1);
  tf2.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "PASS"));
  p2.add("Center",tf2);
  p4.setLayout(new FlowLayout());
  p4.add(bt1);
  p4.add(bt2);
  p2.add("South",p4);
 
  p8.setLayout(new BorderLayout());
  p8.add("Center",jl1);  
  p5.setLayout(new GridLayout(1,3,5,5));
  p5.add(p6);
  p5.add(p7);
  p5.add(p1);
  p8.add("South",p5);
  con.add("login",p8);
  p9.setLayout(new BorderLayout());
  p10.setLayout(new FlowLayout());
  p10.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "Nick Name"));
  p10.add(tf3);
  p10.add(bt3);
  p11.setLayout(new BorderLayout());
  p11.add("North",p10);
  
  p12.setLayout(new BorderLayout());
  p12.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "View Pane"));
  p12.add(tfa1);
  p11.add("Center",p12);
  
  p13.setLayout(new FlowLayout());
  p13.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "Talk Data"));
  p13.add(tf4);
  p13.add(bt4);
  p11.add("South",p13);
  
  p9.add("West",p11);
  
  p14.setLayout(new FlowLayout());
  p14.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "총인원"));
  p14.add(tf5);
  p14.add(jl2);
  
  p15.setLayout(new GridLayout(3,1));
  p15.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), "Option"));
  bg1.add(rb1);
  bg1.add(rb2);
  p15.add(rb1);
  p15.add(rb2);
  p15.add(bt5);
  
  p16.setLayout(new BorderLayout());
  p16.add("North",p14);
  
  p16.add("Center",list1);
  p16.add("South",p15);
  
  p9.add("Center",p16);
  
  
  
  con.add("chatting",p9);
 
  
 }
 public void start(){
  bt2.addActionListener(this);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==bt2){
   String id = tf1.getText();
   String pass = tf2.getText();
   if(id == null || pass == null || id.trim().length() == 0 || pass.trim().length() == 0) {
    JOptionPane.showMessageDialog(this,
     "ID나 PASS가 비었습니다.", "경고",
      JOptionPane.ERROR_MESSAGE);
    return;
   }
   cl.show(con, "chatting");
  }
 }
}
public class Ex01 {
 public static void main(String [] ar){
  Ex1 ex=new Ex1("Chatting~!");
 }
}

'JAVA > 소스' 카테고리의 다른 글

자바 구조 예제  (0) 2014.01.13
Hello Java  (0) 2014.01.13
열혈강의 자바 22장 과제 2  (3) 2012.01.11
열혈강의 자바 22장 과제 1  (1) 2012.01.11
자바 21장 과제 2  (0) 2011.12.15

열혈강의 자바 22장 과제 2

JAVA/소스 2012. 1. 11. 02:54

//아 점점 게을러지내... 급하게 하긴햇으...

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.util.Vector;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
class write {
 private String author;
 private String title;
 private String pass;
 private String content;
 public write(String author1,String title2,String pass3,String content4){
  author=author1;
  title=title2;
  pass=pass3;
  content=content4;
 }
 public String getauthor(){
  return author;
 }
 public String gettitle(){
  return title;
 }
 public String getpass(){
  return pass;
 }
 public String getcontent(){
  return content;
 }
}
class Ex2 extends JFrame implements ActionListener, MouseListener{
 private Vector vc=new Vector();
 private write wr;
 private Container con;
 private JButton jbt1=new JButton("리스트보기");
 private JButton jbt2=new JButton("글쓰기");
 private JButton jbt3=new JButton("저장");
 private JButton jbt4=new JButton("취소");
 private JButton jbt5=new JButton("선택항목보기");
 private JButton jbt6=new JButton("닫기");
 private JButton jbt7=new JButton("확인");

 private JPanel jp1=new JPanel();
 private JPanel jp2=new JPanel();
 private JPanel jp3=new JPanel();
 private JPanel jp4=new JPanel();
 private JPanel jp5=new JPanel();
 private JPanel jp6=new JPanel();
 private JPanel jp7=new JPanel();
 private JPanel jp8=new JPanel();
 private JPanel jp9=new JPanel();

 private JLabel jl1=new JLabel("작성자 : ",JLabel.RIGHT);
 private JLabel jl2=new JLabel("제목 : ",JLabel.RIGHT);
 private JLabel jl3=new JLabel("비밀번호 : ",JLabel.RIGHT);
 private JLabel jl4=new JLabel("리스트목록",JLabel.CENTER);
 private JLabel jl5=new JLabel("제목 : ",JLabel.RIGHT);
 private JLabel jl6=new JLabel("작성자 : ",JLabel.RIGHT);
 private JLabel jl7=new JLabel("",JLabel.LEFT);
 private JLabel jl8=new JLabel("",JLabel.LEFT);

 private JTextField jtf1=new JTextField();
 private JTextField jtf2=new JTextField();
 private TextField jtf3=new TextField();

 private JTextArea jta1=new JTextArea();
 private JTextArea jta2=new JTextArea();
 private List List=new List();
 
 private JDialog jd1=new JDialog(this,"글쓰기",false);
 private JDialog jd2=new JDialog(this,"리스트목록",false);
 private JDialog jd3=new JDialog(this,"글보기",false);
 
 public Ex2(){
  super("게시판");
  this.setSize(250,80);
 
  this.init();
  this.start();
  
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frm=this.getSize();
  
  int xpos =(int)(screen.getWidth()/2- frm.getWidth()/2);
  int ypos =(int)(screen.getHeight()/2-frm.getHeight()/2);
  
  this.setLocation(xpos,ypos);
  this.setResizable(false);
  this.setVisible(true);
 }
 public void init(){
  con=this.getContentPane();
  con.setLayout(new BorderLayout());
  jp1.add(jbt1);
  jp1.add(jbt2);
  con.add("Center",jp1);
 }
 public void start(){
  jbt1.addActionListener(this);
  jbt2.addActionListener(this);
  jbt3.addActionListener(this);
  jbt4.addActionListener(this);
  jbt5.addMouseListener(this);
  jbt6.addActionListener(this);
  jbt7.addActionListener(this);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==jbt7){
   jd3.setVisible(false);
  }
  if(e.getSource()==jbt5){
   jd3.setVisible(true);
   jd3.setLayout(new BorderLayout());
   jd3.setSize(350, 200);
  }
  if(e.getSource()==jbt3){
   jd1.setVisible(false);
   String author1 = jtf1.getText();
   String title2 = jtf2.getText();
   String pass3 = jtf3.getText();
   String content4 = jta1.getText();
   wr=new write(author1, title2, pass3, content4);
   vc.addElement(wr);
   
   jtf1.setText("");
   jtf2.setText("");
   jtf3.setText("");
   jta1.setText("");
  }
  if(e.getSource()==jbt6){
   jd2.setVisible(false);
   List.removeAll();
  }
  if(e.getSource()==jbt4){
   jd1.setVisible(false);
   jtf1.setText("");
   jtf2.setText("");
   jtf3.setText("");
   jta1.setText("");
  }
  if(e.getSource()==jbt1){
   jd2.setVisible(true);
   jd2.setLayout(new BorderLayout());
   jd2.setSize(450, 400);
   jd2.setLocation(700,200);
   jd2.add("North",jl4);
   jd2.add("Center",List);
   jp7.setLayout(new FlowLayout(FlowLayout.RIGHT));
   jp7.add(jbt5);
   jp7.add(jbt6);
   jd2.add("South",jp7);
   
   Font font =new Font("SansSerif",Font.BOLD,20);
   List.setFont(font);
   for(int i=0;i<vc.size();++i){
    int num=i+1;
    wr = (write)vc.elementAt(i);
    List.add(num+":"+wr.gettitle()+":"+wr.getauthor());
   }
  }
  if(e.getSource()==jbt2){
   jd1.setVisible(true);
   jd1.setSize(450, 400);
   jd1.setLocation(200,200);
   jd1.setLayout(new BorderLayout());
   
   jp2.setLayout(new BorderLayout());
   jp2.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED), "기본정보"));
   
   jp3.setLayout(new GridLayout(3,1,5,5));
   jp3.add(jl1);
   jp3.add(jl2);
   jp3.add(jl3);
   jp2.add("West",jp3);
   jp4.setLayout(new GridLayout(3,1,5,5));
   jp4.add(jtf1);
   jp4.add(jtf2);
   jtf3.setEchoChar('*');
   jp4.add(jtf3);
   jp2.add("Center",jp4);
   jd1.add("North",jp2);
   
   jp5.setLayout(new BorderLayout());
   jp5.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED), "내용작성"));
   jp5.add(jta1);
   jd1.add("Center",jp5);
   
   jp6.setLayout(new FlowLayout(FlowLayout.RIGHT));
   jp6.add(jbt3);
   jp6.add(jbt4);
   jd1.add("South",jp6);

   jbt3.setBorder(new BevelBorder(BevelBorder.RAISED));
   jbt4.setBorder(new BevelBorder(BevelBorder.RAISED));
  }
  
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==jbt5){
   int index=List.getSelectedIndex();
   jd3.setVisible(true);
   jd3.setSize(300, 200);
   jd3.setLocation(400,200);
   jd3.setLayout(new BorderLayout());
   jp8.setLayout(new GridLayout(2,1,5,5));
   wr = (write)vc.elementAt(index);
   jl7.setText("제목 : "+wr.gettitle());
   jl8.setText("작성자 : "+wr.getauthor());
   jp8.add(jl7);
   jp8.add(jl8);
   jd3.add("North",jp8);
   jta2.setText(wr.getcontent());
   jd3.add("Center",jta2);
   
   jd3.add("South",jbt7);
  }
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
}
public class Ex02 {
 public static void main(String  [] ar){
  Ex2 ex=new Ex2();
 }
}

'JAVA > 소스' 카테고리의 다른 글

Hello Java  (0) 2014.01.13
열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03
열혈강의 자바 22장 과제 1  (1) 2012.01.11
자바 21장 과제 2  (0) 2011.12.15
자바 문자열 숫자로 바꾸는 방법  (0) 2011.12.12

열혈강의 자바 22장 과제 1

JAVA/소스 2012. 1. 11. 02:54

//미완성... 수정 부분... 짱나서 여기까지...
import java.awt.*;
import java.awt.List;
import java.util.*;
import java.awt.event.*;

class F extends Frame implements ActionListener,MouseListener{
 
 private Button bt1 =new Button("글쓰기");
 private Button bt2 =new Button("새로고침");
 private Button bt3 =new Button("수정");
 private Button bt4 =new Button("삭제");
 private Button bt5 =new Button("종료");
 private Button bt6 =new Button("찾아보기");
 private Button bt7 =new Button("등록");
 private Button bt8 =new Button("취소");

 private Label lb1=new Label("글목록!",Label.LEFT);
 private Label lb2=new Label("글보기!",Label.LEFT);
 private Label lb3=new Label("글쓰기",Label.CENTER);
 private Label lb4=new Label("글제목 : ",Label.RIGHT);
 private Label lb5=new Label("작성자 : ",Label.RIGHT);
 private Label lb6=new Label("첨부파일 : ",Label.RIGHT);
  
 
 private List li=new List();
 
 private TextArea ta1=new TextArea();
 private TextArea ta2=new TextArea();

 private TextField tf1=new TextField();
 private TextField tf2=new TextField();
 private TextField tf3=new TextField();

 private FlowLayout fl1=new FlowLayout();
 private BorderLayout bl1=new BorderLayout(5,5);
 private GridLayout gl1=new GridLayout(1,2,5,5);
 private GridLayout gl2=new GridLayout(1,2,5,5);
 private GridLayout gl3=new GridLayout(1,2,5,5);
 private GridLayout gl4=new GridLayout(1,2,5,5);
 private GridLayout gl5=new GridLayout(1,3,5,5);
 
 private Panel p1=new Panel();//글목록! 글보기!
 private Panel p2=new Panel();//ta1 li
 private Panel p3=new Panel();//p4,p5
 private Panel p4=new Panel();//글쓰기 새로고침
 private Panel p5=new Panel();//수정 삭제 종료
 
 //////Dialog 변수 write함수 쪽에서 다씀  위쪽에 버튼이랑 텍스트 몇개 씀
 final Dialog dlg =new Dialog(this,"글쓰기",true);
 private BorderLayout br2=new BorderLayout();
 private BorderLayout br3=new BorderLayout();
 
 private GridLayout gl6 =new GridLayout(3,1);
 private GridLayout gl7 =new GridLayout(3,1);
 private GridLayout gl8 =new GridLayout(1,2);
 private FlowLayout fl2 =new FlowLayout(FlowLayout.RIGHT);
 
 private Panel p6=new Panel();
 private Panel p7=new Panel();
 private Panel p8=new Panel();
 private Panel p9=new Panel();
 private Panel p10=new Panel();
 
 //등록 할때 쓸거
 private String title;
 private String author;
 private String file;
 private int number=0;
 private ArrayList li1=new ArrayList();
 public F(){
  super("게시판!");
  
  this.init();
  this.start();
  
  this.setSize(500,400);
  
  Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frm=this.getSize();
  
  int x=(int)(screen.width/2-frm.width/2);
  int y=(int)(screen.height/2-frm.height/2);
  
  this.setLocation(x,y);
  this.setVisible(true);
  
 }
 public void init(){//초기화면 창
  this.setLayout(bl1);
  p1.setLayout(gl1);
  p1.add(lb1);
  p1.add(lb2);
  this.add("North",p1);
  p2.setLayout(gl2);
  p2.add(li);
  ta1.setEditable(false); //수정못함 설정
  ta1.setBackground(new Color(144,122,44));
  p2.add(ta1);
  this.add("Center",p2);
  p3.setLayout(gl3);
  p4.setLayout(gl4);
  p4.add(bt1);
  p4.add(bt2);
  p3.add(p4);
  p5.setLayout(gl5);
  p5.add(bt3);
  p5.add(bt4);
  p5.add(bt5);
  p3.add(p5);
  this.add("South",p3);

 }
 public void start(){
  bt7.addActionListener(this);
  bt1.addActionListener(this);
  bt6.addActionListener(this);
  bt2.addActionListener(this);
  bt4.addActionListener(this);//삭제
  bt3.addMouseListener(this);//수정
  li.addMouseListener(this);
  //취소,종료
  bt8.addActionListener(this);
  bt5.addActionListener(this);
  
  //게시판 x종료
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
   }
  });
 }
 public void write(){ //글쓰기 창
  
  dlg.setSize(300,500);
  dlg.setLayout(br2);
  p6.setLayout(br3);
  
  p7.setLayout(gl6);
  p7.add(lb4);
  p7.add(lb5);
  p7.add(lb6);
  p6.add("West",p7);
  
  p8.setLayout(gl7);
  p8.add(tf1);
  p8.add(tf2);
  p9.setLayout(gl8);
  tf3.setEditable(false); //수정 못함 설정
  p9.add(tf3);
  p9.add(bt6);
  p8.add(p9);
  p6.add("Center",p8);
  p6.add("North",lb3);
 
  
  p10.setLayout(fl2);
  p10.add(bt7);
  p10.add(bt8);
  
  
  dlg.add("South",p10);
  dlg.add("Center",ta2);
  dlg.add("North",p6);
  
  //글쓰기 x 종료
  dlg.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e){
    dlg.setVisible(false);
   }
  });
  
  
  
  Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frm=this.getSize();
  
  int x=(int)(screen.width/2-frm.width/4);
  int y=(int)(screen.height/2-frm.height/2);
  dlg.setLocation(x,y);
  dlg.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==bt1){
   write();
  }
  else if(e.getSource()==bt5){
   System.exit(0);
  }
  else if(e.getSource()==bt6){
   FileDialog fdlg=new FileDialog(this,"찾아보기",FileDialog.LOAD);
   fdlg.setVisible(true);
   String dir=fdlg.getDirectory();
   tf3.setText(dir);
  }
  else if(e.getSource()==bt8){
     dlg.setVisible(false);
  }
  else if(e.getSource()==bt7){
   title=tf1.getText();
   author=tf2.getText();
   file=tf3.getName();
   li.add(number+1+" : "+title+" ("+author+")");
   li1.add(lb4.getText()+title+"\n\n"+lb5.getText()+author+"\n\n"+lb6.getText()+file+"\n\n"+"내용 : \n\n"+ta2.getText());
   String str=(String)li1.get(number);
   ta1.setText(str);
   
   
   
   //글쓰기창 공백으로 초기화
   tf1.setText("");
   tf2.setText("");
   tf3.setText("");
   ta2.setText("");
   
   number++;
   dlg.setVisible(false);
  }
  else if(e.getSource()==bt4){

   int del=li.getSelectedIndex();
   li.remove(del);
   li1.remove(del);
   number--;
  }
  else if(e.getSource()==bt2){
   List t_li =new List();
   ArrayList t_li1=new ArrayList();
   for(int i=0;i<number;++i){
   
    t_li.add(i+1+li.getItem(i));
    t_li1.add(li1.get(i));
   }
   li.removeAll(); //리스트 항목 모두 삭제
   li1.clear(); //어레이 리스트 항목 모두 삭제
   
   for(int i=0;i<number;++i){
    li.add(t_li.getItem(i));
    li1.add(t_li1.get(i));
   }
   System.out.println(number);
  }
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  if(e.getSource()==bt3){
   if(bt3.getLabel()=="수정"){
    bt3.setLabel("수정중");

    ta1.setBackground(new Color(255,255,255));
    ta1.setEditable(true);
   }
   else if(bt3.getLabel()=="수정중"){
    bt3.setLabel("수정"); 

    ta1.setBackground(new Color(144,122,44));
    int index=li.getSelectedIndex();
    System.out.println(index);
    li1.set(index,ta1.getText());
    ta1.setEditable(false);
   }
  }
  if(e.getSource()==li){
   int index=li.getSelectedIndex();
   String str =(String)li1.get(index);
   ta1.setText(str);
  }
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 
}


public class Ex_01 {
 public static void main(String [] ar){
  F ex=new F();
 }
}


 

'JAVA > 소스' 카테고리의 다른 글

열혈강의 자바 24장 과제 1 [미완성]  (2) 2012.02.03
열혈강의 자바 22장 과제 2  (3) 2012.01.11
자바 21장 과제 2  (0) 2011.12.15
자바 문자열 숫자로 바꾸는 방법  (0) 2011.12.12
열혈강의 자바 14장 과제1  (0) 2011.10.30

자바 21장 과제 2

JAVA/소스 2011. 12. 15. 01:35


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.applet.*;
class Draw implements Serializable{
 private int x,y,x1,y1;
 private int dist;
 public int getDist() {
  return dist;
 }

 public void setDist(int dist) {
  this.dist = dist;
 }

 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }

 public int getX1() {
  return x1;
 }

 public void setX1(int x1) {
  this.x1 = x1;
 }

 public int getY1() {
  return y1;
 }

 public void setY1(int y1) {
  this.y1 = y1;
 }
}
public class Ex1 extends Applet implements ItemListener ,MouseListener,MouseMotionListener{
 private MenuBar mb=new MenuBar();
 private Menu draw =new Menu("DRAW");
 private CheckboxMenuItem pen=new CheckboxMenuItem("PEN",true);
 private CheckboxMenuItem line=new CheckboxMenuItem("LINE");
 private CheckboxMenuItem oval=new CheckboxMenuItem("OVAL");
 private CheckboxMenuItem rect=new CheckboxMenuItem("RECT");
 
 
 private int x,y,x1,y1;
 private Vector vc =new Vector();
 
 
 public void init(){
  draw.add(pen);
  draw.add(line);
  draw.add(oval);
  draw.add(rect);
  mb.add(draw);
 // this.setMenuBar(mb);
  
 }
 public void start(){
  
  pen.addItemListener(this);
  line.addItemListener(this);
  oval.addItemListener(this);
  rect.addItemListener(this);
  this.addMouseMotionListener(this);
  this.addMouseListener(this);
 }
 //업데이트 없에면 좀 다름 근대 버퍼링이 생김
 public void update(Graphics g){
  paint(g);
 }
 public void paint(Graphics g){

  for(int i=0;i<vc.size();++i){
   Draw d=(Draw)vc.elementAt(i);
   
   if(d.getDist()==1){
    g.drawLine(d.getX(), d.getY(), d.getX1(), d.getY1());
   }
   else if(d.getDist()==2){
    g.drawOval(d .getX(), d.getY(), d.getX1()-d.getX(), d.getY1()-d.getY());
   }
   else if(d.getDist()==3){
    g.drawRect(d.getX(), d.getY(), d.getX1()-d.getX(), d.getY1()-d.getY());
    
   }
  }
  
  if(line.getState()==true){
   g.drawLine(x, y, x1, y1);
  }
  else if(oval.getState()==true){
   g.drawOval(x, y, x1-x, y1-y);
  }
  else if(rect.getState()==true){
   g.drawRect(x, y, x1-x, y1-y);
  }
 }
 public void itemStateChanged(ItemEvent e) {
  pen.setState(false);
  line.setState(false);
  oval.setState(false);
  rect.setState(false);
  
  CheckboxMenuItem imsi=(CheckboxMenuItem)e.getSource();
  
  imsi.setState(true);
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  x=e.getX();
  y=e.getY();
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  x1=e.getX();
  y1=e.getY();
  this.repaint();   
 
  if(pen.getState()!=true){
   int dist=0;
   if(line.getState()==true)dist=1;
   else if(oval.getState()==true)dist=2;
   else if(rect.getState()==true)dist=3;
   Draw d=new Draw();
   d.setDist(dist);
   d.setX(x);
   d.setY(y);
   d.setX1(x1);
   d.setY1(y1);
   vc.add(d);
  }
 }
 @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 mouseDragged(MouseEvent e) {
  // TODO Auto-generated method stub
  x1=e.getX();
  y1=e.getY();
  this.repaint();
  if(pen.getState()){
   Draw d=new Draw();
   d.setDist(1);
   d.setX(x);
   d.setY(y);
   d.setX1(x1);
   d.setY1(y1);
   vc.add(d);
   x=x1;
   y=y1;
  }
 }
 @Override
 public void mouseMoved(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }

}

 

 

 

 

'JAVA > 소스' 카테고리의 다른 글

열혈강의 자바 22장 과제 2  (3) 2012.01.11
열혈강의 자바 22장 과제 1  (1) 2012.01.11
자바 문자열 숫자로 바꾸는 방법  (0) 2011.12.12
열혈강의 자바 14장 과제1  (0) 2011.10.30
열혈강의 자바 13장 과제2  (0) 2011.10.27