Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was working on my uni project (Digital Clock) when I noticed that it gets really buggy.

I'm using NetBeans (as a requirement) and there is a LOT of code. I'm not that good as well, so I don't know ways that could optimise it. So could you guys suggest any "shorter" versions of code to use, maybe cut things somewhere where it is not needed, because right now it gets to the point where program almost freezes my computer.

Also don't suggest things like, alternative methods to display, calculate etc.. as I'm following requirements of uni and can't change "core" idea

Code is to long to fit here, so I uploaded it to a host, code is available here: http://www.sendspace.com/file/52becr

share|improve this question
1  
We need to have code to review posted with the question. They link you provided could disappear some day and this question will then become useless for others. If it is too long, post parts of it at a time in different questions. – Jeff Vanzella Nov 9 '12 at 17:03

closed as off topic by Jeff Vanzella, palacsint, Corbin, Trevor Pilley, Brian Reichle Nov 10 '12 at 2:16

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

up vote 0 down vote accepted

One big thing is you are creating new ImageIcon objects every time your clock updates the numbers:

if(ones == 0)
{
    oneSecsDisplay.setIcon(new javax.swing.ImageIcon("0.jpg");
}
// ...etc.

I'm not sure how they're being displayed on the actual frame since it's hard to read NetBeans Matisse code (you have a lot of text fields with their default names). I would highly recommend loading all of those images one time and storing them in a map:

private Map<Integer, ImageIcon> digitMap = new HashMap<>();

// ... later
private void loadImages() 
{
    digitMap.put(0, new ImageIcon("0.jpg");
    // ...etc
}

// ... later
if(ones == 0)
{
    oneSecsDisplay.setIcon(digitMap.get(0));
}

All that aside, you should really be modeling the clock in a class separate from the JFrame. Though I'm not aware of the limitations of your project (like whether or not you're allowed to take advantage of java.util.Date and java.util.Calendar):

Calendar gc = new GregorianCalendar();
System.out.println(gc.get(Calendar.SECOND));  // prints n seconds
gc.add(Calendar.SECOND, 1); // increment the calendar's time by 1 second
System.out.println(gc.get(Calendar.SECOND));  // prints n + 1 seconds

Then you can write some helper methods to translate the digits of the dates into the images for the frame (or display the time directly in whatever format you need).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.