0

I have the clock displaying correctly, but I need to write my name in the middle of it and I can't figure out how. Also how to make the hour and minute hands change every time the program runs? I assume I need to make the hour and minute equal to a random number generator?

class StillClock extends JPanel 
{ 
   private int hour; 
   private int minute;
   private int second; 

   private boolean hourHandVisible = true; 
   private boolean minuteHandVisible = true;
   private boolean secondHandVisible = true; 


/** Construct a default clock with the current time*/ 
   public StillClock() 
   { 
      setCurrentTime(); 

   } 


/** Construct a clock with specified hour, minute, and second */ 
   public StillClock(int hour, int minute)
   { 
   this.hour = hour; 
   this.minute = minute;
   //this.second = second; 
   } 


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


/** Set a new hour */ 
   public void setHour(int hour) 
   { 
   this.hour = hour; 
   repaint(); 
   } 

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


/** Set a new minute */ 
   public void setMinute(int minute)   
   { 
   this.minute = minute; 
   repaint(); 
   } 

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

   public void setSecond(int second)
   {
   this.second = second;
   repaint();
   }*/

/** Draw the clock */ 
   protected void paintComponent(Graphics g) 
   { 
   super.paintComponent(g); 

// Initialize clock parameters 
   int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5); 
   int xCenter = getWidth() / 2; 
   int yCenter = getHeight() / 2; 

// Draw circle 
   g.setColor(Color.black); 
   g.drawOval(xCenter - clockRadius, yCenter - clockRadius,2 * clockRadius, 2 * clockRadius); 

// Draw minute hand 
   if (minuteHandVisible) 
   { 
   int mLength = (int)(clockRadius * 0.65); 
   int xMinute = (int)(xCenter + mLength *Math.sin(minute * (2 * Math.PI / 60))); 
   int yMinute = (int)(yCenter - mLength *Math.cos(minute * (2 * Math.PI / 60))); 
   g.setColor(Color. blue);
   g.drawLine(xCenter, yCenter, xMinute, yMinute); 
   } 

// Draw hour hand 
   if (hourHandVisible) 
   { 
   int hLength = (int)(clockRadius * 0.5); 
   int xHour = (int)(xCenter + hLength *Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); 
   int yHour = (int)(yCenter - hLength *Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); 
   g.setColor(Color.green); 
   g.drawLine(xCenter, yCenter, xHour, yHour); 
   } 

// }// Display hours on the clock 
   g.setColor(Color.red); 
   for (int i = 0; i < 12; i++) 
   { 
    int x = (int)(xCenter + 0.8 * clockRadius * Math.sin (i*(2 * Math.PI / 12))); 
    int y = (int)(yCenter - 0.8 * clockRadius * Math.cos (i*(2 * Math.PI / 12))); 
    g.drawString("" + ((i == 0) ? 12 : i), x, y); 
   } 

} 

   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); 
   } 


   public Dimension getPreferredSize() 
   { 
   return new Dimension(800, 800); 
   } 

   public boolean isHourHandVisible() 
   { 
   return hourHandVisible; 
   } 

   public boolean isMinuteHandVisible() 
   { 
   return hourHandVisible; 
   } 

   public void setHourHandVisible(boolean hourHandVisible) 
   { 
   this.hourHandVisible = hourHandVisible; 
   repaint(); 
   } 

   public void setMinuteHandVisible(boolean minuteHandVisible) 
   { 
   this.minuteHandVisible = minuteHandVisible; 
   repaint(); 
   } 

} 
Johannes H.
  • 5,875
  • 1
  • 20
  • 40
Rickie
  • 5
  • 6

2 Answers2

1

Start by taking a look at Working with Text APIs

But the basics would be something along the lines of

String text = "You're Name Here";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);

For a comparison of the concept of "centring text vertically", take a look at this Previous Question

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try Adding this to the bottom of your paintComponent()

// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();

// find the center location to display
int stringWidth = fm.stringWidth("MyName");
int stringAccent = fm.getAscent();

// get the position of the leftmost character in the baseline
int xCoordinate = getWidth() / 2 - stringWidth / 2;
int yCoordinate = getHeight() / 2 + stringAccent / 2;

g.drawString("MyName", xCoordinate, yCoordinate);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720