import javax.swing.*; import java.awt.*; public class CirclesDemo extends JFrame { public CirclesDemo() { setTitle("xxx"); class MyPanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Color[] colors = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, new Color(0x000080), new Color(0x800080) }; for(int i = 0; i < 7; i++) { int d = delta(i); g.setColor(colors[i]); g.drawOval(10 + d, 10 + d, 240 - 2 * d, 240 - 2 * d); //coordinator x, y, 지름이 양쪽에 있으니 -2*d } } int delta(int x) { //square 속 circles 간의 간격 double diagonal = Math.sqrt(240 * 240); return (int)(diagonal * x * 15 / 240); //15 is usual } } add(new MyPanel()); //new JPanel로 무명 클래스 만들 수도 있음 setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(300,300); setVisible(true); } public static void main(String[] args) { new CirclesDemo(); } }
5.28.2019
[JAVA] Graphic Programming - Panel
2.02.2018
TCP/IP advanced
1. Make two Java Projects (Name one ServerProgram, and the other, ClientProgram)
2. Make three classes for each (ex. Server.java // SendThread.java // ReceiveThread.java)
<Server.java code>
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(5050);
System.out.println("***채팅방이 열렸습니다***");
System.out.println("기다리는 중...\n");
socket = serverSocket.accept(); // method를 사용하여 client로부터 연결 요청을 기다리고 요청이 오면 연결을 맺은 후 소켓을 생성함
System.out.println("상대방이 입장했습니다.");
SendThread st = new SendThread(socket);
ReceiveThread rt = newReceiveThread(socket);
st.start();
rt.start();
} catch (Exception e) {
} finally {
try {
// socket.close(); // 소켓을 닫는다.
serverSocket.close(); // 서버소켓을 닫는다.
} catch (Exception e) {
}
}
}
}
========================================================================
<Client.java code>
import java.net.Socket;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket socket = null;
try {
socket = new Socket("210.125.97.125", 5050);
System.out.println("***채팅방에 들어왔습니다***");
SendThread st = new SendThread(socket);
ReceiveThread rt = new ReceiveThread(socket);
st.start();
rt.start();
} catch (Exception e) {
} finally {
try {
// socket.close();
} catch (Exception e) {
}
}
}
}
2. Make three classes for each (ex. Server.java // SendThread.java // ReceiveThread.java)
<Server.java code>
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(5050);
System.out.println("***채팅방이 열렸습니다***");
System.out.println("기다리는 중...\n");
socket = serverSocket.accept(); // method를 사용하여 client로부터 연결 요청을 기다리고 요청이 오면 연결을 맺은 후 소켓을 생성함
System.out.println("상대방이 입장했습니다.");
SendThread st = new SendThread(socket);
ReceiveThread rt = newReceiveThread(socket);
st.start();
rt.start();
} catch (Exception e) {
} finally {
try {
// socket.close(); // 소켓을 닫는다.
serverSocket.close(); // 서버소켓을 닫는다.
} catch (Exception e) {
}
}
}
}
========================================================================
<Client.java code>
import java.net.Socket;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket socket = null;
try {
socket = new Socket("210.125.97.125", 5050);
System.out.println("***채팅방에 들어왔습니다***");
SendThread st = new SendThread(socket);
ReceiveThread rt = new ReceiveThread(socket);
st.start();
rt.start();
} catch (Exception e) {
} finally {
try {
// socket.close();
} catch (Exception e) {
}
}
}
}
========================================================================
<SendThread.java code>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SendThread extends Thread {
Socket socket = null;
SendThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// system.in을 이용하여 console창에 입력함.
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// socket.getOutputStream을 통해 소켓의 출력 스트림을 가져온다. client로 data 보낼 수 있다.
// 데이터를 계속 주고받을 수 있도록
while (true) {
String str = reader.readLine();
if (str.equals("BYE")) {
break;
}
writer.println(str); // 한 행의 byte data를 쓴다.
writer.flush();
}
} catch (Exception e) {
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
========================================================================
<ReceiveThread.java code>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class ReceiveThread extends Thread {
Socket socket = null;
ReceiveThread(Socket s) {
socket = s;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String str = reader.readLine();
if (str == null)
break;
System.out.println(str);
}
} catch (Exception e) {
}
}
}
<SendThread.java code>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SendThread extends Thread {
Socket socket = null;
SendThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// system.in을 이용하여 console창에 입력함.
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// socket.getOutputStream을 통해 소켓의 출력 스트림을 가져온다. client로 data 보낼 수 있다.
// 데이터를 계속 주고받을 수 있도록
while (true) {
String str = reader.readLine();
if (str.equals("BYE")) {
break;
}
writer.println(str); // 한 행의 byte data를 쓴다.
writer.flush();
}
} catch (Exception e) {
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
========================================================================
<ReceiveThread.java code>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class ReceiveThread extends Thread {
Socket socket = null;
ReceiveThread(Socket s) {
socket = s;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String str = reader.readLine();
if (str == null)
break;
System.out.println(str);
}
} catch (Exception e) {
}
}
}
TCP/IP Programming - ClientProgram
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 5050);
System.out.println("***채팅방에 들어왔습니다***");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("Hi, I'm client!");
writer.flush();
System.out.println(reader.readLine());
} catch (Exception e) {
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 5050);
System.out.println("***채팅방에 들어왔습니다***");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("Hi, I'm client!");
writer.flush();
System.out.println(reader.readLine());
} catch (Exception e) {
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
TCP/IP Programming - ServerProgram
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(5050);
System.out.println("***채팅방이 열렸습니다***");
System.out.println("기다리는 중...\n");
socket = serverSocket.accept(); // method를 사용하여 client로부터 연결 요청을 기다리고 요청이 오면 연결을 맺은 후 소켓을 생성함
System.out.println("상대방이 입장했습니다.");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // socket.getInputStream을 통해 소켓의 입력 stream을 가져온다. 이를 통해 client가 보낸 data를 읽어 올 수 있다.
PrintWriter writer= new PrintWriter(socket.getOutputStream()); // socket.getOutputStream을 통해 소켓의 출력 스트림을 가져온다. client로 data 보낼 수 있다.
System.out.println(reader.readLine()); // 읽어 온 한 행의 문자열 데이터를 출력한다.
writer.println("Hi, I'm server!"); // 한 행의 byte data를 쓴다.
writer.flush(); // 버퍼가 다 찬 후 한꺼번에 데이터를 보내기 때문에 이를 기다리지 않고 바로 보내기 위해서 flush() method를 사용한다.
} catch (Exception e) {
} finally {
try {
socket.close(); // 소켓을 닫는다.
serverSocket.close(); // 서버소켓을 닫는다.
} catch (Exception e) {
}
}
}
MultiThread
class AlarmThread extends Thread { // 1초마다 알림 울리기
public void run() {
int i = 1;
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
if (i == 10) {
System.out.println("\t10seconds!");
break;
} else {
System.out.println("\t" + i + "초");
}
i++;
}
}
}
class PlusThread extends Thread {
public void run() {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
System.out.println(sum);
}
}
public class PrintAlphabet extends Thread {
int alphabet = 26;
public void run() {
// print alphabet
for (int i = 0; i < alphabet; i++) {
System.out.println((char) ('A' + i));
try {
Thread.sleep(500); // 일시중지하는 명령어로 인자에 값을 넣어주면, 넣은 값만큼 시간을 지연시킨 후에 다음 명령을 실행
} catch (Exception e) {
System.out.println(e);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintAlphabet pa = new PrintAlphabet();
AlarmThread at = new AlarmThread();
PlusThread pt = new PlusThread();
pa.start();
at.start();
pt.start();
}
}
public void run() {
int i = 1;
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
if (i == 10) {
System.out.println("\t10seconds!");
break;
} else {
System.out.println("\t" + i + "초");
}
i++;
}
}
}
class PlusThread extends Thread {
public void run() {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
System.out.println(sum);
}
}
public class PrintAlphabet extends Thread {
int alphabet = 26;
public void run() {
// print alphabet
for (int i = 0; i < alphabet; i++) {
System.out.println((char) ('A' + i));
try {
Thread.sleep(500); // 일시중지하는 명령어로 인자에 값을 넣어주면, 넣은 값만큼 시간을 지연시킨 후에 다음 명령을 실행
} catch (Exception e) {
System.out.println(e);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintAlphabet pa = new PrintAlphabet();
AlarmThread at = new AlarmThread();
PlusThread pt = new PlusThread();
pa.start();
at.start();
pt.start();
}
}
2.01.2018
JAVA Traffic2
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class SignalLamp extends JFrame {
SignalLamp() {
setTitle("TRAFFIC");
setSize(200, 500);
setLayout(null);
// JPanel
JPanel trafficPanel= new JPanel();
trafficPanel.setBorder(new LineBorder(Color.black, 3));
trafficPanel.setLayout(null);
trafficPanel.setLocation(34, 30);
trafficPanel.setSize(120, 360);
trafficPanel.setBackground(Color.black);
this.add(trafficPanel);
// JLabel - red signal
ImageIcon redImg = new ImageIcon("img/red.png");
final JLabel red = new JLabel(redImg); //final을 붙이면 값이 고정됨
red .setHorizontalAlignment(SwingConstants.CENTER);
red.setBorder(new LineBorder(Color.black, 1));
red.setLocation(0, 0);
red.setSize(120, 120);
trafficPanel.add(red);
// JLabel - yellow signal
ImageIcon yellowImg = new ImageIcon("img/yellow.png");
final JLabel yellow = new JLabel(yellowImg);
yellow.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setBorder(new LineBorder(Color.black, 1));
yellow.setLocation(0, 400);
yellow.setSize(120, 120);
trafficPanel.add(yellow);
// JLabel - green signal
ImageIcon greenImg = new ImageIcon("img/green.png");
JLabel green = new JLabel(greenImg);
green.setHorizontalAlignment(SwingConstants.CENTER);
green.setBorder(new LineBorder(Color.black, 1));
green.setLocation(0, 240);
green.setSize(120, 120);
trafficPanel.add(green);
//JLabel - no signal
ImageIcon nosignalImg = new ImageIcon("img/nosignal.png");
JLabel nosignal= new JLabel(nosignalImg);
nosignal.setHorizontalAlignment(SwingConstants.CENTER);
nosignal.setBorder(new LineBorder(Color.black, 1));
nosignal.setSize(120, 120);
trafficPanel.add(nosignal);
// JPanel buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(34, 400);
buttonPanel.setSize(120, 30);
this.add(buttonPanel);
// button
JButton button = new JButton("Change Signal");
button.setLocation(0, 0);
button.setSize(120, 30);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(red.getIcon().toString());
if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon(nosignalImg);
yellow.setIcon(yellowImg);
} else if (red.getIcon().toString().equals("img/red.png")){ //
yellow.setIcon(nosignalImg);
green.setIcon(greenImg);
} else if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon(nosignalImg);
yellow.setIcon(yellowImg);
}
}
});
//button.addActionListener(new MyActionListener(red))
buttonPanel.add(button);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SignalLamp sl = new SignalLamp();
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class SignalLamp extends JFrame {
SignalLamp() {
setTitle("TRAFFIC");
setSize(200, 500);
setLayout(null);
// JPanel
JPanel trafficPanel= new JPanel();
trafficPanel.setBorder(new LineBorder(Color.black, 3));
trafficPanel.setLayout(null);
trafficPanel.setLocation(34, 30);
trafficPanel.setSize(120, 360);
trafficPanel.setBackground(Color.black);
this.add(trafficPanel);
// JLabel - red signal
ImageIcon redImg = new ImageIcon("img/red.png");
final JLabel red = new JLabel(redImg); //final을 붙이면 값이 고정됨
red .setHorizontalAlignment(SwingConstants.CENTER);
red.setBorder(new LineBorder(Color.black, 1));
red.setLocation(0, 0);
red.setSize(120, 120);
trafficPanel.add(red);
// JLabel - yellow signal
ImageIcon yellowImg = new ImageIcon("img/yellow.png");
final JLabel yellow = new JLabel(yellowImg);
yellow.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setBorder(new LineBorder(Color.black, 1));
yellow.setLocation(0, 400);
yellow.setSize(120, 120);
trafficPanel.add(yellow);
// JLabel - green signal
ImageIcon greenImg = new ImageIcon("img/green.png");
JLabel green = new JLabel(greenImg);
green.setHorizontalAlignment(SwingConstants.CENTER);
green.setBorder(new LineBorder(Color.black, 1));
green.setLocation(0, 240);
green.setSize(120, 120);
trafficPanel.add(green);
//JLabel - no signal
ImageIcon nosignalImg = new ImageIcon("img/nosignal.png");
JLabel nosignal= new JLabel(nosignalImg);
nosignal.setHorizontalAlignment(SwingConstants.CENTER);
nosignal.setBorder(new LineBorder(Color.black, 1));
nosignal.setSize(120, 120);
trafficPanel.add(nosignal);
// JPanel buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(34, 400);
buttonPanel.setSize(120, 30);
this.add(buttonPanel);
// button
JButton button = new JButton("Change Signal");
button.setLocation(0, 0);
button.setSize(120, 30);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(red.getIcon().toString());
if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon(nosignalImg);
yellow.setIcon(yellowImg);
} else if (red.getIcon().toString().equals("img/red.png")){ //
yellow.setIcon(nosignalImg);
green.setIcon(greenImg);
} else if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon(nosignalImg);
yellow.setIcon(yellowImg);
}
}
});
//button.addActionListener(new MyActionListener(red))
buttonPanel.add(button);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SignalLamp sl = new SignalLamp();
}
}
JAVA traffic
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class SignalLamp extends JFrame {
SignalLamp() {
setTitle("TRAFFIC");
setSize(200, 500);
setLayout(null);
// JPanel
JPanel trafficPanel = new JPanel();
trafficPanel.setBorder(new LineBorder(Color.black, 3));
trafficPanel.setLayout(null);
trafficPanel.setLocation(34, 30);
trafficPanel.setSize(120, 360);
trafficPanel.setBackground(Color.black);
this.add(trafficPanel);
// JLabel - red signal
ImageIcon redImg = new ImageIcon("img/red.png");
JLabel red = new JLabel(redImg);
red.setText("RED");
red.setHorizontalAlignment(SwingConstants.CENTER);
red.setBorder(new LineBorder(Color.black, 1));
red.setLocation(0, 0);
red.setSize(120, 120);
trafficPanel.add(red);
// JLabel - yellow signal
ImageIcon yellowImg = new ImageIcon("img/yellow.png");
JLabel yellow = new JLabel(yellowImg);
yellow.setText("YELLOW");
yellow.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setBorder(new LineBorder(Color.black, 1));
yellow.setLocation(0, 400);
yellow.setSize(120, 120);
trafficPanel.add(yellow);
// JLabel - green signal
ImageIcon greenImg = new ImageIcon("img/green.png");
JLabel green = new JLabel(greenImg);
green.setText("GREEN");
green.setHorizontalAlignment(SwingConstants.CENTER);
green.setBorder(new LineBorder(Color.black, 1));
green.setLocation(0, 240);
green.setSize(120, 120);
trafficPanel.add(green);
// JPanel buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(34, 400);
buttonPanel.setSize(120, 30);
this.add(buttonPanel);
// button
JButton button = new JButton("Change Signal");
button.setLocation(0, 0);
button.setSize(120, 30);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(red.getIcon().toString());
if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon( Img);
yellow.setIcon(yellowImg);
}
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SignalLamp sl = new SignalLamp();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class SignalLamp extends JFrame {
SignalLamp() {
setTitle("TRAFFIC");
setSize(200, 500);
setLayout(null);
// JPanel
JPanel trafficPanel = new JPanel();
trafficPanel.setBorder(new LineBorder(Color.black, 3));
trafficPanel.setLayout(null);
trafficPanel.setLocation(34, 30);
trafficPanel.setSize(120, 360);
trafficPanel.setBackground(Color.black);
this.add(trafficPanel);
// JLabel - red signal
ImageIcon redImg = new ImageIcon("img/red.png");
JLabel red = new JLabel(redImg);
red.setText("RED");
red.setHorizontalAlignment(SwingConstants.CENTER);
red.setBorder(new LineBorder(Color.black, 1));
red.setLocation(0, 0);
red.setSize(120, 120);
trafficPanel.add(red);
// JLabel - yellow signal
ImageIcon yellowImg = new ImageIcon("img/yellow.png");
JLabel yellow = new JLabel(yellowImg);
yellow.setText("YELLOW");
yellow.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setBorder(new LineBorder(Color.black, 1));
yellow.setLocation(0, 400);
yellow.setSize(120, 120);
trafficPanel.add(yellow);
// JLabel - green signal
ImageIcon greenImg = new ImageIcon("img/green.png");
JLabel green = new JLabel(greenImg);
green.setText("GREEN");
green.setHorizontalAlignment(SwingConstants.CENTER);
green.setBorder(new LineBorder(Color.black, 1));
green.setLocation(0, 240);
green.setSize(120, 120);
trafficPanel.add(green);
// JPanel buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(34, 400);
buttonPanel.setSize(120, 30);
this.add(buttonPanel);
// button
JButton button = new JButton("Change Signal");
button.setLocation(0, 0);
button.setSize(120, 30);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(red.getIcon().toString());
if (red.getIcon().toString().equals("img/red.png")){ //
red.setIcon( Img);
yellow.setIcon(yellowImg);
}
}
});
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SignalLamp sl = new SignalLamp();
}
}
피드 구독하기:
글 (Atom)

