문제
네트워크를 바탕으로 리모컨으로 제어되는 TV 프로그램입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import java.awt.event.*;
import remoteControl.*;
public class RemoteControlTV
{
public static void main( String[] args ) {
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\remoteControl\\image\\";
String serverIP = "localhost";
RemoteControl appliance =
new TVPanel( imagePath );
RemoteControlNetwork panel =
new RemoteControlNetwork( appliance, serverIP );
JFrame frame = new JFrame( "TV(클라이언트)" );
frame.getContentPane().add( (JPanel)appliance );
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;
import java.awt.*;
import javax.swing.*;
public class TVPanel JPanel RemoteControl
{
private boolean power;
private int channel, volume;
private ImageIcon[] imgChannel, imgVolume;
private JLabel lblChannel, lblVolume;
public TVPanel( final String imgPath ) {
final String[] channelFile = { "EBS.gif", "SBS.gif", "KBS.gif", "MBC.gif", "blank.gif" };
imgChannel = new ImageIcon[channelFile.length];
for ( int i = 0; i < channelFile.length; i++ ) {
imgChannel[i] = new ImageIcon( new ImageIcon( imgPath + channelFile[i] ).getImage().getScaledInstance( 250, 120, Image.SCALE_SMOOTH ) );
}
final String[] volumeFile = { "volume0.gif", "volume1.gif", "volume2.gif", "volume3.gif" };
imgVolume = new ImageIcon[volumeFile.length];
for ( int i = 0; i < volumeFile.length; i++ ) {
imgVolume[i] = new ImageIcon( new ImageIcon( imgPath + volumeFile[i] ).getImage().getScaledInstance( 80, 120, Image.SCALE_SMOOTH ) );
}
power = OFF;
channel = imgChannel.length - 1;
volume = 0;
lblChannel = new JLabel( imgChannel[channel] );
lblVolume = new JLabel( imgVolume[volume] );
add( lblChannel );
add( lblVolume );
}
@Override
public void clickPower() {
if( power == OFF ) {
power = ON;
channel = 0;
volume = 1;
lblChannel.setIcon( imgChannel[channel] );
lblVolume.setIcon( imgVolume[volume] );
}
else {
power = OFF;
channel = imgChannel.length - 1;
volume = 0;
lblChannel.setIcon( imgChannel[channel] );
lblVolume.setIcon( imgVolume[volume] );
}
}
@Override
public void clickUp() {
if( power == ON ) {
channel = ( channel + 1 ) % ( imgChannel.length - 1 );
lblChannel.setIcon( imgChannel[channel] );
}
}
@Override
public void clickDown() {
if( power == ON ) {
channel = ( channel + ( imgChannel.length - 2 ) ) % ( imgChannel.length - 1 );
lblChannel.setIcon( imgChannel[channel] );
}
}
@Override
public void clickLeft() {
if( power == ON ) {
volume = ( volume + ( imgVolume.length - 1 ) ) % imgVolume.length;
lblVolume.setIcon( imgVolume[volume] );
}
}
@Override
public void clickRight() {
if( power == ON ) {
volume = ( volume + 1 ) % imgVolume.length;
lblVolume.setIcon( imgVolume[volume] );
}
}
}
package remoteControl;
public RemoteControl
{
boolean ON = true, OFF = false;
void clickPower();
void clickUp();
void clickDown();
void clickLeft();
void clickRight();
}