Link to home
Start Free TrialLog in
Avatar of njava
njava

asked on

Dynamic setSize()

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();
      
}
      
}
Avatar of sudhakar_koundinya
sudhakar_koundinya

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;

}
Avatar of njava

ASKER

It resizes but the frame is now grayish. The text string appears only if I stretch the frame.
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
Wouldn't it be a whole lot easier just to display the time in a JLabel?
Yeah, may be he/she wants to work with Graphics object