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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.setSize(getPreferred Size());
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.MINU TE);
this.second = calendar.get(Calendar.SECO ND);
}
/** 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();
}
}
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
frame.setDefaultCloseOpera
frame.setSize(getPreferred
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
this.minute = calendar.get(Calendar.MINU
this.second = calendar.get(Calendar.SECO
}
/** 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();
}
}
ASKER
It resizes but the frame is now grayish. The text string appears only if I stretch the frame.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
frame.setDefaultCloseOpera
frame.setVisible(true);
frame.setSize(getPreferred
}
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+fontAscen
d.height=fontAscent+strHei
return d;
}