-1

I was given this homework assignment by my professor to implement two buttons and control a fan. I am very new to GUI's and I have not the slightest clue how to call upon my timer in order to increment it using timer.setDelay(); Any help would be greatly appreciated. My code is below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Fan extends JFrame implements ActionListener{

    JButton speedup;
    JButton slowdown;


 JPanel testPanel = new MyPanel();//trying to inherit properties of MyPanel
  public Fan() {
    add(testPanel);

GridLayout f = new GridLayout(1,2);
setLayout(f);

JButton speedup = new JButton("Speed Up");
speedup.addActionListener(this);
add(speedup);

JButton slowdown = new JButton("Slow Down");
slowdown.addActionListener(this);
add(slowdown);

}

/* public void actionPerformed(ActionEvent event){
    int delay;
    String cmd = event.getActionCommand();
    if(cmd == "Speed Up" ){

        delay = testPanel.getTimer().getDelay();
        delay++;
        testPanel.getTimer().setDelay(delay);
        }

    else{

         delay = testPanel.getTimer().getDelay();
        delay--;
        testPanel.getTimer().setDelay(delay);
        */ 
//My attempt at getting timer to work commented out


    }




public class MyPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    public Timer timer = new Timer(10, new TimerListener());
    private int alpha = 0; //angle

    public Timer getTimer(){
        return timer; //getter method for timer
    }

    public MyPanel() {
        timer.start();
    }


        protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        alpha = alpha + 1;
        int xc = getWidth()/2;
        int yc = getHeight()/2;
        int rad = (int)(Math.min(getWidth(), getHeight())*0.4);
        int x = xc - rad;
        int y = yc - rad;
        g.fillArc(x, y, 2*rad, 2*rad, 0+alpha, 30);
        g.fillArc(x, y, 2*rad, 2*rad, 90+alpha, 30);
        g.fillArc(x, y, 2*rad, 2*rad, 180+alpha, 30);
        g.fillArc(x, y, 2*rad, 2*rad, 270+alpha, 30);
    }

    class TimerListener implements ActionListener {     

        public void actionPerformed(ActionEvent e){
            repaint();      
        }       
    }   





    public static void main(String[] args) {

        JFrame fan = new Fan();
        fan.setSize(700, 700);
        fan.setLocationRelativeTo(null);
        fan.setDefaultCloseOperation(EXIT_ON_CLOSE);
        fan.setTitle("Spinning Fan");
        fan.setVisible(true);
    }

}

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user25467
  • 3
  • 2

1 Answers1

3
  1. JPanel testPanel = new MyPanel(); will limit testPanel to the methods of JPanel (so you can't use testPanel.getTimer()). Instead use MyPanel testPanel = new MyPanel();, then you will be able to use testTimer.getTimer();

  2. if(cmd == "Speed Up" ){. Don't compare Strings with ==. Instead use equals. So if ("Speed Up".equals(cmd)) {}

  3. If you want to "Speed up" an animation, you should decrease the delay, not increase it. And vice verse.


Side Notes

  • Run Swings apps on the Event Dispatch Thread. You can achieve this by wrapping the code in your main in a SwingUtilities.invokeLater(...). See more at Initial Threads

Here's a fixed example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Fan extends JFrame implements ActionListener {

    JButton speedup;
    JButton slowdown;

    MyPanel testPanel = new MyPanel();// trying to inherit properties of MyPanel

    public Fan() {
        add(testPanel);

        JButton speedup = new JButton("Speed Up");
        speedup.addActionListener(this);

        JButton slowdown = new JButton("Slow Down");
        slowdown.addActionListener(this);

        JPanel panel = new JPanel();
        panel.add(speedup);
        panel.add(slowdown);

        add(panel, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Spinning Fan");
        setVisible(true);

    }

    public void actionPerformed(ActionEvent event) {
        int delay;
        String cmd = event.getActionCommand();
        if ("Speed Up".equals(cmd)) {
            delay = testPanel.getTimer().getDelay();
            delay--;
            testPanel.getTimer().setDelay(delay);
        } else {
            delay = testPanel.getTimer().getDelay();
            delay++;
            testPanel.getTimer().setDelay(delay);
        }
    }

    // My attempt at getting timer to work commented out

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Fan();
            }
        });
    }
}

class MyPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    public Timer timer = new Timer(10, new TimerListener());
    private int alpha = 0; // angle

    public Timer getTimer() {
        return timer; // getter method for timer
    }

    public MyPanel() {
        timer.start();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        alpha = alpha + 1;
        int xc = getWidth() / 2;
        int yc = getHeight() / 2;
        int rad = (int) (Math.min(getWidth(), getHeight()) * 0.4);
        int x = xc - rad;
        int y = yc - rad;
        g.fillArc(x, y, 2 * rad, 2 * rad, 0 + alpha, 30);
        g.fillArc(x, y, 2 * rad, 2 * rad, 90 + alpha, 30);
        g.fillArc(x, y, 2 * rad, 2 * rad, 180 + alpha, 30);
        g.fillArc(x, y, 2 * rad, 2 * rad, 270 + alpha, 30);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 600);
    }

    class TimerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720