[Challenge] GUI Timer (ADV)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 17349 Views - Last Post: 28 January 2013 - 10:46 AM

#16 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: [Challenge] GUI Timer (ADV)

Posted 11 December 2012 - 05:00 PM

One question: has anyone even thought about using an applet for this? The repaint() method would sure come in handy. I would do this, but I don't know enough about KeyListener yet.
Was This Post Helpful? 1
  • +
  • -

#17 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: [Challenge] GUI Timer (ADV)

Posted 10 January 2013 - 02:27 AM

What you think about making that with processing, I guess it will be very easy with that. How it says:

Quote

Processing is an open source programming language and environment for people who want to create images, animations, and interactions.

Here is link to processing official web-page.
Was This Post Helpful? 0
  • +
  • -

#18 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: [Challenge] GUI Timer (ADV)

Posted 26 January 2013 - 07:36 AM

I made tutorial about that and tried to post it, but it was not accepted. That's why I have posted about that in my blog. Same idea: making it using processing.
Was This Post Helpful? 0
  • +
  • -

#19 b.szabolcs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 04-February 12

Re: [Challenge] GUI Timer (ADV)

Posted 28 January 2013 - 10:46 AM

As I like to solve Rubik cubes I found it necessary to make my own stopwatch. So here it is:
/***
 * A simple representation of a stopwatch.
 * 
 * @author Szabolcs Besenyei
 * 
 */
public class Stopwatch {
	private boolean running;
	private long startingTime;
	private long elapsedTime;

	public Stopwatch() {
		running = false;
	}

	private long getSystemTime() {
		return System.currentTimeMillis();
	}

	public boolean isRunning() {
		return running;
	}
	
	public long calculateElapsedTime() {
		return getSystemTime() - startingTime;
	}

	public long getStartingTime() {
		return startingTime;
	}

	public long getElapsedTime() {
		return elapsedTime;
	}

	public void stopTimer() {
		if (running) {
			running = false;
			elapsedTime = calculateElapsedTime();
		}
	}

	public void startTimer() {
		final int inspectionTime = 0;
		startingTime = getSystemTime() + inspectionTime;
		running = true;
	}

}




And here is the GUI (I made a Save session / Clear session function to it):
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class WatchFrame extends JFrame {

	private JPanel contentPanel;
	private JTextField txtBestValue;
	private JTextField txtAvgFiveValue;
	private JTextField txtAvgTwelveValue;
	private Stopwatch stopwatch;
	private JTextArea txtResult;
	private List<Long> resultList;
	private JLabel lblTimer;
	private Long bestTime = Long.MAX_VALUE;
	
	private Timer timer = new Timer(50, new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			updateTimer();			
		}
	});
	
 
	private final KeyListener myKeyListener = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			
			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
				if (stopwatch.isRunning()) {
					stopwatch.stopTimer();
					timer.stop();
					updateTimer();
					resultList.add(stopwatch.getElapsedTime());
					txtResult.append(convertResult(stopwatch.getElapsedTime()) + "\n");
					updateBestTime();
					updateAvg(5);
					updateAvg(12);
				} else {
					stopwatch.startTimer();
					timer.start();
				}
			}

		}

	};
	
	
	private String convertResult(long result) {
		// 00:00.000
		return String.format("%02d:%02d.%03d", result / 60000, (result / 1000) % 60, result % 1000);
	}
	
	private void updateBestTime() {
		for (Long result : resultList) {
			if (result < bestTime) {
				bestTime = result;
			}
		}		
		txtBestValue.setText(convertResult(bestTime));
	}
	
	private void updateAvg(int n) {		
		if (resultList.size() >= n) {
			// sort the resultList, pick up the best N results, avg it			
			List<Long> avgList = new ArrayList<>(n);
			Collections.sort(resultList);
			for(int i = 0; i < n; i++) {
				avgList.add(resultList.get(i));
			}
			
			Long sumOfList = 0L;
			for(Long l : resultList) {
				sumOfList += l;
			}
			Long avgOfN = sumOfList / n;
			
			switch(n) {
			case 5:
				txtAvgFiveValue.setText(convertResult(avgOfN));
				break;
			case 12:
				txtAvgTwelveValue.setText(convertResult(avgOfN));
				break;
			default:
				break;
			}
		}
	}

	private void updateTimer() {
		lblTimer.setText(convertResult(stopwatch.calculateElapsedTime()));
	}

	/**
	 * Create the frame.
	 */
	public WatchFrame() {
		setTitle("3-by-3 Rubik Stopwatch");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 454, 371);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		final JFrame frameRef = this;
		JMenuItem mntmSaveSession = new JMenuItem("Save session");
		mntmSaveSession.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileDialog fileDialog = new FileDialog(frameRef, "Save", FileDialog.SAVE);
				fileDialog.setVisible(true);
				try(FileWriter fw = new FileWriter(fileDialog.getFile())) {
					for(Long l : resultList) {
						fw.write(convertResult(l) + "\n");
					}
				} catch (IOException ioe) {
					// TODO Auto-generated catch block
					ioe.printStackTrace();
				}
				
			}
		});
		
		mnFile.add(mntmSaveSession);
		
		JMenuItem mntmClearSession = new JMenuItem("Clear session");
		mntmClearSession.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resultList.clear();
				txtAvgFiveValue.setText("--");
				txtAvgTwelveValue.setText("--");
				txtBestValue.setText("--");
				txtResult.setText("");
				lblTimer.setText("00:00.000");
			}
		});
		mnFile.add(mntmClearSession);
		
		contentPanel = new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPanel);
		contentPanel.setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel = new JLabel("Rubik's Timer");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		contentPanel.add(lblNewLabel, BorderLayout.NORTH);

		JPanel averagePanel = new JPanel();
		contentPanel.add(averagePanel, BorderLayout.SOUTH);
		averagePanel.setLayout(new GridLayout(3, 2, 10, 10));

		JLabel lblBestTime = new JLabel("Best time:");
		lblBestTime.setHorizontalAlignment(SwingConstants.CENTER);
		averagePanel.add(lblBestTime);

		txtBestValue = new JTextField();
		txtBestValue.setEditable(false);
		averagePanel.add(txtBestValue);
		txtBestValue.setText("--");
		txtBestValue.setColumns(10);

		JLabel lblAvgOf = new JLabel("Avg. of 5:");
		lblAvgOf.setHorizontalAlignment(SwingConstants.CENTER);
		averagePanel.add(lblAvgOf);

		txtAvgFiveValue = new JTextField();
		txtAvgFiveValue.setEditable(false);
		txtAvgFiveValue.setText("--");
		averagePanel.add(txtAvgFiveValue);
		txtAvgFiveValue.setColumns(10);

		JLabel lblAvgOf_1 = new JLabel("Avg. of 12:");
		lblAvgOf_1.setHorizontalAlignment(SwingConstants.CENTER);
		averagePanel.add(lblAvgOf_1);

		txtAvgTwelveValue = new JTextField();
		txtAvgTwelveValue.setEditable(false);
		txtAvgTwelveValue.setText("--");
		averagePanel.add(txtAvgTwelveValue);
		txtAvgTwelveValue.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		contentPanel.add(scrollPane, BorderLayout.WEST);
		contentPanel.setFocusable(true);
		contentPanel.addKeyListener(myKeyListener);

		txtResult = new JTextArea();
		txtResult.setColumns(10);
		txtResult.setEditable(false);
		txtResult.setFont(new Font("Tahoma", Font.PLAIN, 11));
		scrollPane.setViewportView(txtResult);
		
		lblTimer = new JLabel("00:00.00");
		lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
		lblTimer.setFont(lblTimer.getFont().deriveFont(36f));
		contentPanel.add(lblTimer, BorderLayout.CENTER);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("cube.png"));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		contentPanel.add(label, BorderLayout.EAST);

		stopwatch = new Stopwatch();
		
		resultList = new ArrayList<>();
	}


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WatchFrame frame = new WatchFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}



Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2