###疑問、問題
前回の質問でいろいろしったのでまた新しく質問を書きなおさせていただきます。また、コードを全文のせた方がいいとのご指摘もあったのでそうさせていただきます。
問題ですが、Controller classにおいて追加させていただいたtimerがうまく機能せず点滅させることができない状況です。キーボードから該当のボタンへの入力があった際それがあったとわかるように画像を変化させたい、今回その一つとして点滅をえらびました。どこが原因で噛み合わないのか見当がつかないのですがわかる方はいらっしゃいますでしょうか。どうぞよろしくお願い致します。
###ソースコード
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import javax.swing.Timer; import java.awt.*; import java.awt.geom.Ellipse2D; import javax.swing.*; import java.util.*; import java.applet.*; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.InputEvent; class RoundButton extends JButton {//ボタンに貼り付けた画像の一部を透過させるクラス(ここは問題ないはずです) protected Shape shape; protected Shape base; public RoundButton(Icon icon){ super(icon); setModel(new DefaultButtonModel()); setBackground(Color.BLACK); setContentAreaFilled(false); setFocusPainted(false); setBorderPainted(false); setAlignmentY(Component.TOP_ALIGNMENT); initShape(); } protected void initShape(){ if(!getBounds().equals(base)){ Dimension s = getPreferredSize(); base = getBounds(); shape = new Ellipse2D.Float(0,0,s.width,s.height); } } @Override public Dimension getPreferredSize(){ Icon icon = getIcon(); Insets i = getInsets(); int iw = Math.max(icon.getIconWidth(), icon.getIconHeight()); return new Dimension(iw+i.right+i.left,iw+i.top+i.bottom); } @Override public boolean contains(int x,int y){ initShape(); return super.contains(x,y); } } //------------------------------------------------------ class Controller extends WindowAdapter implements KeyListener {//ここが問題のクラス点滅が起きない private Model model; Timer timer; RoundButton flushButton; Color defaultColor = Color.GRAY; Color flushColor = Color.RED; boolean flushFlag; public Controller(Model model){ this.model=model; timer = new Timer(1000, this::expired); timer.setRepeats(true); } public void keyTyped(KeyEvent actionEvent){ char c= actionEvent.getKeyChar(); switch(c){ case'a': model.snea1(); break; case'q': model.symbal2(); break; case'f': model.tam1(); break; case'g': model.tam2(); break; case'h': model.tam3(); break; case'b': model.bass(); break; case'p': model.crush(); break; case'l': model.hihat(); break; } if (actionEvent.getSource() instanceof RoundButton) { flushButton((RoundButton)actionEvent.getSource()); } } public void keyPressed(KeyEvent actionEvent){ } public void keyReleased(KeyEvent actionEvent){ } public void flushButton(RoundButton button) { timer.stop(); if (flushButton != null) { flushButton.setBackground(defaultColor); } flushButton = button; timer.start(); } void expired(ActionEvent ev/*actionEventでもだめだした*/) { flushFlag = !flushFlag; flushButton.setBackground(flushFlag ? defaultColor : flushColor); flushButton.repaint(); } } //---------------------------------------------------------------------- class Model extends Observable{//mvcにおけるmodelclass private AudioClip pong; public void snea1(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void symbal2(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void tam1(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void tam2(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void tam3(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void bass(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void crush(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } public void hihat(){ pong = Applet.newAudioClip(getClass().getResource("音声ファイル")); pong.play(); } } //--------------------------------------------------------- class View5 extends JFrame{//mvcにおけるviewclass public View5(Controller aController){ this.setSize(750,440); JPanel p =new JPanel(); p.setLayout(null); ImageIcon icon1 = new ImageIcon("画像ファイル"); RoundButton snea1=new RoundButton(icon1); snea1.setBounds(50, 250, 150, 150); ImageIcon icon2 = new ImageIcon("画像ファイル"); RoundButton symbal2=new RoundButton(icon2); symbal2.setBounds(0, 0, 180, 180); ImageIcon icon3 = new ImageIcon("画像ファイル"); RoundButton tam1=new RoundButton(icon3); tam1.setBounds(180, 100, 130,130); ImageIcon icon4 = new ImageIcon("画像ファイル"); RoundButton tam2=new RoundButton(icon4); tam2.setBounds(310, 50, 130,130); ImageIcon icon5 = new ImageIcon("画像ファイル"); RoundButton tam3=new RoundButton(icon5); tam3.setBounds(440, 100, 130, 130); ImageIcon icon6 = new ImageIcon("画像ファイル"); RoundButton bass=new RoundButton(icon6); bass.setBounds(285, 230, 180, 180); ImageIcon icon7 = new ImageIcon("画像ファイル"); RoundButton crush=new RoundButton(icon7); crush.setBounds(570, 0, 180, 180); ImageIcon icon8 = new ImageIcon("画像ファイル"); RoundButton hihat=new RoundButton(icon8); hihat.setBounds(500, 230, 180, 180); p.setOpaque(false); snea1.addKeyListener(aController); symbal2.addKeyListener(aController); tam1.addKeyListener(aController); tam2.addKeyListener(aController); tam3.addKeyListener(aController); bass.addKeyListener(aController); crush.addKeyListener(aController); hihat.addKeyListener(aController); snea1.setActionCommand("snea1"); symbal2.setActionCommand("symbal2"); tam1.setActionCommand("tam1"); tam2.setActionCommand("tam2"); tam3.setActionCommand("tam3"); bass.setActionCommand("bass"); crush.setActionCommand("crush"); hihat.setActionCommand("hihat"); p.add(snea1); p.add(symbal2); p.add(tam1); p.add(tam2); p.add(tam3); p.add(bass); p.add(crush); p.add(hihat); this.setFocusable(true); this.addWindowListener(aController); getContentPane().add(p, BorderLayout.CENTER); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ new View5(new Controller(new Model())); } }
上のプログラムはmvcモデルで構築しています。
###追加の質問
timer = new Timer(1000,this::expired);
のところですが、調べてみて"::"の使い方は理解できたのですが、コンパイルがここだけできないです。エラー文は
View5.java:63: エラー: ')'がありません timer = new Timer(1000,this::expired); ^ View5.java:63: エラー: 式の開始が不正です timer = new Timer(1000,this::expired); ^ View5.java:63: エラー: ';'がありません timer = new Timer(1000,this::expired);
となっています。javaのバージョンの問題でしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/16 06:20
2016/12/16 06:41 編集
2016/12/18 11:17
2016/12/18 11:37
2016/12/18 12:37