?
Solved

Dynamic setSize()

Posted on 2005-03-10
5
Medium Priority
?
434 Views
Last Modified: 2008-02-26
Hi Experts!

I need help with passing parameters to setSize() function. The frame width should be the width of the text string + font ascent; the frame height should be the height of the text string + font ascent. The problem is that I initialise the text string parameters only in paintComponent method, so when I use Clock constructor they are still null. The text string and the font point will be different each time.

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Clock extends JPanel
{
private int fontPoint = 30;
private int fontAscent;
private int hour, minute, second;
private int strWidth, strHeight;
private String time;

public Clock()
{
   setCurrentTime();
   time = getHour() + ":" + getMinute() + ":" + getSecond();
      
  JFrame frame = new JFrame("Clock");    
  setBackground(Color.WHITE);                
  setFont(new Font("SansSerif", Font.PLAIN, fontPoint));       
  frame.getContentPane().add(this);       
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
  frame.setSize(getPreferredSize());       
  frame.setVisible(true);

}

 protected void paintComponent(Graphics g)

 {
    super.paintComponent(g);
    FontMetrics fm = g.getFontMetrics();
    fontAscent = fm.getAscent();
    strWidth = fm.stringWidth(time);
    strHeight = fm.getHeight();
   
    int xCoordinate = fontAscent/2;
    int yCoordinate = strHeight/2 + fontAscent/2;
   
    g.drawString(time, xCoordinate, yCoordinate);    
       
    g.dispose();
 }

public void setCurrentTime()
{
    // Construct a calendar for the current date and time
    Calendar calendar = new GregorianCalendar();

    // Set current hour, minute and second
    this.hour = calendar.get(Calendar.HOUR_OF_DAY);
    this.minute = calendar.get(Calendar.MINUTE);
    this.second = calendar.get(Calendar.SECOND);
}

 /** Return hour */
  public int getHour() {
    return hour;
  }
 
  /** Return minute */
  public int getMinute() {
    return minute;
  }
 
  /** Return second */
  public int getSecond() {
    return second;
  }

 public Dimension getPreferredSize()
 {       
    return new Dimension(strWidth + fontAscent, strHeight + fontAscent);
 }

public static void main(String[] argv)
{
      new Clock();
      
}
      
}
0
Comment
Question by:njava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13514533
change  ur constructor and GetPreferredSize methods with below methods

public Clock()
{
   setCurrentTime();
   time = getHour() + ":" + getMinute() + ":" + getSecond();
     
  JFrame frame = new JFrame("Clock");    
  setBackground(Color.WHITE);              
  setFont(new Font("SansSerif", Font.PLAIN, fontPoint));      
  frame.getContentPane().add(this);      
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  frame.setVisible(true);
 frame.setSize(getPreferredSize());      


}

public Dimension getPreferredSize()
{
      Graphics g=getGraphics();

      FontMetrics fm = g.getFontMetrics();
    fontAscent = fm.getAscent();
    strWidth = fm.stringWidth(time);
    strHeight = fm.getHeight();
      Dimension d=new Dimension();
      d.width=strWidth+fontAscent;
      d.height=fontAscent+strHeight;

      return d;

}
0
 
LVL 1

Author Comment

by:njava
ID: 13514600
It resizes but the frame is now grayish. The text string appears only if I stretch the frame.
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 400 total points
ID: 13514841
In that case this should help you

import javax.swing.* ;
import java.awt.* ;
import java.util.* ;

public class Clock
    extends JPanel
{
  private int fontPoint = 30 ;

  private int fontAscent ;

  private int hour , minute , second ;

  private int strWidth , strHeight ;

  private String time ;

  JFrame frame = new JFrame ( "Clock" ) ;

  public Clock ()
  {
    setCurrentTime () ;
    time = getHour () + ":" + getMinute () + ":" + getSecond () ;
    setBackground ( Color.WHITE ) ;
    setFont ( new Font ( "SansSerif" , Font.PLAIN , fontPoint ) ) ;
    frame.getContentPane ().add ( this ) ;
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
    frame.setVisible ( true ) ;
    frame.pack () ;
  }

  public Dimension getPreferredSize ()
  {
    Graphics g = getGraphics () ;
    FontMetrics fm = g.getFontMetrics () ;
    fontAscent = fm.getAscent () ;
    strWidth = fm.stringWidth ( time ) ;
    strHeight = fm.getHeight () ;
    Dimension d = new Dimension () ;
    d.width = strWidth + fontAscent ;
    d.height = fontAscent + strHeight ;
    int xCoordinate = fontAscent / 2 ;
    int yCoordinate = strHeight / 2 + fontAscent / 2 ;
    g.drawString ( time , xCoordinate , yCoordinate ) ;
    return d ;
  }

  protected void paintComponent ( Graphics g )
  {
    frame.setSize ( getPreferredSize () ) ;
    super.paintComponent ( g ) ;
    FontMetrics fm = g.getFontMetrics () ;
    fontAscent = fm.getAscent () ;
    strWidth = fm.stringWidth ( time ) ;
    strHeight = fm.getHeight () ;
    int xCoordinate = fontAscent / 2 ;
    int yCoordinate = strHeight / 2 + fontAscent / 2 ;
    g.drawString ( time , xCoordinate , yCoordinate ) ;
    g.dispose () ;
  }

  public void setCurrentTime ()
  {
    // Construct a calendar for the current date and time
    Calendar calendar = new GregorianCalendar () ;
    // Set current hour, minute and second
    this.hour = calendar.get ( Calendar.HOUR_OF_DAY ) ;
    this.minute = calendar.get ( Calendar.MINUTE ) ;
    this.second = calendar.get ( Calendar.SECOND ) ;
  }

  /** Return hour */
  public int getHour ()
  {
    return hour ;
  }

  /** Return minute */
  public int getMinute ()
  {
    return minute ;
  }

  /** Return second */
  public int getSecond ()
  {
    return second ;
  }

  public static void main ( String[] argv )
  {
    new Clock () ;
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13514892
Wouldn't it be a whole lot easier just to display the time in a JLabel?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13514911
Yeah, may be he/she wants to work with Graphics object
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question