Results 1 to 2 of 2
  1. #1
    Isong is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default What's wrong with my code?

    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.text.DateFormatSymbols;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    import javax.swing.*;
    
    public class tester
    {
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				FontFrame frame = new FontFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    
    class FontFrame extends JFrame
    {
    	public FontFrame()
    	{
    		setTitle("This week (2010)");
    		setSize(500,250);
    		
    		CalendarComponent component = new CalendarComponent();
    		add(component);	
    	}
    }
    
    class CalendarComponent extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		
    		GregorianCalendar d = new GregorianCalendar();
    		int today = d.get(Calendar.DAY_OF_MONTH);
    		int week = d.get(Calendar.WEEK_OF_MONTH);
    		int month = d.get(Calendar.MONTH);
    		
    		String dates = "";
    		String message = "";
    		
    		d.set(Calendar.DAY_OF_WEEK,1);
    
    		//print weekday name
    		String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    		
    		for (int i=0; i < 8; i++)
    		{
    			dates = dates + weekdayNames[i] + " ";
    		}
    		
    		Font f = new Font("Courier", Font.BOLD, 23);
    		g2.setFont(f);
    		
    		FontRenderContext context = g2.getFontRenderContext();
    		Rectangle2D bounds = f.getStringBounds(dates, context);
    		
    		double x = (getWidth() - bounds.getWidth())/2;
    		double y = (getHeight() - bounds.getHeight())/2;
    		
    		//add ascent to y to reach the baseline
    		double ascent = -bounds.getY();
    		double baseY = y + ascent;
    		
    		g2.drawString(dates, (int) x, (int) baseY);
    		
    		do
    		{
    		//print day
    		int day = d.get(Calendar.DAY_OF_MONTH);
    		int weekday = d.get(Calendar.DAY_OF_WEEK);
    		int firstDayOfWeek = d.getFirstDayOfWeek();
    		
    		message = message + day;
    		
    		//marks current date with *
    		if(day == today) message = message +"*";
    		
    		if (day == day) message = message +"| ";
    		
    		if (weekday == firstDayOfWeek) message = message + "\n";
    		
    		//advance to the next day
    		d.add(Calendar.DAY_OF_WEEK,1);
    		
    		}
    		while (d.get(Calendar.MONTH) == month);
    		
    		//measure the size of the message
    		bounds = f.getStringBounds(message, context);
    		
    		//set (x,y) = top-left of the text
    		
    		x = (getWidth() - bounds.getWidth())/2;
    		y = (getHeight() - bounds.getHeight())/2;
    		
    		//add ascent to y to reach the baseline
    		
    		ascent = -bounds.getY();
    		baseY = y + ascent;
    		
    		//draw the message
    		
    		g2.drawString(message, (int) x, (int) baseY+35);
    	}
    }
    Trying to make my program output something like this,



    however, I can't get the string to move to the next line for every week.
    Also, the week ends a day late.
    for example, the first week ends on the 7th, the second week on the 14th, and the third week on the 21st.

  2. #2
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

Similar Threads

  1. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 11:28 AM
  2. Please tell me what i am doing wrong in this code
    By Basit781 in forum CLDC and MIDP
    Replies: 11
    Last Post: 05-20-2010, 05:08 PM
  3. What is wrong with my code?!
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 08:01 PM
  4. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •