안드로이드 개발 질문/답변
(글 수 358)
ANDROID 학원에서 3개월 배우고 직접 프로그램 짜는 연습 중인데요
기본 생각은 이렇게 만들었습니다.
한 클래스에 모든 변수와
소켓 .
서버 소켓
스트림 을
몰아넣고
get,set 을 만든 다음에
싱글턴으로 만들어서 각 클래스 마다 선언된 변수나 서버소켓을 성언된 클래스로 접근 하여
값을 저장하고 빼내고 하는데요.
어떤 자바 책을 보든 android 책을 봐도
맴버 필드에 변수 선언을 하더군요.
저처럼 클래스로 그냥 다 몰아 넣어 버리는게 아니더군요.
그래서 고민입니다 .
쓰래기 소스라는건 알지만. 그래도 지적좀 해주시면 안될까요????
package ex1;
import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
public class Canvers_say extends Canvas {
//[*]------------------------------------------------------------[*] //[*] 싱글턴 //[*]------------------------------------------------------------[*] private Canvers_say (){}; private static Canvers_say canvers_say; public static Canvers_say getCanvers_say(){ if(canvers_say == null) canvers_say = new Canvers_say(); return canvers_say; } //[*]------------------------------------------------------------[*] //[*] 구조체 가져오기 //[*]------------------------------------------------------------[*] Type_say type_say; public void init_Type(){ type_say = Type_say.get_say_type(); } //[*]------------------------------------------------------------[*] //[*] 메인 주소 받아 오기. //[*]------------------------------------------------------------[*] Main_say main_say; public void setMain_say(Main_say main_say) { this.main_say = main_say; } //[*]------------------------------------------------------------[*] //[*] 그림 그리는 곳 //[*]------------------------------------------------------------[*] @Override public void paint(Graphics g) { super.paint(g); Dimension d = getSize(); type_say.setBuff_img(createImage(d.width,d.height)); type_say.setG_buff(type_say.buff_img.getGraphics()); type_say.g_buff.setColor(Color.black); type_say.g_buff.fillOval(getWidth()/2-200, getHeight()/2-200, 400, 400); type_say.g_buff.setColor(Color.green); type_say.g_buff.fillOval(getWidth()/2-160, getHeight()/2-160, 320, 320); type_say.g_buff.setColor(Color.BLUE); type_say.g_buff.fillOval(getWidth()/2-80, getHeight()/2-80, 160, 160); type_say.g_buff.setColor(Color.yellow); type_say.g_buff.fillOval(getWidth()/2-40, getHeight()/2-40, 80, 80); type_say.g_buff.setColor(Color.red); type_say.g_buff.fillOval(getWidth()/2-20, getHeight()/2-20, 40, 40); if (type_say.isImg_flag()){ type_say.g_buff.setColor(Color.white); type_say.g_buff.fillOval(type_say.getX(), type_say.getY(), 20, 20); } g.drawImage(type_say.buff_img, 0, 0, this); }
@Override public void update(Graphics g) { super.update(g); paint(g); } }
package ex1;
public class Input_Thread extends Thread {
private Input_Thread(){}; private static Input_Thread input_Thread; public static Input_Thread Get_Input_thread(){ if(input_Thread == null) input_Thread = new Input_Thread(); return input_Thread; } Type_say type_say; public void init_Type(){ type_say = Type_say.get_say_type(); } Main_say main_say; public void setMain_say(Main_say main_say) { this.main_say = main_say; } @Override public void run() { super.run(); try { type_say.setInput_int(-1); type_say.setInput_buff(new byte[1208]); while(true){ while((type_say.input_int = type_say.inputStream.read(type_say.input_buff)) != -1){ type_say.setInput_buff_String(new String(type_say.input_buff,0,type_say.input_int)); System.out.println("dsafdsaf"); main_say.area_input.setText(type_say.getInput_buff_String()); } } } catch (Exception e) { e.printStackTrace(); } } }
package ex1;
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea;
public class Main_say extends JFrame { Socket_say socket_say; Type_say type_say; Canvers_say canvers_say; Input_Thread input_Thread; //[*]------------------------------------------------------------[*] //[*] 화면 을 구성하는 아이템 //[*]------------------------------------------------------------[*] JPanel pannel_1, pannel_2; JTextArea area_input, area_port; JButton button_server; public Main_say(){ //[*]------------------------------------------------------------[*] //[*] 클래스 생성 //[*]------------------------------------------------------------[*] type_say = Type_say.get_say_type(); //구조체 socket_say = Socket_say.get_Socket_say(); //소켓 socket_say.init_Type(); socket_say.setMain_say(this); canvers_say = Canvers_say.getCanvers_say(); //그림 canvers_say.init_Type(); canvers_say.setMain_say(this); input_Thread = Input_Thread.Get_Input_thread(); input_Thread.init_Type(); input_Thread.setMain_say(this); //[*]------------------------------------------------------------[*] //[*] 화면 구성 //[*]------------------------------------------------------------[*] pannel_1 = new JPanel(new BorderLayout()); pannel_2 = new JPanel(new GridLayout(0,2)); pannel_2.add(area_port = new JTextArea("8888")); pannel_2.add(button_server = new JButton("서버열기")); pannel_1.add(area_input = new JTextArea(),BorderLayout.NORTH); pannel_1.add(pannel_2, BorderLayout.SOUTH); pannel_1.add(canvers_say, BorderLayout.CENTER); this.add(pannel_1); setSize(800,500); setVisible(true); addWindowListener(windowAdapter);
button_server.addActionListener(button_Click); // canvers_say.addMouseMotionListener(mouseMotion); } // private MouseMotionListener mouseMotion = new MouseMotionAdapter() { // // @Override // public void mouseDragged(MouseEvent e) { // super.mouseDragged(e); // type_say.setImg_flag(true); // type_say.setX(e.getX()); // type_say.setY(e.getY()); // canvers_say.repaint(); // // } // // @Override // public void mouseMoved(MouseEvent e) { // super.mouseMoved(e); // type_say.setImg_flag(true); // type_say.setX(e.getX()); // type_say.setY(e.getY()); // canvers_say.repaint(); // // } // // // // }; private ActionListener button_Click = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { type_say.setPort(area_port.getText()); if (socket_say.init_soket()){ area_port.setText("서버가 열렸습니다."); type_say.setSTATE_SAY(Type_say.SOKER_ACCEPT_SAY); thread_start(); } } };
public void thread_start(){ switch (type_say.getSTATE_SAY()) { case Type_say.SOKER_ACCEPT_SAY : //서버에 기기가 들어 왔을때 type_say.setSoket_thread(new Thread(socket_say)); type_say.soket_thread.start(); break;
case Type_say.SOKER_INPUT_TH: System.out.println("dsafdsa"); type_say.setInput_thread(new Thread(input_Thread)); type_say.input_thread.start(); break; } } //[*]------------------------------------------------------------[*] //[*] 창 닫을때 동작 //[*]------------------------------------------------------------[*] private WindowAdapter windowAdapter = new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); try { if (type_say.getInputStream() !=null){ type_say.getInputStream().close(); } if (type_say.getOutputStream() !=null){ type_say.getOutputStream().close(); } if (type_say.getServerSocket() !=null){ type_say.getServerSocket().close(); } if (type_say.getSocket() !=null){ type_say.getSocket().close(); } } catch (Exception e2) { // TODO: handle exception } System.exit(0); } }; public static void main(String[] args) { new Main_say(); }
}
package ex1;
import java.net.ServerSocket;
public class Socket_say extends Thread{ private static Socket_say socket_say; private Socket_say(){}; public static Socket_say get_Socket_say(){ if(socket_say == null) socket_say = new Socket_say(); return socket_say; } Type_say type_say; public void init_Type(){ type_say = Type_say.get_say_type(); } Main_say main_say; public void setMain_say(Main_say main_say) { this.main_say = main_say; }
public boolean init_soket() { try { type_say.setServerSocket(new ServerSocket (Integer.parseInt(type_say.getPort().trim()))); return true; } catch (Exception e) { e.printStackTrace(); return false; } } @Override public void run() { super.run(); try { while(true){ type_say.socket = type_say.serverSocket.accept(); type_say.inputStream = type_say.socket.getInputStream(); type_say.outputStream = type_say.getOutputStream(); type_say.setSTATE_SAY(Type_say.SOKER_INPUT_TH); main_say.area_input.setText(type_say.serverSocket.getInetAddress().toString()); main_say.thread_start(); } } catch (Exception e) { e.printStackTrace(); } }
package ex1;
import java.awt.Graphics; import java.awt.Image; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket;
public class Type_say { //[*]------------------------------------------------------------[*] //[*] 싱글턴 //[*]------------------------------------------------------------[*] private Type_say(){}; private static Type_say type_say; public static Type_say get_say_type(){ if(type_say == null) type_say = new Type_say(); return type_say; }
//[*]------------------------------------------------------------[*] //[*] 통신 관련 //[*]------------------------------------------------------------[*] ServerSocket serverSocket ; Socket socket ; InputStream inputStream ; OutputStream outputStream ; String port ; boolean socket_flag ; //[*]------------------------------------------------------------[*] //[*] input 관련 //[*]------------------------------------------------------------[*] int input_int; byte[] input_buff; String input_buff_String; //[*]------------------------------------------------------------[*] //[*] 그리기 관련 //[*]------------------------------------------------------------[*]
int x, y; Image buff_img; Graphics g_buff;
boolean img_flag;
//[*]------------------------------------------------------------[*] //[*] 상태를 기억하는 //[*]------------------------------------------------------------[*] int STATE_SAY ; final static int SOKER_ACCEPT_SAY = 1; //서버에 기기가 들어 왔을때. final static int SOKER_INPUT_TH = 2;
//[*]------------------------------------------------------------[*] //[*] 쓰래드 //[*]------------------------------------------------------------[*] Thread soket_thread; Thread input_thread; //[*]------------------------------------------------------------[*] //[*] Get / Set //[*]------------------------------------------------------------[*] public int getInput_int() { return input_int; }
public void setInput_int(int input_int) { this.input_int = input_int; }
public byte[] getInput_buff() { return input_buff; }
public void setInput_buff(byte[] input_buff) { this.input_buff = input_buff; }
public String getInput_buff_String() { return input_buff_String; }
public void setInput_buff_String(String input_buff_String) { this.input_buff_String = input_buff_String; } public Thread getInput_thread() { return input_thread; }
public void setInput_thread(Thread input_thread) { this.input_thread = input_thread; }
public boolean isSocket_flag() { return socket_flag; }
public void setSocket_flag(boolean socket_flag) { this.socket_flag = socket_flag; }
public Graphics getG_buff() { return g_buff; }
public void setG_buff(Graphics g_buff) { this.g_buff = g_buff; } public boolean isImg_flag() { return img_flag; }
public void setImg_flag(boolean img_flag) { this.img_flag = img_flag; } 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 Image getBuff_img() { return buff_img; }
public void setBuff_img(Image buff_img) { this.buff_img = buff_img; } public int getSTATE_SAY() { return STATE_SAY; }
public void setSTATE_SAY(int sTATE_SAY) { STATE_SAY = sTATE_SAY; } public Thread getSoket_thread() { return soket_thread; }
public void setSoket_thread(Thread soket_thread) { this.soket_thread = soket_thread; }
public ServerSocket getServerSocket() { return serverSocket; }
public void setServerSocket(ServerSocket serverSocket) { this.serverSocket = serverSocket; }
public Socket getSocket() { return socket; }
public void setSocket(Socket socket) { this.socket = socket; }
public InputStream getInputStream() { return inputStream; }
public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; }
public OutputStream getOutputStream() { return outputStream; }
public void setOutputStream(OutputStream outputStream) { this.outputStream = outputStream; }
public String getPort() { return port; }
public void setPort(String port) { this.port = port; } }
이렇게 클래스로 다 몰아넣고
다른클래스에서 불러오는게 좋은 방법이긴 한걸까요??
안드로이드에서도 비슷하게 만들었는데요,
장점은 액티비티가 바뀌어도 값을 던질 필요 없이 타입 클래스만 받아오면 변수를 그냥 써도 되더라구요.