あるサイトに載っていたプログラムを移しつつ理解しようと思っていたのですが、MainPanel.javaをコンパイルしようとすると、prepareComponentsのcheckHPcondition=new Timer(5,myActionListener);の部分でコンストラクターTimer(int,MainPanel.MyActionListener)は未定義ですと出てきました。どこを直せば良いのかわからないので、教えていただきたいです。環境はeclipseです
package sample07_1_ProgressBar;
public class Main {
static MainWindow mainWindow;
public static void main(String[] args) {
mainWindow=new MainWindow(); //ウインドウを生成
mainWindow.preparePanels(); //painに直接貼るパネルを生成
mainWindow.prepareComponents(); //その他のコンポーネントを生成
mainWindow.setFrontScreenAndFocus(ScreenMode.MAIN);
//起動後最初に表示する画像を手前に持ってくる
mainWindow.setVisible(true); //最後にウインドウを可視化
}
}
package sample07_1_ProgressBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.management.timer.Timer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainPanel extends JPanel{
private static final long serialVersionUID=1L;
//定数フィールド
Color backgroundColor=Color.green;
final int MAXHP=243;
final int DAMAGE=43;
//コンポーネント
JPanel hpPanel;
JPanel mainPanel;
JPanel buttonPanel;
JLabel mainLabel;
ImageIcon icon01;
ImageIcon icon02;
ImageIcon icon03;
ImageIcon icon04;
JButton button;
JLabel label;
MyProgressBar hpBar;
Timer checkHPcondition;//hpバーを監視(waitingやmaxなど)
//リスナー
MyActionListener myActionListener;//ボタンと現在hpを監視
//コンストラクタ
MainPanel(){
//パネルサイズと貼り付け位置の設定は不要(勝手に画面サイズに合わせてくれる)
this.setLayout(new BorderLayout());//レイアウト設定
this.setBackground(backgroundColor);//背景の色
//メインパネルに貼るパネルを生成
hpPanel=new JPanel();
mainPanel=new JPanel();
buttonPanel=new JPanel();
hpPanel.setBackground(Color.cyan);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.black);
this.add(hpPanel,BorderLayout.NORTH);
this.add(mainPanel,BorderLayout.CENTER);
this.add(buttonPanel,BorderLayout.SOUTH);
}
public void prepareComponents() {
//中央の画像を準備
mainLabel=new JLabel();
icon01=new ImageIcon(getClass().getClassLoader().getResource("yuusya_game.png"));
icon02=new ImageIcon(getClass().getClassLoader().getResource("images.jpeg"));
icon03=new ImageIcon(getClass().getClassLoader().getResource("images-2.jpeg"));
icon04=new ImageIcon(getClass().getClassLoader().getResource("Unknown.jpeg"));
mainLabel.setIcon(icon01);
mainPanel.add(mainLabel);
//ボタン
button=new JButton();
button.setText("勇者を攻撃(HPを"+DAMAGE+"減らす)");
button.setFocusable(false);
myActionListener=new MyActionListener();
button.addActionListener(myActionListener);
buttonPanel.add(button);
//パネル内にプログレスバーを表示(制限時間用)
hpBar=new MyProgressBar("HP",MAXHP);
checkHPcondition=new Timer(5,myActionListener);
checkHPcondition.start();
hpPanel.add(hpBar);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//ボタン用
if(e.getSource()==button) {
if(mainLabel.getIcon()==icon01) {
hpBar.minus(DAMAGE);
mainLabel.setIcon(icon02);
button.setEnabled(false);
button.setText("勇者に"+DAMAGE+"のダメージ!");
}else if(mainLabel.getIcon()==icon03) {
hpBar.plus(MAXHP);
mainLabel.setIcon(icon04);
button.setEnabled(false);
button.setText("蘇生中...");
}
}
//hpの状態を監視
if(e.getSource()==checkHPcondition) {
if(hpBar.mode==MyProgressBar.Mode.max) {
mainLabel.setIcon(icon01);
button.setEnabled(true);
button.setText("勇者を攻撃(HPを"+DAMAGE+"減らす");
hpBar.mode=MyProgressBar.Mode.waiting;
}else if(hpBar.mode==MyProgressBar.Mode.zero) {
mainLabel.setIcon(icon03);
button.setEnabled(true);
button.setText("勇者を蘇生");
hpBar.mode=MyProgressBar.Mode.waiting;
}else if(hpBar.mode==MyProgressBar.Mode.middle) {
mainLabel.setIcon(icon01);
button.setEnabled(true);
button.setText("勇者を攻撃(HPを"+DAMAGE+"減らす)");
hpBar.mode=MyProgressBar.Mode.waiting;
}
}
}
}
}
package sample07_1_ProgressBar;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class MainWindow extends JFrame{
private static final long serialVersionUID=1L;
//フィールド
ScreenMode screenMode=ScreenMode.MAIN;
//定数
final int WIDTH=300; //フレームの幅
final int HEIGHT=350; //フレームの高さ
//レイアウト(紙芝居のような設定用)
CardLayout layout=new CardLayout();
//コンポーネント
MainPanel mainPanel;
//コンストラクタ
MainWindow(){
//ウインドウ左上のアイコンとタイトル
this.setTitle("ラスボスごっこ");
ImageIcon icon=new ImageIcon(getClass().getClassLoader().getResource("yuusya_game.png"));
this.setIconImage(icon.getImage());
//いつもの
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false); //画面サイズの変更を禁止
this.getContentPane().setBackground(Color.green); //背景の色
//this.getContentPane().setBackground(new Color(0xF6F6F6)); //細かく設定するときはこれ
this.setLayout(layout); //紙芝居のように設定
this.setPreferredSize(new Dimension(WIDTH,HEIGHT)); //サイズ設定
this.pack(); //自動サイズ調整 これがないと変なサイズになる
this.setLocationRelativeTo(null); //起動後のスクリーンの位置を中央に pack()より後に呼ぶ
//this.setLocation(450,50); //細かく設定するときはこっち
}
//コンストラクタの後に呼び出す
public void preparePanels() {
//パネルの準備
mainPanel=new MainPanel();
this.add(mainPanel,"メイン画面");
this.pack();
}
public void prepareComponents() {
mainPanel.prepareComponents();
}
//スクリーンモード切り替え
public void setFrontScreenAndFocus(ScreenMode s) {
screenMode=s;
//表示する画面の設定
switch(screenMode) {
case MAIN:
layout.show(this.getContentPane(),"メイン画面");
mainPanel.requestFocus();
break;
}
}
}
package sample07_1_ProgressBar;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class MyProgressBar extends JProgressBar implements ActionListener{
private static final long serialVersionUID=1L;
String name;
int max;
int now;
int hold01;
int hold02;
Timer timer;
Mode mode=Mode.waiting;
public enum Mode{
waiting,max,middle,zero,increasing,decreasing;
//waitingはボタンを押せる状態(基本)increasing/decreasingはゲージの処理中
//増減が終わった時のhpに応じてmax/middle/zeroの状態に変化、その後waitingにする
}
//コンストラクタ
MyProgressBar(String str,int maximum){
name=str;
max=maximum;
now=max;
//色の設定
this.setForeground(Color.green);
this.setBackground(Color.white);
this.setUI(new BasicProgressBarUI(){
protected Color getSelectionForeground() {
return Color.black;//文字の色(前)
}
protected Color getSelectionBackground() {
return Color.gray;//(後)
}
});
//値と文字の設定
this.setValue(100);//パーセントを引数に入力
this.updateText();
this.setFont(new Font("MV BOli",Font.BOLD,15));
this.setStringPainted(true);
timer=new Timer(10,this);
}
//文字列更新メソッド
//hpの表示を担当
public void updateText() {
this.setString(name+":"+now+"/"+max);
}
//値を1減らすメソッド
//actionPerformedから呼ばれる
public void minusOne() {
now-=1;
this.setValue(now*100/max);//あまりのある割り算
this.updateText();
}
//1つ増やすメソッド
public void plusOne() {
now+=1;
this.setValue(now*100/max);
this.updateText();
}
//値を減らすメソッド
//これを呼ぶとタイマーが作動
//引数で指定した分だけ繰り返しマイナスメソッドを呼び続ける
public void minus(int num) {
hold01=num;
hold02=now-num;
this.mode=Mode.decreasing;
this.timer.start();
}
//値を増やすメソッド
public void plus(int num) {
hold01=num;
hold02=now+num;
this.mode=Mode.increasing;
this.timer.start();
}
//オーバーライド
//タイマーによって10ミリ秒ごとに呼ばれる
public void actionPerfomed(ActionEvent e) {
switch(this.mode){
case decreasing:
if(this.now>this.hold02) {
this.minusOne();
}else {
this.timer.stop();
this.mode=Mode.middle;
}
if(this.now<=max/2) {
this.setForeground(Color.yellow);
}
if(this.now<=max/4) {
this.setForeground(Color.red);
}
if(this.now<=0) {
this.timer.stop();
this.mode=Mode.zero;
}
break;
case increasing:
if(this.now<this.hold02) {
this.plusOne();
}else {
this.timer.stop();
this.mode=Mode.middle;
}
if(this.now>=max/4) {
this.setForeground(Color.yellow);
}
if(this.now>=max/2) {
this.setForeground(Color.green);
}
if(this.now>=this.max) {
this.timer.stop();
this.mode=Mode.max;
}
break;
default:
}
}
}
package sample07_1_ProgressBar;
public enum ScreenMode {
//画面のモードはとりあえず一つだけ
MAIN,
}