The Artima Developer Community
Sponsored Link

Java Answers Forum
Help Needed

3 replies on 1 page. Most recent reply: Nov 15, 2006 12:24 PM by Tanvir Hossain

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Help Needed Posted: Nov 12, 2006 3:28 PM
Reply to this message Reply
Advertisement
Could anybody pls pls tell me what is the problem with my following code which is a simple clock(no compile error but doesnt show the time)-
import java.util.*;
import java.text.DateFormat;
import java.awt.*;
import java.awt.geom.*;
public class Clock extends Frame implements Runnable
{

public void run() {
Thread myThread = Thread.currentThread();
while (myThread !=null) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){

}
}
}
public void paint(Graphics g) {
// get the time and convert it to a date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// format it and display it
DateFormat dateFormatter = DateFormat.getTimeInstance();
g.drawString(dateFormatter.format(date), 5, 10);
}
//public void update(Graphics g)
//{ paint(g) ; }

Clock(){

setBounds(300,200,200,200);
setVisible(true);

}
public static void main(String str[])
{
Clock myclock=new Clock();
Thread myThread=new Thread(myclock);
myThread.start();
}

}


how to use repaint method in java console programe(not in applet).when to use repaint and when to use update method and why. could u pls write me a prorgame which has a button that draw something upon being pressed on the frame or just give hints how to do it


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Needed Posted: Nov 15, 2006 7:24 AM
Reply to this message Reply
First I separated Thread and Frame, because in my Opinion sending the Thread to sleep also causes that the frame is not painted. Don't know if this is right or wrong, because It didn't work anyway.

So I came up with the idea not to paint directly into the frame, but into a panel displayed into the frame and that worked. Here's my code:
/*
 * Clock.java
 *
 * Created on 13. November 2006, 12:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
package keylogger;
 
/**
 *
 * @author Neumi
 */
import java.util.*;
import java.text.DateFormat;
import java.awt.*;
import java.awt.geom.*;
public class Clock extends Frame{
    
    public Clock() {
        setBounds(300,200,200,200);
        setLayout(new java.awt.BorderLayout());
        myPanel = new ClockPanel();
        add(myPanel, java.awt.BorderLayout.CENTER);
        setVisible(true);
        new ClockSetter(this).start();
    }
    
    public void displayTime() {
        myPanel.repaint();
    }
 
    public static void main(String str[]) {
        Clock myclock = new Clock();
    }
 
    private ClockPanel myPanel;
 
    private static class ClockSetter extends Thread {
        private Clock clockFrame;
        boolean doRunThread = true;
 
        public ClockSetter (Clock clockFrame) {
            this.clockFrame = clockFrame;
        }
        public void run() {
            doRunThread = true;
            while (doRunThread) {
                clockFrame.displayTime();
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted");
                    System.out.println(e.getMessage());
                }
            }
        }
 
        public void stopThread() {
            doRunThread = false;
            notify();
        }
   }
    
    private class ClockPanel extends java.awt.Panel {
        public ClockPanel() {
            super();
        }
        public void paint(Graphics g) {
            super.paint(g);
            // get the time and convert it to a date
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            // format it and display it
            DateFormat dateFormatter = DateFormat.getTimeInstance();
            g.drawString(dateFormatter.format(date), 5, 10);
        }
    }
    
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Needed Posted: Nov 15, 2006 7:26 AM
Reply to this message Reply
Don't mind the package, I just put the code into the same dir as the code for a keyboard wedge barcode scanner test tool.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Help Needed Posted: Nov 15, 2006 12:24 PM
Reply to this message Reply
U r so helpful ,very very very THNX

Flat View: This topic has 3 replies on 1 page
Topic: Help Needed Previous Topic   Next Topic Topic: is it possible to add JScrollPane/Bar to JPanel and painting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use