Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Painting Clock, Overlaps the current String display

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Painting Clock, Overlaps the current String display

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.util.Calendar;
    import javax.swing.*;
     
    public class MyClock extends JPanel implements Runnable {
     
        Thread runner;
     
        public MyClock() {
     
            setDoubleBuffered(true);
            setFocusable(true);
            setLayout(null);
        }
     
        public void paintComponent(Graphics g) {
     
            Calendar now = Calendar.getInstance();
            int hrs = now.get(Calendar.HOUR_OF_DAY);
            int min = now.get(Calendar.MINUTE);
            int sec = now.get(Calendar.SECOND);
     
            String time = zero(hrs) + ":" + zero(min) + ":" + zero(sec);
     
            g.drawString(time, 60, 40);
     
            repaint();
        }
     
        public String zero(int num) {
     
            String number = (num < 10) ? ("0" + num) : ("" + num);
            return number;                                   
        }
     
        public void start() {
     
            if (runner == null) {
     
                runner = new Thread(this);
            }
     
            runner.start();
        }
     
        public void run() {
     
            while (runner == Thread.currentThread()) {
     
                try {
     
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
     
                    System.out.println("Thread failed");
                }
            }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame();
            f.setSize(400, 300);
            f.add(new MyClock());
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }

    i just gode this code from the web, with a little bit of modification, i need to implement a digital clock for another project.. i just cant figure out why does the next painting overlaps the current one.. any help please.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Painting Clock, Overlaps the current String display

    by the way.. my goal is.. to create a separate class as a Clock, but extends as a JPanel not a JFrame, i just want to add/call it to any JFrame window i have or in any JPanel/Container

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Painting Clock, Overlaps the current String display

    1- Don't just throw code together you get from the internet. Take the time to really understand what's going on. You'll save yourself a ton of headaches. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    2- Don't call repaint() from a paint method, including paintComponent(). Use a Swing Timer instead, otherwise you're going to be constantly drawing, which is really going to eat up a lot of resources.

    3- Your zero() method is doing the job of a DecimalFormat instance.

    4- JPanel's paintComponent() method redraws the background, which in effect clears out any previously drawn frames. Start your paintComponent() method with a call to super.paintComponent() in order to maintain that functionality.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (October 6th, 2011)

Similar Threads

  1. How to detect mouse event while painting in a loop
    By r9reen in forum AWT / Java Swing
    Replies: 6
    Last Post: March 1st, 2011, 01:58 AM
  2. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  3. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. help with clock class
    By collinsislee in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:53 PM