JAVA 프로그래밍

문제

리모컨으로 제어하는 TV 프로그램의 GUI 문제입니다 이를 해결하는 다음 프로그램을 해석하세요 
 

알고리즘

 리모컨 GUI 초기화 
   리모컨 버튼의 이미지 및 리스너 초기화

리모컨 버튼을 클릭시 가전제품 상태 업데이트
   리모컨 전원 버튼을 누르면 TV 전원 상태 변경
   리모컨 상(△) 버튼을 누르면 TV 채널 번호 증가
   리모컨 하(▽) 버튼을 누르면 TV 채널 번호 감소
   리모컨 좌(◁) 버튼을 누르면 TV 음량 감소
   리모컨 우(▷) 버튼을 누르면 TV 음량 증가

TV 화면 초기화
   이미지를 바탕으로 채널 및 음량 초기화


  TV 및 리모컨 이미지

 
 

  클래스 다이어그램
   
  초기화 과정

 

  버튼 클릭시 처리 과정

 
 

프로그램 코드

E001	// 이벤트 대기중
E002	// POWER를 선택
E003	// LEFT를 선택
E004	// RIGHT를 선택
E005	// UP을 선택
E006	// DOWN을 선택
E007	// POWER를 선택
	 
	// 파일명 : ./Chapter14/RemoteControlTVGUIMain.java
	import java.awt.*;
	import javax.swing.*;
	import remoteControl.RemoteControllerPanel;
	import remoteControl.TVPanel;
	 
	public class RemoteControlTVGUIMain
	{
1		public static void main( String[] args )
		{
			final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\remoteControl\\image\\";
			 
			// 판을 틀에 끼우고 실행 준비 완료
2			TVPanel appliance =
3			                    new TVPanel( imagePath );
4			RemoteControllerPanel remoteController =
5			                                         new RemoteControllerPanel( imagePath, appliance );
			JFrame frame = new JFrame( "TV" );
			frame.setLayout( new BorderLayout() );
			frame.add( remoteController, BorderLayout.WEST );
			frame.add( appliance, BorderLayout.EAST );
			frame.setPreferredSize( new Dimension( 600,170 ) );
			frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
			frame.pack();
			frame.setVisible( true );
6		}
	}
			 
	// 파일명 : ./src/remoteControl/RemoteControllerPanel.java
	package remoteControl;
	import javax.swing.*;
	import java.awt.*;
	import java.awt.event.*;
	 
	// 리모컨 GUI 클래스
	public class RemoteControllerPanel extends JPanel implements ActionListener
	{
		protected JButton[] button;
		public final static int POWER = 0, UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
		// 리모컨으로 제어할 수 있는 가전제품 : TV, 에어컨, 로봇청소기
		protected RemoteControl appliance;
	 
		// 리모컨 GUI 초기화 
R1b		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 );
R1e		}
	 
	 
		// 가전제품(TV, 로봇청소기, 에어컨)을 제어하는 리모컨 GUI 초기화
R2b		public RemoteControllerPanel( String imgPath, RemoteControl appliance ) {
R21			this( imgPath );
			this.appliance = appliance;
R2e		}
 
		// 리모컨 버튼을 클릭시 가전제품 상태 업데이트	
		@Override
R3b		public void actionPerformed( ActionEvent event ) {
			if ( ( event.getSource() == button[POWER] ) && ( appliance != null ) )
R31				appliance.clickPower();
			else if ( ( event.getSource() == button[UP] ) && ( appliance != null ) )
R32				appliance.clickUp();
			else if ( ( event.getSource() == button[DOWN] ) && ( appliance != null ) )
R33				appliance.clickDown();
			else if ( ( event.getSource() == button[LEFT] ) && ( appliance != null ) )
R34				appliance.clickLeft();
			else if ( ( event.getSource() == button[RIGHT] ) && ( appliance != null ) )
R35				appliance.clickRight();
R3e		}
	}
 
 
	// 파일명 : ./src/remoteControl/TVPanel.java
	package remoteControl;
	import java.awt.*;
	import javax.swing.*;
	 
	// 리모컨으로 제어하는 TV GUI 클래스
	public class TVPanel extends JPanel implements RemoteControl
	{
		private boolean power;
		private int channel, volume;
		private ImageIcon[] imgChannel, imgVolume;
		private JLabel lblChannel, lblVolume;
		 
		// TV 화면 초기화 
T1b		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 );
T1e		}
		 
		// 리모컨 전원 버튼을 누르면 TV 전원 상태 변경 
		@Override
T2b		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] );
			}
T2e		}
		 
		// 리모컨 상(△) 버튼을 누르면 TV 채널 번호 증가 
		@Override
T3b		public void clickUp() {
			if( power == ON ) {
				channel = ( channel + 1 ) % ( imgChannel.length - 1 );
				lblChannel.setIcon( imgChannel[channel] );
			}
T3e		}
		 
		// 리모컨 하(▽) 버튼을 누르면 TV 채널 번호 감소 
		@Override
T4b		public void clickDown() {
			if( power == ON ) {
				channel = ( channel + ( imgChannel.length - 2 ) ) % ( imgChannel.length - 1 );
				lblChannel.setIcon( imgChannel[channel] );
			}
T4e		}
		 
		// 리모컨 좌(◁) 버튼을 누르면 TV 음량 감소 
		@Override
T5b		public void clickLeft() {
			if( power == ON ) {
				volume = ( volume + ( imgVolume.length - 1 ) ) % imgVolume.length;
				lblVolume.setIcon( imgVolume[volume] );
			}
T5e		}
		 
		// 리모컨 우(▷) 버튼을 누르면 TV 음량 증가 
		@Override
T6b		public void clickRight() {
			if( power == ON ) {
				volume = ( volume + 1 ) % imgVolume.length;
				lblVolume.setIcon( imgVolume[volume] );
			}
T6e		}
	}
 
 
	// 파일명 : ./src/remoteControl/RemoteControl.java
	package remoteControl;
	 
	// 전원, 상, 하, 좌, 우 버튼이 있는 리모컨
	public interface RemoteControl
	{
		boolean ON = true, OFF = false;
		void clickPower();
		void clickUp();
		void clickDown();
		void clickLeft();
		void clickRight();
	}

실행 순서

 
 					※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다