18 Replies - 6952 Views - Last Post: 28 January 2013 - 10:46 AM
#1
[Challenge] GUI Timer (ADV)
Posted 25 August 2012 - 05:26 PM
The challenge is to make a GUI timer which uses <space> to start and stop.
It should show minutes, seconds, and miliseconds.
You'll be able to look at a Average of 5 times, and 12 times.
Maybe a option for a maximum of 10 second inspection time before the actually timer starts.
I'm not setting a limite of lines. But Try to make it as small as possible. And we'll see who'll make the best one.
Let the programming BEGIN!
Replies To: [Challenge] GUI Timer (ADV)
#2
Re: [Challenge] GUI Timer (ADV)
Posted 01 September 2012 - 08:10 AM
Also I'm kinda confused about the challenge. Is it a stopwatch? And what is the average thing about?
But I will just leave it here, lets see if it gets deleted
It is some old code I once made to help some guy
Edit: removed code until a mod accepts this isn't homework
This post has been edited by CasiOo: 01 September 2012 - 08:12 AM
#3
Re: [Challenge] GUI Timer (ADV)
Posted 01 September 2012 - 01:05 PM
CasiOo, on 01 September 2012 - 08:10 AM, said:
Also I'm kinda confused about the challenge. Is it a stopwatch? And what is the average thing about?
But I will just leave it here, lets see if it gets deleted
It is some old code I once made to help some guy
Edit: removed code until a mod accepts this isn't homework
It's not homework, I'm going into programming school in about 3 years. So don't worry.
#4
Re: [Challenge] GUI Timer (ADV)
Posted 01 September 2012 - 06:02 PM
CasiOo, on 01 September 2012 - 11:10 AM, said:
Also I'm kinda confused about the challenge. Is it a stopwatch? And what is the average thing about?
But I will just leave it here, lets see if it gets deleted
It is some old code I once made to help some guy
Edit: removed code until a mod accepts this isn't homework
I approved it. This challenge seems a little simplistic, but I figured it would give newbies something to work on. Honestly, use of a Timer or Thread.sleep() would make this task rather trivial.
#5
Re: [Challenge] GUI Timer (ADV)
Posted 25 September 2012 - 06:45 PM
import javax.swing.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.*;
import java.awt.event.*;
public class StopWatch {
int m = 0;
int s = 0;
int ms = 0;
JFrame frame;
JPanel panelR;
JPanel panelL;
JTextField timerTextField;
JButton stop;
JButton start;
JTextArea timeList;
JLabel average5;
JLabel average12;
Timer timer;
public static void main(String[] args) {
StopWatch sw = new StopWatch();
sw.makeGUI();
}
///////////////////////////////////////////////////////////////////////
public void makeGUI(){
frame = new JFrame("StopWatch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelL = new JPanel();
panelL.setLayout(new BoxLayout(panelL, BoxLayout.Y_AXIS));
timerTextField = new JTextField(m+":"+s+","+ms);
timerTextField.setEditable(false);
timeList = new JTextArea();
timeList.setEditable(false);
panelL.add(timerTextField);
panelL.add(timeList);
panelR = new JPanel();
panelR.setLayout(new BoxLayout(panelR, BoxLayout.Y_AXIS));
start = new JButton("Start");
start.addActionListener(new startButtonListener());
panelR.add(start);
stop = new JButton("Stop");
stop.addActionListener(new stopButtonListener());
panelR.add(stop);
stop.setEnabled(false);
JLabel label5avg = new JLabel("Last 5 avg: ");
panelR.add(label5avg);
average5 = new JLabel(avg5m+":"+avg5s+","+avg5ms);
panelR.add(average5);
JLabel label12avg = new JLabel("Last 12 avg:");
panelR.add(label12avg);
average12 = new JLabel(avg12m+":"+avg12s+","+avg12ms);
panelR.add(average12);
//////KEYBOARD
panelL.setFocusable(true);
panelL.requestFocus(true);
panelL.addKeyListener(new SpaceListener());
start.setFocusable(false);
stop.setFocusable(false);
//////KEYBOARD
frame.getContentPane().add(BorderLayout.CENTER, panelL);
frame.getContentPane().add(BorderLayout.EAST,panelR);
frame.setSize(235, 200);
frame.setVisible(true);
}
///////////////////////////////////////////////////////////////////////
ArrayList<Integer> msList = new ArrayList<Integer>();
ArrayList<Integer> sList = new ArrayList<Integer>();
ArrayList<Integer> mList = new ArrayList<Integer>();
int sum5ms, sum5s, sum5m,
avg5ms, avg5s, avg5m,
sum12ms, sum12s, sum12m,
avg12ms, avg12s, avg12m;
public void avgTimes(){
sum5ms = 0; sum12ms = 0;
sum5s = 0; sum12s = 0;
sum5m = 0; sum12m = 0;
avg5ms = 0; avg12ms = 0;
avg5s = 0; avg12s = 0;
avg5m = 0; avg12m = 0;
for(int x = 0; x < msList.size() && x<5; x++){
sum5ms += msList.get(msList.size()-(x+1));
sum5s +=sList.get(sList.size()-(x+1));
sum5m +=mList.get(mList.size()-(x+1));
avg5ms = sum5ms / (x+1);
avg5s = sum5s / (x+1);
avg5m = sum5m /(x+1);
}
average5.setText(avg5m+":"+avg5s+","+avg5ms);
for(int x = 0; x<msList.size() && x<12; x++){
sum12ms += msList.get(msList.size()-(x+1));
sum12s +=sList.get(sList.size()-(x+1));
sum12m +=mList.get(mList.size()-(x+1));
avg12ms = sum12ms / (x+1);
avg12s = sum12s / (x+1);
avg12m = sum12m /(x+1);
}
average12.setText(avg12m+":"+avg12s+","+avg12ms);
}
///////////////////////////////////////////////////////////////////////
boolean isTimerOn;
public void startTimer(){
isTimerOn = true;
start.setEnabled(false);
stop.setEnabled(true);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
ms++;
if(ms==1000){
s++;
ms=0;
}
if(s==60){
m++;
s=0;
}
timerTextField.setText(m+":"+s+","+ms);
}
},0 ,1);
}
///////////////////////////////////////////////////////////////////////
public void stopTimer(){
timer.cancel();
timeList.insert(m+":"+s+","+ms+"\n",0);
mList.add(m);
sList.add(s);
msList.add(ms);
avgTimes();
m = 0;
s = 0;
ms = 0;
timerTextField.setText(m+":"+s+","+ms);
isTimerOn = false;
start.setEnabled(true);
stop.setEnabled(false);
}
//////////////////LISTENERS//////////////////////////////////////////////
class startButtonListener implements ActionListener{
public void actionPerformed(ActionEvent a){
startTimer();
}
}
class stopButtonListener implements ActionListener{
public void actionPerformed(ActionEvent a){
stopTimer();
}
}
class SpaceListener implements KeyListener{
public void keyPressed(KeyEvent VK_SPACE){
}
public void keyTyped(KeyEvent VK_SPACE){
if(isTimerOn){
stop.doClick();
}
else
start.doClick();
}
public void keyReleased(KeyEvent VK_SPACE){
}
}
}
That's my first reply for challenge so if I did something wrong - I'm sorry.
For me as a novice, hardest was getting average values of last 5 and 12 times...I spend some time on it
I would be very thankful if somebody could give me some advices - what I did wrong or what I could do better, etc.
Greetings.
#6
Re: [Challenge] GUI Timer (ADV)
Posted 26 September 2012 - 10:58 AM
johnml, on 25 September 2012 - 09:45 PM, said:
-snip code-
That's my first reply for challenge so if I did something wrong - I'm sorry.
For me as a novice, hardest was getting average values of last 5 and 12 times...I spend some time on it
I would be very thankful if somebody could give me some advices - what I did wrong or what I could do better, etc.
Greetings.
Your SpaceListener would probably work for any button pressed. You have to make sure the event is VK_SPACE and you can't do that by naming the parameter with that name. The name of the parameter is arbitrary.
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE) { ... }
}
Other than that, not a bad go. Good job!
#7
Re: [Challenge] GUI Timer (ADV)
Posted 26 September 2012 - 02:50 PM
#8
Re: [Challenge] GUI Timer (ADV)
Posted 30 September 2012 - 10:08 AM
If anyone wants to make my example look pretty, then go ahead
Shouldn't be too hard, I even made a JComponent for the watch to allow every kind of customization
import java.awt.*;
import javax.swing.*;
public class Stopwatch extends JFrame {
public Stopwatch() {
this.setTitle("Stopwatch");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.lightGray);
AverageView average = new AverageView();
WatchView watch = new WatchView(average);
add(watch, BorderLayout.CENTER);
add(average, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
new Stopwatch();
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JPanel;
public class WatchView extends JPanel implements ActionListener {
private final int delay = 16;
private Timer timer;
private WatchComponent watch;
private JButton start;
private JButton stop;
private boolean running;
private AverageView averageView;
public WatchView(AverageView averageView) {
super(new BorderLayout());
this.averageView = averageView;
watch = new WatchComponent();
start = new JButton("Start");
stop = new JButton("Stop");
start.addActionListener(this);
stop.addActionListener(this);
add(watch, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(start);
buttonPanel.add(stop);
add(buttonPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (!running) {
running = true;
watch.reset();
timer = new Timer(true);
timer.scheduleAtFixedRate(new MyTimerTask(), 0, delay);
}
}
else {
if (running) {
running = false;
timer.cancel();
logTime();
}
}
}
private void logTime() {
averageView.addRun(watch.getTime());
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
watch.update(delay);
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
public class WatchComponent extends JComponent {
private long elapsedTime;
private long hours, minutes, seconds, millies;
private Font font;
public WatchComponent() {
super();
setPreferredSize(new Dimension(200, 40));
font = new Font("Arial", Font.PLAIN, 16);
}
public void update(long time) {
elapsedTime += time;
updateTime();
repaint();
}
private void updateTime() {
long totalSeconds = elapsedTime / 1000;
hours = totalSeconds / 3600;
minutes = (totalSeconds % 3600) / 60;
seconds = totalSeconds % 60;
millies = elapsedTime % 1000;
}
public void reset() {
elapsedTime = 0;
repaint();
}
public String getTime() {
return String.format("%02d:%02d:%02d:%03d", hours, minutes, seconds, millies);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
String time = getTime();
Graphics2D gx = (Graphics2D) g;
gx.setFont(font);
FontMetrics fontMetrics = gx.getFontMetrics();
Rectangle2D bounds = fontMetrics.getStringBounds(time, gx);
int startX = (int) (getWidth() / 2 - bounds.getWidth() / 2);
int startY = (int) (getHeight() / 2 + bounds.getHeight() / 2);
gx.drawString(time, startX, startY);
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
public class AverageView extends JPanel {
private JList<String> list;
private DefaultListModel<String> listModel = new DefaultListModel<String>();
public AverageView() {
super(new BorderLayout());
setPreferredSize(new Dimension(200, 200));
list = new JList<String>(listModel);
add(list, BorderLayout.CENTER);
}
public void addRun(String run) {
listModel.addElement(run);
}
}
#9
Re: [Challenge] GUI Timer (ADV)
Posted 31 October 2012 - 09:28 AM
johnml, on 25 September 2012 - 06:45 PM, said:
That's my first reply for challenge so if I did something wrong - I'm sorry.
For me as a novice, hardest was getting average values of last 5 and 12 times...I spend some time on it
I would be very thankful if somebody could give me some advices - what I did wrong or what I could do better, etc.
Greetings.
I Have staticized the Whole Code.
My Question is : What is the use of OOP, when you can do it all in Procedural Code ?
import javax.swing.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.*;
import java.awt.event.*;
public class StopWatch {
static int m = 0;
static int s = 0;
static int ms = 0;
JFrame frame;
JPanel panelR;
JPanel panelL;
static JTextField timerTextField;
static JButton stop;
static JButton start;
static JTextArea timeList;
static JLabel average5;
static JLabel average12;
static Timer timer;
public static void main(String[] args) {
StopWatch sw = new StopWatch();
sw.makeGUI();
}
///////////////////////////////////////////////////////////////////////
public void makeGUI(){
frame = new JFrame("StopWatch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelL = new JPanel();
panelL.setLayout(new BoxLayout(panelL, BoxLayout.Y_AXIS));
timerTextField = new JTextField(m+":"+s+","+ms);
timerTextField.setEditable(false);
timeList = new JTextArea();
timeList.setEditable(false);
panelL.add(timerTextField);
panelL.add(timeList);
panelR = new JPanel();
panelR.setLayout(new BoxLayout(panelR, BoxLayout.Y_AXIS));
start = new JButton("Start");
start.addActionListener(new startButtonListener());
panelR.add(start);
stop = new JButton("Stop");
stop.addActionListener(new stopButtonListener());
panelR.add(stop);
stop.setEnabled(false);
JLabel label5avg = new JLabel("Last 5 avg: ");
panelR.add(label5avg);
average5 = new JLabel(avg5m+":"+avg5s+","+avg5ms);
panelR.add(average5);
JLabel label12avg = new JLabel("Last 12 avg:");
panelR.add(label12avg);
average12 = new JLabel(avg12m+":"+avg12s+","+avg12ms);
panelR.add(average12);
//////KEYBOARD
panelL.setFocusable(true);
panelL.requestFocus(true);
panelL.addKeyListener(new SpaceListener());
start.setFocusable(false);
stop.setFocusable(false);
//////KEYBOARD
frame.getContentPane().add(BorderLayout.CENTER, panelL);
frame.getContentPane().add(BorderLayout.EAST,panelR);
frame.setSize(235, 200);
frame.setVisible(true);
}
///////////////////////////////////////////////////////////////////////
static ArrayList<Integer> msList = new ArrayList<Integer>();
static ArrayList<Integer> sList = new ArrayList<Integer>();
static ArrayList<Integer> mList = new ArrayList<Integer>();
static int sum5ms;
static int sum5s;
static int sum5m;
static int avg5ms;
static int avg5s;
static int avg5m;
static int sum12ms;
static int sum12s;
static int sum12m;
static int avg12ms;
static int avg12s;
static int avg12m;
public static void avgTimes(){
sum5ms = 0; sum12ms = 0;
sum5s = 0; sum12s = 0;
sum5m = 0; sum12m = 0;
avg5ms = 0; avg12ms = 0;
avg5s = 0; avg12s = 0;
avg5m = 0; avg12m = 0;
for(int x = 0; x < msList.size() && x<5; x++){
sum5ms += msList.get(msList.size()-(x+1));
sum5s +=sList.get(sList.size()-(x+1));
sum5m +=mList.get(mList.size()-(x+1));
avg5ms = sum5ms / (x+1);
avg5s = sum5s / (x+1);
avg5m = sum5m /(x+1);
}
average5.setText(avg5m+":"+avg5s+","+avg5ms);
for(int x = 0; x<msList.size() && x<12; x++){
sum12ms += msList.get(msList.size()-(x+1));
sum12s +=sList.get(sList.size()-(x+1));
sum12m +=mList.get(mList.size()-(x+1));
avg12ms = sum12ms / (x+1);
avg12s = sum12s / (x+1);
avg12m = sum12m /(x+1);
}
average12.setText(avg12m+":"+avg12s+","+avg12ms);
}
///////////////////////////////////////////////////////////////////////
static boolean isTimerOn;
public static void startTimer(){
isTimerOn = true;
start.setEnabled(false);
stop.setEnabled(true);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
ms++;
if(ms==1000){
s++;
ms=0;
}
if(s==60){
m++;
s=0;
}
timerTextField.setText(m+":"+s+","+ms);
}
},0 ,1);
}
///////////////////////////////////////////////////////////////////////
public static void stopTimer(){
timer.cancel();
timeList.insert(m+":"+s+","+ms+"\n",0);
mList.add(m);
sList.add(s);
msList.add(ms);
avgTimes();
m = 0;
s = 0;
ms = 0;
timerTextField.setText(m+":"+s+","+ms);
isTimerOn = false;
start.setEnabled(true);
stop.setEnabled(false);
}
class startButtonListener implements ActionListener{
public void actionPerformed(ActionEvent a){
StopWatch.startTimer();
}
}
class stopButtonListener implements ActionListener{
public void actionPerformed(ActionEvent a){
StopWatch.stopTimer();
}
}
static class SpaceListener implements KeyListener{
public void keyPressed(KeyEvent VK_SPACE){
}
public void keyTyped(KeyEvent VK_SPACE){
if(isTimerOn){
stop.doClick();
}
else
start.doClick();
}
public void keyReleased(KeyEvent VK_SPACE){
}
}
//////////////////LISTENERS//////////////////////////////////////////////
}
#10
Re: [Challenge] GUI Timer (ADV)
Posted 31 October 2012 - 05:31 PM
#11
Re: [Challenge] GUI Timer (ADV)
Posted 31 October 2012 - 05:46 PM
#12
Re: [Challenge] GUI Timer (ADV)
Posted 01 November 2012 - 12:18 PM
burakaltr, on 31 October 2012 - 05:28 PM, said:
I Have staticized the Whole Code.
My Question is : What is the use of OOP, when you can do it all in Procedural Code ?
[...]
In Procedural Code after staticizing it, main method would be like that?
public static void main(String[] args) {
StopWatch.makeGUI();
}
That's what You mean?
#13
Re: [Challenge] GUI Timer (ADV)
Posted 01 November 2012 - 03:26 PM
johnml, on 01 November 2012 - 07:18 PM, said:
burakaltr, on 31 October 2012 - 05:28 PM, said:
I Have staticized the Whole Code.
My Question is : What is the use of OOP, when you can do it all in Procedural Code ?
[...]
In Procedural Code after staticizing it, main method would be like that?
public static void main(String[] args) {
StopWatch.makeGUI();
}
That's what You mean?
I would not suggest you to take the static path he presented you for
What is the point of using an object oriented language if you make it all static?
#14
Re: [Challenge] GUI Timer (ADV)
Posted 01 November 2012 - 03:57 PM
#15
Re: [Challenge] GUI Timer (ADV)
Posted 08 November 2012 - 05:22 AM
|
|

New Topic/Question
Reply



MultiQuote







|