GT-R Register - Official Nissan Skyline and GTR Owners Club forum - View Single Post - Java help please
View Single Post
Old 31st October 2008, 01:00 PM   #3 (permalink)
moz
moz has no status.
GTR Register Member
 
Join Date: Mar 2005
Location: Finland
Cars owned: R32 GT-R
Posts: 281
This works, just the formatting of the output needs sorting from the DisplayCalendar() method, I'm not to great with formatting. I'm sure you'll manage

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calendar4 implements ActionListener
{
	private JFrame outputFrame;
	private Container container;
	private JLabel yearLabel,  monthLabel;
	private JTextField inputYear;
	private JButton myButton,  calcButton;
	private ButtonGroup myButtonGroup;
	private JTextArea myTextArea;
	private JComboBox inputMonth;
	String days[] =
	{
		"Su ", " Mo ", " Tu ", " We ", " Th ", " Fr ", " Sa "
	};
	String months[] =
	{
		"January", "February", "March", "April",
		"May", "June", "July", "August", "September",
		"October", "November", "December"
	};
	int DaysInMonth[] =
	{
		31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	};
	
	protected String[][] calendar = 
	{ 
		{ " ", " ", " ", " ", " ", " ", " " },
		{ " ", " ", " ", " ", " ", " ", " " },
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	}
	};
	
	public static void main(String[] args)
	{
		new Calendar4();
	}

	public Calendar4()
	{
		outputFrame = new JFrame();
		outputFrame.setSize(500, 400);
		outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		container = outputFrame.getContentPane();

		container.setLayout(new FlowLayout());

		container.setBackground(Color.RED);

		yearLabel = new JLabel("Enter Year (After 1900)");
		container.add(yearLabel);

		inputYear = new JTextField("", 10);
		container.add(inputYear);
		inputYear.addActionListener(this);

		monthLabel = new JLabel("Select Month");
		container.add(monthLabel);

		inputMonth = new JComboBox(months);

		container.add(inputMonth);
		inputMonth.setSelectedIndex(0);
		inputMonth.addActionListener(this);

		calcButton = new JButton("Press To Display Calendar");
		container.add(calcButton);
		calcButton.addActionListener(this);

		myTextArea = new JTextArea(10, 40);
		container.add(new JScrollPane(myTextArea));

		inputYear.setText("2008");

		outputFrame.setVisible(true);
	}

	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == calcButton)
		{
			myTextArea.setText("");

			if (!YearInputIsValid()) return;

			int userYear = Integer.parseInt(inputYear.getText(), 10);

			myTextArea.append(isLeapYear(userYear) ? "Is a leap year.\n" : "Is not a leap year.\n");
			fillCalendar(userYear, inputMonth.getSelectedIndex());
			DisplayCalendar();
		}
	}

	private void DisplayCalendar()
	{
		for (int i = 0; i < calendar.length; i++)
		{
			for (int j = 0; j < calendar[i].length; j++)
				myTextArea.append(calendar[i][j] + " ");
			myTextArea.append("\n");
		}
	}

	private boolean YearInputIsValid()
	{

		if (inputYear.getText().equals(""))
		{
			myTextArea.append("Please enter a valid year value.\n");
			return false;
		}

		try
		{
			Integer.parseInt(inputYear.getText(), 10);
		}
		catch (Exception e)
		{
			myTextArea.append("The entered values must consist only of numbers,\n");
			return false;
		}

		if (Integer.parseInt(inputYear.getText(), 10) < 1899)
		{
			myTextArea.append("Year must be 1900 or after\n");
			return false;
		}
		return true;
	}

	private void InitialiseCalendar()
	{
		for (int i = 0; i < calendar.length; i++)
		{
			for (int j = 0; j < calendar[i].length; j++)
				calendar[i][j] = " ";
		}
	}

	private void fillCalendar(int userYear, int userMonth)
	{
		InitialiseCalendar();
		int xNum = (CalcFirstOfMonth(userYear, userMonth));

		int yNum = 0;

		int numDays = DaysInMonth[userMonth] + ((isLeapYear(userYear) && (userMonth == 1)) ? 1 : 0);
		int day = 1;
		do
		{
			calendar[yNum][xNum] = String.valueOf(day);
			xNum++;
			if (xNum > 6)
			{
				xNum = 0;
				yNum += 1;
			}
			day++;
		} while (day <= numDays);
	}

	private int CalcFirstOfMonth(int userYear, int userMonth)
	{
		int startDay;

		if ((userMonth < 0) || (userMonth > 11))
		{
			return (-1);
		}
		startDay = CalcJanuaryFirst(userYear);

		for (int i = 0; i < userMonth; i++)
		{
			startDay += DaysInMonth[i];
		}
		if ((userMonth > 1) && isLeapYear(userYear))
		{
			startDay++;
		}
		return (startDay % 7);
	}

	private int CalcJanuaryFirst(int userYear)
	{
		return ((5 + (userYear - 1582) + CalcLeapYears(userYear)) % 7);
	}

	private int CalcLeapYears(int userYear)
	{
		int leapYears;
		int hundreds;
		int fourHundreds;

		leapYears = (userYear - 1581) / 4;
		hundreds = (userYear - 1501) / 100;
		leapYears -= hundreds;
		fourHundreds = (userYear - 1201) / 400;
		leapYears += fourHundreds;
		return (leapYears);
	}

	private boolean isLeapYear(int userYear)
	{
		if ((userYear % 4 == 0) && (userYear % 100 != 0) || (userYear % 400 == 0))
		{
			return true;
		}
		return false;
	}
}
Shamelessly butchered a little bit from here:
CalendarTable

Hope you don't mind the changes to some of your code, I get carried away sometimes Maybe you can just take what you need to fix the original code.
__________________
moz is offline   Reply With Quote