import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class CreateRoom01 extends JFrame implements ActionListener{
private static final int BOLD = 0;
private JPanel p = new JPanel();
private JLabel jl = new JLabel("새로운 방 설정");
private JLabel j2 = new JLabel("방 제목");
private JLabel j3 = new JLabel("인원수");
private JLabel j4 = new JLabel("비밀번호");
private JTextField tf1 = new JTextField();
private JTextField tf2 = new JTextField();
private JTextField tf3 = new JTextField();
private JButton jbt1 = new JButton("완료");
private JButton jbt2 = new JButton("취소");
private JCheckBox cb = new JCheckBox("비공개");
public CreateRoom01(){
this.init();
super.setSize(500,350);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int xpos = (int)(screen.getWidth()/2 - this.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - this.getHeight()/2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
Container con = this.getContentPane();
con.add(p);
this.getClass();
p.setLayout(null);
p.add(jl);
p.add(j2);
p.add(j3);
p.add(j4);
jl.setBounds(120,10,250,20);
jl.setFont(new Font("",BOLD,20));
j2.setBounds(60,90,50,20);
j3.setBounds(60,130,50,20);
j4.setBounds(60,170,60,20);
p.add(tf1);
tf1.setBounds(120,90,250,20);
tf1.setFont(new Font("",BOLD,15));
p.add(tf2);
tf2.setBounds(120,130,50,20);
p.add(tf3);
tf3.setBounds(120,170,250,20);
tf3.setEnabled(false);
p.add(jbt1);
jbt1.setBounds(160,250,70,40);
p.add(jbt2);
jbt2.setBounds(250,250,70,40);
p.add(cb);
cb.setBounds(380,40,90,50);
jbt2.addActionListener(this);
jbt1.addActionListener(this);
cb.addActionListener(this);
}
public void newRoom(){
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== cb){
tf3.setEnabled(true);
}else if(e.getSource() == jbt2){
super.dispose();
}else if (e.getSource() == jbt1){
int x = JOptionPane.showConfirmDialog
(this, "새로운 방을 만드시겠습니까?","방만들기",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(x==1){
return;
}else if(x==0){
newRoom();
}
}
}
}
public class CreateRoom {
public static void main(String []args){
CreateRoom01 cr = new CreateRoom01();
}
}
//여기까지가 방만들기창.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.*;
import javax.swing.border.*;
class MyFrame extends JFrame implements ActionListener, Runnable {
String ipAddress;
final static int server_port = 12345;
Socket client = null;
ObjectOutputStream oos;
ObjectInputStream ois;
String usr_id;
ReceiveDataThread rt;
CanvasImage ci = new CanvasImage();
// 기본베이스
private BorderLayout base_bl = new BorderLayout();
private JPanel left_jp = new JPanel();
private JPanel right_jp = new JPanel();
// 그림판
private BorderLayout left_bl = new BorderLayout();
private BorderLayout picture_bl = new BorderLayout();
private GridLayout correctAnswer_gl = new GridLayout(6, 1);
private GridLayout pictureOption_gl = new GridLayout(2, 5);
private JPanel picture_jp = new JPanel();
private JPanel pictureOption_jp = new JPanel();
private JPanel pictureDrawing_jp = new JPanel();
private JPanel correctAnswer_jp = new JPanel();
private JTextField correctAnswer_jtf[] = new JTextField[5];
private JLabel time_jlb = new JLabel("60초", JLabel.CENTER);
private JButton pictureOption_jbt[][] = new JButton[2][5];
private TitledBorder tb[] = new TitledBorder[5];
// 접속자판
private BorderLayout information_bl = new BorderLayout();
private JPanel character_jp = new JPanel();
private JPanel message_jp = new JPanel();
private JPanel information_jp = new JPanel();
// 접속자 정답메세지
private GridLayout message_gl = new GridLayout(1, 5);
private JTextField message_jtf[] = new JTextField[5];
// 접속자정보
private GridLayout character_gl = new GridLayout(1, 5);
private BorderLayout character_bl[] = new BorderLayout[5];
private JPanel character_p[] = new JPanel[5];
private JLabel character_jlb[] = new JLabel[5];
private JLabel characterName_jlb[] = new JLabel[5];
// 제시어,채팅창,시작버튼
private BorderLayout right_bl = new BorderLayout();
// 제시어
private JPanel problem_jp = new JPanel();
private JTextField jtf = new JTextField("", 7);
private JLabel problem_jlb = new JLabel("제시어");
// 채팅창
private BorderLayout chat_bl = new BorderLayout();
private FlowLayout chat_fl = new FlowLayout(FlowLayout.RIGHT);
private JPanel chat_jp = new JPanel();
private JPanel chatMsg_jp = new JPanel();
private JTextArea chat_jta = new JTextArea();
private JTextField chat_jtf = new JTextField(20);
private JButton chat_jbt = new JButton("정답");
private JScrollPane jsp = new JScrollPane(chat_jta);
// 나가기예약,준비,시작
private BorderLayout button_bl = new BorderLayout();
private GridLayout ready_gl = new GridLayout(1, 2);
private JPanel button_jp = new JPanel();
private JPanel ready_jp = new JPanel();
private JButton exit_jbt = new JButton("나가기");
private JButton ready_jbt = new JButton("준비");
private JButton start_jbt = new JButton("시작");
private int flag = 0;// 나가기예약 판별
private boolean f = false;// 레디버튼을 눌렀을때 나가기버튼 활성화 비활성화
private boolean b = false;// 제한시간내에 정답을 맞추면 true가 되어 스레드가 멈춘다.
private int time = 60;
public MyFrame(String address, String name) {
ipAddress = address;
usr_id = name;
super.setSize(1200, 800);
super.setBackground(Color.WHITE);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int xpos = (int) (screen.getWidth() / 2 - this.getWidth() / 2);
int ypos = (int) (screen.getHeight() / 2 - this.getHeight() / 2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
this.init();
this.start();
this.chatClient();
}
public void init() {
Container con = this.getContentPane();
// 그림그릴 이미지 생성
ci.setImg(createImage(770, 600));
ci.setgImg((Graphics2D) ci.getImg().getGraphics());
ci.getgImg().setStroke(new BasicStroke(4.0f));
// ci.repaint();
// 기본창
con.setLayout(base_bl);
con.add("Center", left_jp);
con.add("East", right_jp);
// 그림판
left_jp.setLayout(left_bl);
left_jp.add("Center", picture_jp);
picture_jp.setLayout(picture_bl);
picture_jp.add("South", pictureOption_jp);
picture_jp.add("East", correctAnswer_jp);
picture_jp.add("Center", ci);
ci.setEnabled(false);
pictureOption_jp.setLayout(pictureOption_gl);
for (int i = 0; i < pictureOption_jbt.length; ++i) {
for (int j = 0; j < pictureOption_jbt[i].length; ++j) {
pictureOption_jbt[i][j] = new JButton(" ");
pictureOption_jp.add(pictureOption_jbt[i][j]);
if (i == 0 && j == 0)
pictureOption_jbt[i][j].setBackground(Color.RED);
if (i == 0 && j == 1)
pictureOption_jbt[i][j].setBackground(Color.BLUE);
if (i == 0 && j == 2)
pictureOption_jbt[i][j].setBackground(Color.WHITE);
if (i == 1 && j == 0)
pictureOption_jbt[i][j].setBackground(Color.GREEN);
if (i == 1 && j == 1)
pictureOption_jbt[i][j].setBackground(Color.YELLOW);
if (i == 1 && j == 2)
pictureOption_jbt[i][j].setBackground(Color.BLACK);
pictureOption_jbt[i][j].addActionListener(this);
}
}
// 정답자텍스트필드
correctAnswer_jp.setLayout(correctAnswer_gl);
for (int i = 0; i < correctAnswer_jtf.length; ++i) {
correctAnswer_jtf[i] = new JTextField("정답자" + i);
correctAnswer_jtf[i].setFont(new Font("", Font.BOLD, 30));
tb[i] = new TitledBorder((i + 1) + "번문제 맞춘사람");
correctAnswer_jtf[i].setBorder(tb[i]);
correctAnswer_jp.add(correctAnswer_jtf[i]);
correctAnswer_jtf[i].setEditable(false);
}
// 제한시간
time_jlb.setFont(new Font("", Font.BOLD, 30));
TitledBorder tb = new TitledBorder("제한시간");
time_jlb.setBorder(tb);
correctAnswer_jp.add(time_jlb);
// 접속자판
left_jp.add("South", information_jp);
information_jp.setLayout(information_bl);
// 정답텍스트필드
information_jp.add("North", message_jp);
message_jp.setLayout(message_gl);
for (int i = 0; i < message_jtf.length; ++i) {
message_jtf[i] = new JTextField("정답");
message_jtf[i].setFont(new Font("", Font.BOLD, 30));
message_jp.add(message_jtf[i]);
message_jtf[i].setEditable(false);
}
// 접속자정보라벨
information_jp.add("Center", character_jp);
character_jp.setLayout(character_gl);
for (int i = 0; i < 5; ++i) {
character_p[i] = new JPanel();
character_bl[i] = new BorderLayout();
character_jlb[i] = new JLabel("캐릭터" + i);
characterName_jlb[i] = new JLabel("닉네임" + i);
character_jlb[i].setFont(new Font("", Font.BOLD, 40));
characterName_jlb[i].setFont(new Font("", Font.BOLD, 20));
character_jp.add(character_p[i]);
character_p[i].setLayout(character_bl[i]);
character_p[i].add("Center", character_jlb[i]);
character_p[i].add("South", characterName_jlb[i]);
}
characterName_jlb[0].setText(usr_id);
// 제시어,채팅창,준비버튼
right_jp.setLayout(right_bl);
right_jp.add("North", problem_jp);
right_jp.add("Center", chat_jp);
right_jp.add("South", button_jp);
// 제시어
TitledBorder b1 = new TitledBorder("제시어");
jtf.setBorder(b1);
problem_jp.add(jtf);
jtf.setEditable(false);
jtf.setFont(new Font("", Font.BOLD, 40));
// 채팅창
chat_jp.setLayout(chat_bl);
chat_jp.add("Center", jsp);
chat_jta.setEditable(false);
chat_jp.add("South", chatMsg_jp);
chatMsg_jp.setLayout(chat_fl);
chatMsg_jp.add(chat_jtf);
chatMsg_jp.add(chat_jbt);
chat_jbt.setEnabled(false);
// 나가기,준비,시작버튼
button_jp.setLayout(button_bl);
button_jp.add("North", exit_jbt);
button_jp.add("Center", ready_jp);
ready_jp.setLayout(ready_gl);
ready_jp.add(ready_jbt);
ready_jp.add(start_jbt);
exit_jbt.setFont(new Font("", Font.BOLD, 30));
ready_jbt.setFont(new Font("", Font.BOLD, 50));
start_jbt.setFont(new Font("", Font.BOLD, 50));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 윈도우창 종료.
}
public void start() {
for (int i = 0; i < message_jtf.length; ++i) {
message_jtf[i].addActionListener(this);
}
chat_jtf.addActionListener(this);
chat_jbt.addActionListener(this);
exit_jbt.addActionListener(this);
ready_jbt.addActionListener(this);
start_jbt.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String msg = null;
if (e.getSource() == pictureOption_jbt[0][0])
ci.getgImg().setColor(Color.RED);
else if (e.getSource() == pictureOption_jbt[0][1])
ci.getgImg().setColor(Color.BLUE);
else if (e.getSource() == pictureOption_jbt[0][2])
ci.getgImg().setColor(Color.WHITE);
else if (e.getSource() == pictureOption_jbt[1][0])
ci.getgImg().setColor(Color.GREEN);
else if (e.getSource() == pictureOption_jbt[1][1])
ci.getgImg().setColor(Color.YELLOW);
else if (e.getSource() == pictureOption_jbt[1][2])
ci.getgImg().setColor(Color.BLACK);
else if (e.getSource() == pictureOption_jbt[0][3])
ci.getgImg().setStroke(new BasicStroke(1.0f));
else if (e.getSource() == pictureOption_jbt[1][3])
ci.getgImg().setStroke(new BasicStroke(7.0f));
else if (e.getActionCommand() == "나가기") {
int x = JOptionPane.showConfirmDialog(this, "게임을 종료하시겠습니까?",
"게임종료", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (x == 0) {
String m = "/quit";
try {
oos.writeObject(m);
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
} else if (e.getActionCommand() == "나가기 예약") {
if (flag == 0) {
chat_jta.append(characterName_jlb[0].getText()
+ "님이 나가기 예약을 하셨습니다.\n");
flag = 1;
} else {
chat_jta.append(characterName_jlb[0].getText()
+ "님이 나가기 예약을 취소하셨습니다.\n");
flag = 0;
}
} else if (e.getSource() == ready_jbt) {
if (f == false) {
exit_jbt.setEnabled(false);
f = true;
} else {
exit_jbt.setEnabled(true);
f = false;
}
} else if (e.getSource() == start_jbt) {
jtf.setText("고등어");// 제시어 셋팅
this.buttonOff();
Thread t = new Thread(this);
t.start();
} else if (e.getSource() == chat_jbt) {
msg = chat_jtf.getText();
if (msg.trim().length() == 0) {// 엔터키만누르면 처리되지 않는다.
return;
}
message_jtf[0].setText(msg);
chat_jtf.setText("");
this.check(msg);
} else {
chat_jtf = (JTextField) e.getSource();
msg = chat_jtf.getText();
//chat_jta.setForeground(Color.BLACK);
if (msg.trim().length() == 0) {// 엔터키만누르면 처리되지 않는다.
return;
}
try {//메세지 전송부분
oos.writeObject(msg);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
chat_jtf.setText("");
chat_jtf.requestFocus();
}
}
public void check(String msg) {
if (message_jtf[0].getText().equals(jtf.getText())) {
b = true;
JOptionPane.showMessageDialog(this, "정답입니다", "축하합니다",
JOptionPane.INFORMATION_MESSAGE);
correctAnswer_jtf[0].setText(usr_id);
message_jtf[0].setText("");
} else {
chat_jta.append(characterName_jlb[0].getText() + "님 틀렸습니다.\n");
}
}
public void chatClient() {
try {
client = new Socket(ipAddress, server_port);
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
oos.writeObject(usr_id);
oos.flush();
rt = new ReceiveDataThread();
Thread t = new Thread(rt);
t.start();
chat_jtf.requestFocus();
} catch (Exception e) {
}
}
class ReceiveDataThread implements Runnable {
String receiveData;
@Override
public void run() {
try {
while (true) {
receive ois.readObject();
chat_jta.append(receiveData + "\n");
}
} catch (ClassNotFoundException e) {
System.out.print(e);
} catch (IOException e) {
System.out.print(e);
}
}
}
@Override
public void run() {
while (time > 0) {
try {
Thread.sleep(1000);
if (b == true) {
b = false;
break;
}
} catch (Exception z) {
}
time--;
time_jlb.setText(time + "초");
}
if (time == 0) {
JOptionPane.showMessageDialog(this,
"정답은 " + jtf.getText() + " 입니다", "TIME OVER",
JOptionPane.INFORMATION_MESSAGE);
this.buttonOn();
} else {
this.buttonOn();
}
}
public void buttonOff() {
ci.setEnabled(true);
start_jbt.setEnabled(false);
ready_jbt.setEnabled(false);
exit_jbt.setText("나가기 예약");
chat_jbt.setEnabled(true);
exit_jbt.setEnabled(true);
}
public void buttonOn() {
if (flag == 1)
System.exit(0);
jtf.setText("");
time = 60;
time_jlb.setText("60초");
ci.setEnabled(false);
chat_jbt.setEnabled(false);
ready_jbt.setEnabled(true);
start_jbt.setEnabled(true);
exit_jbt.setText("나가기");
exit_jbt.setEnabled(true);
}
}
public class Main {
public static void main(String[] args) {
MyFrame mf = new MyFrame(null,null);
}
}
이게 메인인데 어떻게 합칠수있죠..?
제가 진짜 초보자라 좀 설명좀 해주세요,
이렇게 긴 소스를 아무 대가 없이 읽는 사람들이 있겠냐
음.. 읽어달라는거라기 보단. 그냥 두 클래스를 합치려면 어떤걸 써야하는지만 길만 잡아주시면 .. 제가 공부하는데 아주 큰 도움이 될거같습니더 ㅠ
테스트