public void connect() throws Exception { ServerSocket ss = new ServerSocket(port); while (true) { Socket s = ss.accept(); System.out.println("Socket Accepted!"); InputThread it = new InputThread(s, srf); it.start(); } }
/** Constructs the GUI */ public Piano(String userID, int userImgNum) { this.userID = userID; this.userImgNum = userImgNum; // ----------- 소켓 생성 ------------ try { sock = new Socket(SERVER_IP, 10001); pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream())); br = new BufferedReader(new InputStreamReader(sock.getInputStream())); pw.println(this.userImgNum + this.userID); pw.flush(); } catch (Exception ex) { System.out.println("서버와 접속시 오류가 발생하였습니다."); System.out.println(ex); System.exit(1); } //// 현재 클라이언트 수 파악 try { String cnt = br.readLine(); cntClient = Integer.parseInt(cnt); if (cntClient == -1) { JOptionPane.showMessageDialog(null, "게임 도중에 접속할 수 없습니다."); System.exit(0); } else if (cntClient >= 4) { JOptionPane.showMessageDialog(null, "방이 꽉 찼습니다."); System.exit(0); } else if (cntClient == 0) { // 내가 첫 입장이면 this.bMaster = true; pw.println(FLAG_MASTER); pw.flush(); } String bExist = br.readLine(); if (bExist.equals("false")) { JOptionPane.showMessageDialog(null, "동일한 ID가 존재합니다."); System.exit(0); } } catch (IOException e) { e.printStackTrace(); } // ----------- 채팅 스레드 생성----------- InputThread inputThread = new InputThread(sock, br); inputThread.start(); // ------------ Create GUI ----------- frame = new JFrame("Piano GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create the mainPanel Container mainPanel = frame.getContentPane(); // mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.setLayout(new GridLayout(3, 1, 20, 0)); mainPanel.setForeground(Color.WHITE); mainPanel.setBackground(Color.BLACK); // mainPanel.add(Box.createRigidArea(new Dimension(0,10))); // ---- Instrument and tempo panel // --------------------------------------------------------------- ImagePanel iTpanel = new ImagePanel("images/background1.png"); // iTpanel = new ImagePanel("images/background1.png"); iTpanel.setLayout(new BoxLayout(iTpanel, BoxLayout.X_AXIS)); iTpanel.setForeground(Color.WHITE); iTpanel.setBackground(Color.GREEN); iTpanel.add(Box.createRigidArea(new Dimension(20, 0))); // --- User1 --- userPane1 = new JPanel(); userPane1.setLayout(new BoxLayout(userPane1, BoxLayout.Y_AXIS)); userPane1.setBackground(Color.GRAY); // img + label icon1 = new ImageIcon("images/user.jpg"); UserImage1 = new JLabel(icon1); userPane1.add(UserImage1); // label pane labelPane1 = new JPanel(); labelPane1.setLayout(new BoxLayout(labelPane1, BoxLayout.X_AXIS)); labelPane1.setBackground(Color.GRAY); labelPane1.setMaximumSize(new Dimension(UserImage1.getPreferredSize().width, 20)); UserLabel1 = new JLabel(" "); UserLabel1.setForeground(Color.WHITE); labelPane1.add(UserLabel1); labelPane1.add(Box.createRigidArea(new Dimension(10, 0))); ScoreLabel1 = new JLabel(" "); ScoreLabel1.setForeground(Color.YELLOW); labelPane1.add(ScoreLabel1); labelPane1.setAlignmentX(Component.LEFT_ALIGNMENT); userPane1.add(labelPane1); iTpanel.add(userPane1); iTpanel.add(Box.createRigidArea(new Dimension(10, 0))); // --- User2 --- userPane2 = new JPanel(); userPane2.setLayout(new BoxLayout(userPane2, BoxLayout.Y_AXIS)); userPane2.setBackground(Color.GRAY); // img + label icon2 = new ImageIcon("images/user.jpg"); UserImage2 = new JLabel(icon2); userPane2.add(UserImage2); // label pane labelPane2 = new JPanel(); labelPane2.setLayout(new BoxLayout(labelPane2, BoxLayout.X_AXIS)); labelPane2.setBackground(Color.GRAY); labelPane2.setMaximumSize(new Dimension(UserImage2.getPreferredSize().width, 20)); UserLabel2 = new JLabel(" "); UserLabel2.setForeground(Color.WHITE); labelPane2.add(UserLabel2); labelPane2.add(Box.createRigidArea(new Dimension(10, 0))); ScoreLabel2 = new JLabel(" "); ScoreLabel2.setForeground(Color.YELLOW); labelPane2.add(ScoreLabel2); labelPane2.setAlignmentX(Component.LEFT_ALIGNMENT); userPane2.add(labelPane2); iTpanel.add(userPane2); iTpanel.add(Box.createRigidArea(new Dimension(50, 0))); // 정답 및 힌트 JPanel at = new JPanel(); at.setLayout(new BoxLayout(at, BoxLayout.Y_AXIS)); // at.setLayout(new GridLayout(2,1)); answer = new JLabel("노래 정답", JLabel.CENTER); Border border = BorderFactory.createLineBorder(Color.WHITE, 2); answer.setBorder(border); answer.setForeground(Color.WHITE); answer.setBackground(Color.BLACK); answer.setOpaque(true); answer.setSize(5, 5); at.add(answer); time = new JLabel("시간", JLabel.CENTER); time.setBorder(border); time.setForeground(Color.WHITE); time.setBackground(Color.BLACK); time.setOpaque(true); time.setSize(10, 5); at.add(time); // ----------- 시간 스레드 생성----------- // tt = new TimeThread(time,sock,pw); iTpanel.add(at); iTpanel.add(Box.createRigidArea(new Dimension(80, 0))); /*JLabel time = new JLabel("시간",JLabel.CENTER); time.setForeground(Color.WHITE); time.setBackground(Color.BLACK); time.setOpaque(true); iTpanel.add(time);*/ // ImageIcon hintImg = new ImageIcon("images/hint01.jpg"); hintImg = new ImageIcon("images/hint0.png"); hint = new JLabel(hintImg); hint.setForeground(Color.BLACK); iTpanel.add(hint); iTpanel.add(Box.createRigidArea(new Dimension(300, 0))); // Add iTpanel to mainPanel mainPanel.add(iTpanel); // -------- piano keys panel // ----------------------------------------------------------------------------------- JLayeredPane pianoKeyPanel = makeKeys(); mainPanel.add(pianoKeyPanel); // ---- Buttom panel // ---------------------------------------------------------------------------------- ImagePanel iTpane2 = new ImagePanel("images/background3.png"); iTpane2.setLayout(new BoxLayout(iTpane2, BoxLayout.X_AXIS)); iTpane2.setForeground(Color.WHITE); iTpane2.setBackground(Color.BLACK); iTpane2.add(Box.createRigidArea(new Dimension(100, 0))); // Instrument List JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); InstList = new List(4, false); InstList.add("피아노"); InstList.add("기타"); InstList.add("비브라폰"); InstList.add("바이올린"); InstList.addActionListener(this); controlPanel.setOpaque(false); controlPanel.add(InstList); iTpane2.add(controlPanel); InstList.select(0); // ---- chatting pane ---- JPanel chatPane = new JPanel(); chatPane.setLayout(new BorderLayout(0, 5)); chatPane.setBackground(Color.BLACK); // chatting box chatBox = new JTextArea(); chatBox.setFont(new Font("Ariel", Font.PLAIN, 14)); JScrollPane scrollPane = new JScrollPane(chatBox); chatPane.add(scrollPane, BorderLayout.CENTER); // ---chat write box (input)---- JPanel chatInputPane = new JPanel(); chatInputPane.setLayout(new BoxLayout(chatInputPane, BoxLayout.X_AXIS)); // chatInputText 채팅 입력 창 chatInputText = new JTextField(); chatInputText.setFont(new Font("Ariel", Font.PLAIN, 14)); chatInputText.addActionListener(this); chatInputPane.add(chatInputText); // Send Button chatBtn = new JButton("send"); chatBtn.addActionListener(this); chatBtn.setName("chatBtn"); chatInputPane.add(chatBtn); chatPane.add(chatInputPane, BorderLayout.SOUTH); // attach iTpane2.add(chatPane); iTpane2.add(Box.createRigidArea(new Dimension(10, 0))); // start button btStart = new JButton("start"); btStart.addActionListener(this); btStart.setName("btStart"); iTpane2.add(btStart); iTpane2.add(Box.createRigidArea(new Dimension(10, 0))); btStart.setEnabled(false); // User userPane3 = new JPanel(); userPane3.setLayout(new BoxLayout(userPane3, BoxLayout.Y_AXIS)); userPane3.setBackground(Color.GRAY); icon3 = new ImageIcon("images/user.jpg"); UserImage3 = new JLabel(icon3); userPane3.add(UserImage3); // label pane labelPane3 = new JPanel(); labelPane3.setLayout(new BoxLayout(labelPane3, BoxLayout.X_AXIS)); labelPane3.setBackground(Color.GRAY); labelPane3.setMaximumSize(new Dimension(UserImage3.getPreferredSize().width, 20)); UserLabel3 = new JLabel(" "); UserLabel3.setForeground(Color.WHITE); labelPane3.add(UserLabel3); labelPane3.add(Box.createRigidArea(new Dimension(10, 0))); ScoreLabel3 = new JLabel(" "); ScoreLabel3.setForeground(Color.YELLOW); labelPane3.add(ScoreLabel3); labelPane3.setAlignmentX(Component.LEFT_ALIGNMENT); userPane3.add(labelPane3); iTpane2.add(userPane3); iTpane2.add(Box.createRigidArea(new Dimension(10, 0))); userPane4 = new JPanel(); userPane4.setLayout(new BoxLayout(userPane4, BoxLayout.Y_AXIS)); userPane4.setBackground(Color.GRAY); icon4 = new ImageIcon("images/user.jpg"); UserImage4 = new JLabel(icon4); userPane4.add(UserImage4); // label pane labelPane4 = new JPanel(); labelPane4.setLayout(new BoxLayout(labelPane4, BoxLayout.X_AXIS)); labelPane4.setBackground(Color.GRAY); labelPane4.setMaximumSize(new Dimension(UserImage4.getPreferredSize().width, 20)); UserLabel4 = new JLabel(" "); UserLabel4.setForeground(Color.WHITE); labelPane4.add(UserLabel4); labelPane4.add(Box.createRigidArea(new Dimension(10, 0))); ScoreLabel4 = new JLabel(" "); ScoreLabel4.setForeground(Color.YELLOW); labelPane4.add(ScoreLabel4); labelPane4.setAlignmentX(Component.LEFT_ALIGNMENT); userPane4.add(labelPane4); iTpane2.add(userPane4); iTpane2.add(Box.createRigidArea(new Dimension(20, 0))); // Add iTpanel to mainPanel mainPanel.add(iTpane2); // mainPanel.add(Box.createRigidArea(new Dimension(0,20))); // Show the window frame.setVisible(true); frame.setResizable(false); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation( (screenSize.width - frame.getWidth()) / 2 - 450, (screenSize.height - frame.getHeight()) / 2 - 290); frame.setSize(900, 580); } // 생성자 끝