package straws;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.net.URL;
import java.util.Calendar;
import javax.imageio.*;
import javax.swing.*;
import java.util.GregorianCalendar;
public class LoadImageApp extends Component{
public void paint(Graphics g) {
//declare variables
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
System.out.println("Current Time : " + hour + ":"
+ minute + ":" + second + " " + am_pm);
//draw hour images
if(hour > 9){
g.drawImage(loadImage("1.jpg"), 0, 0, null);
}
else{
g.drawImage(loadImage("0.jpg"), 0, 0, null);
g.drawImage(loadImage(hour+".jpg"), 200, 0, null);
}
//get minute digits
if(minute>9){
String minute_string=Integer.toString(minute);
char[] minute_digits = minute_string.toCharArray();
int first_minute_digit = minute_digits[0]-48;
int second_minute_digit = minute_digits[1]-48;
//draws colin and minutes
g.drawImage(loadImage("11.jpg"), 400, 0, null);
g.drawImage(loadImage(first_minute_digit+".jpg"),400+13, 0, null);
g.drawImage(loadImage(second_minute_digit+".jpg"),600+13, 0, null);
//when minute is less than 10 the char array returns null for first digit
//and messes up the program, this fixes it
}else{
String minute_string=Integer.toString(minute);
char[] minute_digits = minute_string.toCharArray();
int second_minute_digit = minute_digits[0]-48;
g.drawImage(loadImage(0+".jpg"), 400+13, 0, null);
g.drawImage(loadImage(second_minute_digit+".jpg"), 600+13, 0, null);
}
}
public static BufferedImage loadImage(String name) {
String imgFileName = name;
URL url = LoadImageApp.class.getResource(imgFileName);
BufferedImage img = null;
try {
img = ImageIO.read(url);
} catch (Exception e) {
}
return img;
}
public Dimension getPreferredSize() {
return new Dimension(813, loadImage("5.jpg").getHeight(null));
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.add(new LoadImageApp());
f.pack();
}
}
help with my code (beginner)clock program
Page 1 of 1
5 Replies - 14003 Views - Last Post: 24 September 2010 - 08:02 AM
#1 Guest_hey*
help with my code (beginner)
Posted 23 September 2010 - 01:27 PM
Hi guys, Just so you know I am a java beginner. I have been trying to write a program which will display pictures that represent the current time of day. I got the program to work but I need to figure out how to get it to refresh every minute. I would like some suggestions along with critique of my code. Thanks for the help.
Replies To: help with my code (beginner)
#2
Re: help with my code (beginner)
Posted 23 September 2010 - 01:44 PM
This is one way you can do it:
were using the thread as our timer with a delay of 1 min.
public class LoadImageApp extends Component{
private static final int DELAY = 1000*60;
public LoadImageApp()
{
new Thread(new Runnable(){
public void run() {
while(true)
{
try {
Thread.sleep(DELAY);
refresh();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
});
}
private void refresh()
{
this.repaint();
}
}
were using the thread as our timer with a delay of 1 min.
This post has been edited by H3R3T1C: 23 September 2010 - 01:44 PM
#3 Guest_hey*
Re: help with my code (beginner)
Posted 23 September 2010 - 02:28 PM
I like your solution however I can't get it to work in my code. I am still only running through the paint method once.
#4
Re: help with my code (beginner)
Posted 23 September 2010 - 03:06 PM
As AWT Components are outdated and don't always work well with Swing JComponents, better to use Swing JComponents instead. You can quite easily use a Swing Timer to accomplish this, as Swing isn't Thread-safe.
Also, Dogstopper has a tutorial on Preloading Images with SwingWorker.
class MyPanel extends JPanel{
MyPanel(){
//invokes repaint() once/minute
Timer t = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e){
repaint();
}
});
t.start();
}
public void paint(Graphics g){
super.paint(g);
//code
}
}
Also, Dogstopper has a tutorial on Preloading Images with SwingWorker.
#5 Guest_hey*
Re: help with my code (beginner)
Posted 23 September 2010 - 08:36 PM
macosxnerd101 ,
Your solution worked beautifully. Thanks for the help. Your link was also very useful.
Your solution worked beautifully. Thanks for the help. Your link was also very useful.
#6
Re: help with my code (beginner)
Posted 24 September 2010 - 08:02 AM
Glad I could help!
Page 1 of 1

New Topic/Question
Reply
MultiQuote









|