문제
네트워크를 바탕으로 여러 가전을 제어하기 위한 리모컨 프로그램입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import java.awt.event.*;
import remoteControl.RemoteControlNetwork;
public class RemoteController
{
public static void main( String[] args ) {
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\remoteControl\\image\\";
String serverIP = "localhost";
RemoteControlNetwork panel =
new RemoteControlNetwork( imagePath, serverIP );
JFrame frame = new JFrame( "리모컨(클라이언트)" );
frame.getContentPane().add( panel );
frame.addWindowListener( new () {
public void ( WindowEvent event ) {
panel.close();
}
} );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
package remoteControl;
import java.awt.event.ActionEvent;
import network.Network;
public class RemoteControlNetwork RemoteControllerPanel
{
private Network network;
private String id;
private final String header = "[RMTC] ";
public RemoteControlNetwork( String imgPath, String serverIP ) {
super( imgPath );
connectAsClient( serverIP, this.getClass().getSimpleName() );
}
public RemoteControlNetwork( RemoteControl appliance, String serverIP ) {
super( appliance );
connectAsClient( serverIP, appliance.getClass().getSimpleName() );
}
public void connectAsClient( String serverIP, String applianceName ) {
id = "[" + applianceName +"]";
network =
new Network();
network.connectAsClient( serverIP, this );
}
@Override
public void ( ActionEvent event ) {
String message = "";
if ( event.getSource() == button[POWER] )
message = header + "POWER";
else if ( event.getSource() == button[UP] )
message = header + "UP";
else if ( event.getSource() == button[DOWN] )
message = header + "DOWN";
else if ( event.getSource() == button[LEFT] )
message = header + "LEFT";
else if ( event.getSource() == button[RIGHT] )
message = header + "RIGHT";
network.write( message );
}
@Override
public void (){
for ( String message = null; ( message =
network.read() )
!= null; ) {
if ( !message.contains( header ) )
continue;
else if ( message.contains( "POWER" ) && ( appliance != null ) )
appliance.clickPower();
else if ( message.contains( "UP" ) && ( appliance != null ) )
appliance.clickUp();
else if ( message.contains( "DOWN" ) && ( appliance != null ) )
appliance.clickDown();
else if ( message.contains( "LEFT" ) && ( appliance != null ) )
appliance.clickLeft();
else if ( message.contains( "RIGHT" ) && ( appliance != null ) )
appliance.clickRight();
}
}
public void close() {
network.write( header + id + " 네트워크 연결 종료" );
network.close();
}
}
package network;
import java.io.*;
import java.net.*;
public class Network
{
public static final int serverPort = 7700;
protected serverSocket;
protected socket;
private in;
private out;
private waitForCounterpart;
public Network() {
serverSocket = null;
socket = null;
in = null;
out = null;
waitForCounterpart = null;
}
public void connectAsServer( obj ) {
{
serverSocket = new ( serverPort );
socket =
serverSocket.();
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectAsClient( String serverIP, obj ) {
{
socket =
new ( serverIP, serverPort );
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectInOut( obj ) {
{
in = new ( new ( socket.() ));
out = new ( socket.(), true );
waitForCounterpart = new ( obj );
waitForCounterpart.();
} ( Exception e ) {
e.printStackTrace();
}
}
public String read() {
{
if ( this.isConnecting() == true )
return
in.readLine();
} ( Exception e ) {
e.printStackTrace();
}
return null;
}
public void write( String data ) {
{
if ( this.isConnecting() == true )
out.println( data );
} ( Exception e ) {
e.printStackTrace();
}
}
public boolean isConnecting() {
if ( ( socket != null )
&& ( in != null )
&& ( out != null )
&& ( waitForCounterpart != null )
&& ( waitForCounterpart.getState() != .State.TERMINATED ) )
return true;
else
return false;
}
public void close() {
{
if ( waitForCounterpart != null ) {
waitForCounterpart.interrupt();
waitForCounterpart = null;
}
if ( in != null ) {
in.close();
in=null;
}
if ( out != null ) {
out.flush();
out.close();
out = null;
}
if ( socket != null ) {
socket.close();
socket = null;
}
if ( serverSocket != null ) {
serverSocket.close();
serverSocket = null;
}
} ( Exception e ) {
e.printStackTrace();
}
}
}
package remoteControl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RemoteControllerPanel JPanel ActionListener
{
protected JButton[] button;
public final static int POWER = 0, UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
protected RemoteControl appliance;
public RemoteControllerPanel( String imgPath ) {
this.appliance = null;
final String[] strButton = { "power.gif", "up.gif", "down.gif", "left.gif", "right.gif" };
button = new JButton[strButton.length];
for ( int i = 0; i < strButton.length; i++ ) {
button[i] = new JButton( new ImageIcon( new ImageIcon( imgPath + strButton[i] ).getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[i].addActionListener( this );
}
this.setPreferredSize( new Dimension( 240, 120 ) );
this.setLayout( new BorderLayout() );
this.add( button[POWER], BorderLayout.CENTER );
this.add( button[UP ], BorderLayout.NORTH );
this.add( button[DOWN ], BorderLayout.SOUTH );
this.add( button[LEFT ], BorderLayout.WEST );
this.add( button[RIGHT], BorderLayout.EAST );
}
public RemoteControllerPanel( String imgPath, RemoteControl appliance ) {
this( imgPath );
this.appliance = appliance;
}
public RemoteControllerPanel( RemoteControl appliance ) {
this.appliance = appliance;
}
@Override
public void ( ActionEvent event ) {
if ( ( event.getSource() == button[POWER] ) && ( appliance != null ) )
appliance.clickPower();
else if ( ( event.getSource() == button[UP] ) && ( appliance != null ) )
appliance.clickUp();
else if ( ( event.getSource() == button[DOWN] ) && ( appliance != null ) )
appliance.clickDown();
else if ( ( event.getSource() == button[LEFT] ) && ( appliance != null ) )
appliance.clickLeft();
else if ( ( event.getSource() == button[RIGHT] ) && ( appliance != null ) )
appliance.clickRight();
}
}
package remoteControl;
public RemoteControl
{
boolean ON = true, OFF = false;
void clickPower();
void clickUp();
void clickDown();
void clickLeft();
void clickRight();
}