JAVA 프로그래밍

문제

리모컨으로 제어하는 TV 프로그램의 GUI 문제입니다 실행순서를 클릭하세요 
 

프로그램 코드

E001	// 이벤트 대기중
E002	// POWER를 선택
E003	// LEFT를 선택
E004	// RIGHT를 선택
E005	// UP을 선택
E006	// DOWN을 선택
E007	// POWER를 선택

	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		}
	}
			
	package remoteControl;
	import javax.swing.*;
	import java.awt.*;
	import java.awt.event.*;

	public class RemoteControllerPanel extends JPanel implements ActionListener
	{
		protected JButton[] button;
		public final static int POWER = 0, UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
		protected RemoteControl appliance;

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		}

	
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		}
	}

	package remoteControl;
	import java.awt.*;
	import javax.swing.*;

	public class TVPanel extends JPanel implements RemoteControl
	{
		private boolean power;
		private int channel, volume;
		private ImageIcon[] imgChannel, imgVolume;
		private JLabel lblChannel, lblVolume;
		
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		}
		
		@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		}
		
		@Override
T3b		public void clickUp() {
			if( power == ON ) {
				channel = ( channel + 1 ) % ( imgChannel.length - 1 );
				lblChannel.setIcon( imgChannel[channel] );
			}
T3e		}
		
		@Override
T4b		public void clickDown() {
			if( power == ON ) {
				channel = ( channel + ( imgChannel.length - 2 ) ) % ( imgChannel.length - 1 );
				lblChannel.setIcon( imgChannel[channel] );
			}
T4e		}
		
		@Override
T5b		public void clickLeft() {
			if( power == ON ) {
				volume = ( volume + ( imgVolume.length - 1 ) ) % imgVolume.length;
				lblVolume.setIcon( imgVolume[volume] );
			}
T5e		}
		
		@Override
T6b		public void clickRight() {
			if( power == ON ) {
				volume = ( volume + 1 ) % imgVolume.length;
				lblVolume.setIcon( imgVolume[volume] );
			}
T6e		}
	}

	package remoteControl;

	public interface RemoteControl
	{
		boolean ON = true, OFF = false;
		void clickPower();
		void clickUp();
		void clickDown();
		void clickLeft();
		void clickRight();
	}








 
실행 순서