Layout.java
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Layout extends JFrame implements FocusListener{
JButton startButton, aboutButton, helpButton, settingButton, exitButton;
KeyAction keyAction = new KeyAction();
ImageIcon background = new ImageIcon(".//source/background.png");
JLabel label = new JLabel(background);
public Layout(){
super("Teris version 1.000");
this.setBounds( 60, 100, 800, 600);
this.setTitle("Teris Version 1.00");
this.setLayout(null);
startButton = new JButton(new ImageIcon(Path(0)));//set startButton background
startButton.setBounds(getRectangle(0));//set startButton size & position
this.add(startButton);//add startButton in this panel
startButton.addFocusListener(this);//add focus listener for startButton
aboutButton = new JButton(new ImageIcon(Path(2)));//set aboutButton background
aboutButton.setBounds(getRectangle(2));//set aboutButton size & position
this.add(aboutButton);//add aboutButton in this panel
aboutButton.addFocusListener(this);//add focus listener for startButton
settingButton = new JButton(new ImageIcon(Path(4)));//set settingButton background
settingButton.setBounds(getRectangle(4));//set settingButton size & position
this.add(settingButton);//add settingButton in this panel
settingButton.addFocusListener(this);//add focus listener for settingButton
helpButton = new JButton(new ImageIcon(Path(6)));//set helpButton background
helpButton.setBounds(getRectangle(6));//set helpButton size & position
this.add(helpButton);//add helpButton in this panel
helpButton.addFocusListener(this);//add focus listener for helpButton
exitButton = new JButton(new ImageIcon(Path(8)));//set exitButton background
exitButton.setBounds(getRectangle(8));//set exitButton size & position
add(exitButton);//add exitButton in this panel
exitButton.addFocusListener(this);//add focus listener for exitButton
label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
this.add(label);
this.addKeyListener(keyAction);
this.setVisible(true);
this.selectMenu();
}
public String Path(int i){
String path[] = {
".//src/startUnfocus.png",
".//src/startFocus.png",
".//src/aboutUnfocus.png",
".//src/aboutFocus.png",
".//src/settingUnfocus.png",
".//src/settingFocus.png",
".//src/helpUnfocus.png",
".//src/helpFocus.png",
".//src/exitUnfocus.png",
".//src/exitFocus.png"
};
return path[i];
}
//according image width & height to setting menu button position & size
public Rectangle getRectangle(int i){
File picture[] = new File [10];
Rectangle pictureRectangle[] = new Rectangle[10];
BufferedImage sourceImage[] = new BufferedImage[10];
try{
for(int j = 0; j < 10; j++){
picture[j] = new File(Path(j));
sourceImage[j] = ImageIO.read(new FileInputStream(picture[j]));
pictureRectangle[j] = new Rectangle((this.getWidth() - sourceImage[j].getWidth()) / 2,
(int)(j / 2) * this.getHeight() / 6 + 30, sourceImage[j].getWidth(), sourceImage[j].getHeight());
}
return pictureRectangle[i];
}
catch(IOException e){
e.printStackTrace();
return null;
}
}
public void focusGained(FocusEvent e) {
if(e.getSource() == startButton){
startButton.setIcon(new ImageIcon(Path(1)));
keyAction.menuIndex = 0;
}
if(e.getSource() == aboutButton){
aboutButton.setIcon(new ImageIcon(Path(3)));
keyAction.menuIndex = 1;
}
if(e.getSource() == settingButton){
settingButton.setIcon(new ImageIcon(Path(5)));
keyAction.menuIndex = 2;
}
if(e.getSource() == helpButton){
helpButton.setIcon(new ImageIcon(Path(7)));
keyAction.menuIndex = 3;
}
if(e.getSource() == exitButton){
exitButton.setIcon(new ImageIcon(Path(9)));
keyAction.menuIndex = 4;
}
System.out.println(keyAction.menuIndex);
}
public void focusLost(FocusEvent e) {
if(e.getSource() == startButton){
startButton.setIcon(new ImageIcon(Path(0)));
}
if(e.getSource() == aboutButton){
aboutButton.setIcon(new ImageIcon(Path(2)));
}
if(e.getSource() == settingButton){
settingButton.setIcon(new ImageIcon(Path(4)));
}
if(e.getSource() == helpButton){
helpButton.setIcon(new ImageIcon(Path(6)));
}
if(e.getSource() == exitButton){
exitButton.setIcon(new ImageIcon(Path(8)));
}
}
public void selectMenu(){
switch(keyAction.menuIndex){
case 0:
startButton.requestFocus(true);
break;
case 1:
aboutButton.requestFocus(true);
break;
case 2:
settingButton.requestFocus(true);
break;
case 3:
helpButton.requestFocus(true);
break;
case 4:
exitButton.requestFocus(true);
break;
}
}
}
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Layout extends JFrame implements FocusListener{
JButton startButton, aboutButton, helpButton, settingButton, exitButton;
KeyAction keyAction = new KeyAction();
ImageIcon background = new ImageIcon(".//source/background.png");
JLabel label = new JLabel(background);
public Layout(){
super("Teris version 1.000");
this.setBounds( 60, 100, 800, 600);
this.setTitle("Teris Version 1.00");
this.setLayout(null);
startButton = new JButton(new ImageIcon(Path(0)));//set startButton background
startButton.setBounds(getRectangle(0));//set startButton size & position
this.add(startButton);//add startButton in this panel
startButton.addFocusListener(this);//add focus listener for startButton
aboutButton = new JButton(new ImageIcon(Path(2)));//set aboutButton background
aboutButton.setBounds(getRectangle(2));//set aboutButton size & position
this.add(aboutButton);//add aboutButton in this panel
aboutButton.addFocusListener(this);//add focus listener for startButton
settingButton = new JButton(new ImageIcon(Path(4)));//set settingButton background
settingButton.setBounds(getRectangle(4));//set settingButton size & position
this.add(settingButton);//add settingButton in this panel
settingButton.addFocusListener(this);//add focus listener for settingButton
helpButton = new JButton(new ImageIcon(Path(6)));//set helpButton background
helpButton.setBounds(getRectangle(6));//set helpButton size & position
this.add(helpButton);//add helpButton in this panel
helpButton.addFocusListener(this);//add focus listener for helpButton
exitButton = new JButton(new ImageIcon(Path(8)));//set exitButton background
exitButton.setBounds(getRectangle(8));//set exitButton size & position
add(exitButton);//add exitButton in this panel
exitButton.addFocusListener(this);//add focus listener for exitButton
label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
this.add(label);
this.addKeyListener(keyAction);
this.setVisible(true);
this.selectMenu();
}
public String Path(int i){
String path[] = {
".//src/startUnfocus.png",
".//src/startFocus.png",
".//src/aboutUnfocus.png",
".//src/aboutFocus.png",
".//src/settingUnfocus.png",
".//src/settingFocus.png",
".//src/helpUnfocus.png",
".//src/helpFocus.png",
".//src/exitUnfocus.png",
".//src/exitFocus.png"
};
return path[i];
}
//according image width & height to setting menu button position & size
public Rectangle getRectangle(int i){
File picture[] = new File [10];
Rectangle pictureRectangle[] = new Rectangle[10];
BufferedImage sourceImage[] = new BufferedImage[10];
try{
for(int j = 0; j < 10; j++){
picture[j] = new File(Path(j));
sourceImage[j] = ImageIO.read(new FileInputStream(picture[j]));
pictureRectangle[j] = new Rectangle((this.getWidth() - sourceImage[j].getWidth()) / 2,
(int)(j / 2) * this.getHeight() / 6 + 30, sourceImage[j].getWidth(), sourceImage[j].getHeight());
}
return pictureRectangle[i];
}
catch(IOException e){
e.printStackTrace();
return null;
}
}
public void focusGained(FocusEvent e) {
if(e.getSource() == startButton){
startButton.setIcon(new ImageIcon(Path(1)));
keyAction.menuIndex = 0;
}
if(e.getSource() == aboutButton){
aboutButton.setIcon(new ImageIcon(Path(3)));
keyAction.menuIndex = 1;
}
if(e.getSource() == settingButton){
settingButton.setIcon(new ImageIcon(Path(5)));
keyAction.menuIndex = 2;
}
if(e.getSource() == helpButton){
helpButton.setIcon(new ImageIcon(Path(7)));
keyAction.menuIndex = 3;
}
if(e.getSource() == exitButton){
exitButton.setIcon(new ImageIcon(Path(9)));
keyAction.menuIndex = 4;
}
System.out.println(keyAction.menuIndex);
}
public void focusLost(FocusEvent e) {
if(e.getSource() == startButton){
startButton.setIcon(new ImageIcon(Path(0)));
}
if(e.getSource() == aboutButton){
aboutButton.setIcon(new ImageIcon(Path(2)));
}
if(e.getSource() == settingButton){
settingButton.setIcon(new ImageIcon(Path(4)));
}
if(e.getSource() == helpButton){
helpButton.setIcon(new ImageIcon(Path(6)));
}
if(e.getSource() == exitButton){
exitButton.setIcon(new ImageIcon(Path(8)));
}
}
public void selectMenu(){
switch(keyAction.menuIndex){
case 0:
startButton.requestFocus(true);
break;
case 1:
aboutButton.requestFocus(true);
break;
case 2:
settingButton.requestFocus(true);
break;
case 3:
helpButton.requestFocus(true);
break;
case 4:
exitButton.requestFocus(true);
break;
}
}
}