Google

Friday, May 30, 2008

Java JTree Stuff

How To Use Trees

IntiCas 013: Subject Table and ComboBox


Added Subject Table and Combobox.
To add teacher to the 3rd colum, select the appropriate cell then, select the teacher from the ComboBox. The teacher initials will automatically be inserted into the selected cell in the JTable.
Note that the ComboBox is initalized with the list of teacher intitials when the Subject Frame first loads.

To Do:
Will implement algorithm to allocate Subjects to Classroom.
Then will test the allocation algorithm.
If it works, I will add the Classroom Frame so that more classrooms may be added.

Thursday, May 29, 2008

Inticas 012: Entering Teachers via JTable



Above is Inticas 012.
Added capability of entering Teachers via JTable.
Just enter the teachers particulars directly into each cell.

Click on File/Save to save the Teacher data.
Also, when Teacher Frame is opened, it automatically reads the
TeacherSerialized.txt file and populates the JTable.

I also found out the JTable cannot accept integer values, only can
accept Strings. All number that is entered is entered as a String.
This is so, even though the default object value in each cell is Object.
As such, I had to modify the Teacher Class so that the maxTotal data type
is String instead of integers:



public class Teacher implements Serializable
{
private String name;
private String initials;
private boolean needBreak = false;
private int taughtToday;
private String maxTotal;

. . .

}



When extracting data from the cell, need to cast it to string:

//read table cells into Teacher Object
t[r]=new Teacher();
t[r].setName((String)teacherTable.getValueAt(r, 0));
t[r].setInitials((String)teacherTable.getValueAt(r, 1));
t[r].setmaxTotal((String)teacherTable.getValueAt(r, 2));



To Do:

Will add similar functionality for Entering Subjects using JTable and also
will add similar functionality for entering Classrooms using JTable.

Wednesday, May 28, 2008

IntiCas 011: Can add multiple Teachers

Added code to add multiple teachers.
For this POC (Proof of Concept), I hardcoded 2 teachers into the
Save menuitem in the Add Teacher Frame.

To Do:
Will make it possible to add teacher from the JTable itself.

IntiCas Algorithm 002



The above is version 002 of the IntiCas Algorithm.

Added:

break out of Subject Loop

so that once a Classroom slot has been assigned a Subject, we want to stop
iterating through the other Subjects. Instead, we want to break out to the
outer loop so that we can move on to the next Classroom slot.

Remember that we are doing a Simulation Scenario here and not yet doing the release version.
The simulation is a mini simplified version of a simple case where Inti has only 1 classroom with 3 teachers and 9 subjects and simple constraints.

Tuesday, May 27, 2008

Inticas 010: Added Subject Class

I realize that it is Subjects which is being assigned to Classroom Slots.
It is not the Teachers who are being assigned to Classrooms.

And I also realize that Teacher is a property of Subjects:

package inticas;
import java.io.Serializable;

public class Subject implements Serializable{
private String name;
private int maxhours;
private int hoursSoFar;
private String teacher;

public void setName(String n){
name=n;
}
public void setmaxHours(int h){
maxhours=h;
}
public void incrementHours(){
hoursSoFar++;
}

}


And so, I created the Subject class above.

To Do:
Add a Subject Table and implement Subject Adding functionality.

IntiCas Algorithm 001

Qualitative Description

When assigning classrooms, we assign Subjects to classroom slots.
We do not assign Teachers to classroom slots.
This is because, each subject must be taught 6 hours each week.
A subject should not exceed 2 hours each day. This is to prevent the same subject from being
held twice in a day.
When assigning subjects, check for Subject constraints and also Teacher constraints.
This is done by fetching the properties/attributes of each Subject as well as Teacher Objects.
If none of the constraints are violated, then assign the Subject to the slot. And immediately modify the relevant properties of the Subject object and also modify the relevant property of the Teacher object to indicate a change in property (eg, Teacher object has needBreak property after it has been assigned a 2 hour slot).


Algorithm





Inticas 009 : Added Teacher Table

Added Teacher Table.

It has an Edit menu with an Add menuitem.
When Add menuitem is clicked, the Table will be appended
with an additional row.

Grab Pixel Color in Java

http://www.rgagnon.com/javadetails/java-0257.html

Eject CD Tray in Java

This is how to eject a CD Tray in Java without using JNI.

From Michael Dunn :


class OpenCD
{
public OpenCD()
{
try
{
String driveLetter = javax.swing.JOptionPane.showInputDialog("Enter CD drive letter");
if(driveLetter != null)
{
java.io.File file = new java.io.File("OpenCD.vbs");
java.io.FileWriter fw = new java.io.FileWriter(file);
fw.write("CreateObject(\"Shell.Application\").NameSpace(17).ParseName(\""+driveLetter+"\" & \":\\\").InvokeVerb(\"e&ject\")");
fw.close();
Runtime.getRuntime().exec("WScript.exe openCD.vbs");
Thread.sleep(2000);
file.deleteOnExit();
}
}
catch(Exception e){e.printStackTrace();}
System.exit(0);
}

}



Call it as new OpenCD() from within static main.
It works.



This is better, it can eject and also close, from rgagnon:

class CDUtils {
private CDUtils() { }

public static void open(String drive) {
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
+ "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
+ drive + "\") \n"
+ "cd.Eject";
fw.write(vbs);
fw.close();
Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
// thanks to TrueJavaProgammer for the waitFor() tip!
// Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
// Thread.sleep(2000);
}
catch(Exception e){
e.printStackTrace();
}
}

public static void close(String drive) {
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new FileWriter(file);
// to close a CD, we need eject two times!
String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
+ "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
+ drive + "\") \n"
+ "cd.Eject \n "
+ "cd.Eject ";
fw.write(vbs);
fw.close();
Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
// thanks to TrueJavaProgammer for the waitFor() tip!
// Runtime.getRuntime().exec("wscript "+ file.getPath());
// Thread.sleep(2000);
}
catch(Exception e){
e.printStackTrace();
}
}

}


Call the static open and close methods from main:

CDUtils.open("E:\\");

or

CDUtils.close("E:\\");

Java 3D Tutorial

Java 3D Tutorial

Free Java Game Source Codes

Download Java3D API

Monday, May 26, 2008

Inticas 008: Removed XML Added Teacher Class

In Inticas 008, removed all xml code, since it is not necessary to save data in xml format.
Instead both objects Classroom and Teacher which implements Serializable is saved in files as Objects.

Saving as Objects has the advantage that the entire object including data information is saved as well.

I also added the Teacher Class:

package inticas;
import java.io.Serializable;

public class Teacher implements Serializable
{
private String name;
private String initials;
private boolean needBreak = false;
private int taughtToday;
private int maxTotal;
private String subjects[] = new String[10];

public void setName(String s) {
name = s;
}
public void setInitials(String s) {
initials = s;
}
public void toggleBreak() {
needBreak = !needBreak;
}
public void incrementTaughtToday() {
taughtToday++;
}
public void setmaxTotal(int i) {
maxTotal = i;
}

////////////////////////////////////
public String getName(){
return name;
}
public String getInitials(){
return initials;
}
public int getmaxTotal(){
return maxTotal;
}


}


Only a few attributes and methods at this point.
Will gradually add more.

Also added a Add Teacher Frame which hardcodes one teacher object and saves as file:
TeacherSerialized.txt

How to close JFrames

mindprod.com

Sunday, May 25, 2008

IntiCas Simulation Scenario

Intro

I will now attempt to build Inticas based on the simulated scenario below.
The idea is to build a prototype where there is only a base case scenario.
If the base case scenario can work, only then will I proceed to add more classrooms, more teachers and more constraints.

Scenario

Inti has 1 classroom. There are three teachers: PC, JM and SM.
There are 9 subjects taught by the respective teachers:

PC:
Java, VB and C#

JM:
Maths, Physics, Chem

SM:
Econs, Accounts, Mgt

Teacher constraints:

Each subject takes up 6 hours per week.
Each teacher can teach maximum 6 hours each day.
Each teacher should not teach more than 2 hours without a break.
The break should be at least 1 hour.

Classroom constraints:
Maximum 72 hours per week because there are 72 slots.
Total teaching hours is 6 x 9 = 54 hours because there are 9 subjects and each subject requires 6 hours.


Subject constraints:
Each subject should not exceed 2 hours per day. No same subject should occur more than once on any day.

To Do:
Design and code a system that can automatically generate a timetable that meets the above constraints and display it in a JTable.

Your system should also be able to save each autogenerated timetable in user selected files.
It should also be able to load previously auto-generated timetables.

Subject constraints take priority over Teacher constraints.
That means, when assigning slots, we put in the slots based on the Subject, not based upon the Teacher. The teacher is property of the Subject. The Subject is not the property of the Teacher.

Inticas 007: Save and Open Table Files

Added capability to save Table to a file.
Added capability to open file and populate Table.

Used Classroom object to capture Table data before saving.
Classroom object implements serializable so that the entire object can be saved,
instead of individual data.

In the next version, I will sanitize the project by removing all unnecessary XML code.

Saturday, May 24, 2008

Friday, May 23, 2008

Panda3D Free Game Library

Free 3D Game Library for C++

Inticas 006: Created Classroom class

I'm re-evaluating whether or not to use XML to store data. XML introduces an element of complexity to the coding. I want to keep it simple.

I'm considering the possibility of storing data using a pre-defined class, called Classroom:

public class Classroom
{
private String classroomname;
private String mon[] = new String[24];
private String tues[] = new String[24];
private String wed[] = new String[24];
private String thurs[] = new String[24];
private String fri[] = new String[24];
private String sat[] = new String[24];

public void setMonSlot(String name, int time)
{
mon[time] = name;
}
public void setTuesSlot(String name, int time)
{
tues[time] = name;
}
public void setWedSlot(String name, int time)
{
wed[time] = name;
}
public void setThursSlot(String name, int time)
{
thurs[time] = name;
}
public void setFriSlot(String name, int time)
{
fri[time] = name;
}
public void setSatSlot(String name, int time)
{
sat[time] = name;
}
//////////////////////////////////////////

public String getMonSlot(int time)
{
return mon[time];
}
public String getTuesSlot(int time)
{
return tues[time];
}
public String getWedSlot(int time)
{
return wed[time];
}
public String getThursSlot(int time)
{
return thurs[time];
}
public String getFriSlot(int time)
{
return fri[time];
}
public String getSatSlot(int time)
{
return sat[time];
}


}



It will be stored in files as Classroom objects.

Wednesday, May 21, 2008

Inticas 005: Added Table

Added JTable:


I am now able to programmatically read each cell and also write to each cell.
For example, to set a value at cell 5, 5:


mytable.setValueAt("cc",5,5);


where mytable is a JTable object

To read a value from cell 5,5 :

String myvalue = (String)mytable.getValueAt(5, 5);


To be done:
Next I will implement code to save the contents of each cell in this xml format:






where PC, JM and SK are example Lecturer's names.

I will also need to implement code to enable user to populate the JTable with any selected xml file of the above format.

Tutorial on JTable

Inticas 004 : Added Xml Writer Demo

Below is a screen shot of the Xml Writer Demo application:



The new frame demonstrates how to an xml file called book.xml is created and saved.
The book.xml is saved in the root folder of the inticas project. And the contents is also automatically displayed in the TextArea of the form.

Monday, May 19, 2008

How to Create MDI App in Netbeans

YouTube - How fast we create MDI forms with Netbeans


Below is Inticas003:



It is an MDI Implementation of the previous Inticas. The two applications :

1. File Chooser (which allows you to select any text file to open and view)
2. Xml Dom Demo (which opens the file 'employees.xml' and displays its contents)

is now implemented within two different Frames (Forms).

The Xml Dom Demo now displays the exmployees.xml content within the Frame (form) instead of at the bottom of the Netbeans IDE.

The steps for building Inticas003 follows.

1. Create a new project.
Select a Java Desktop Application

2. Add a Desktop Pane from the Swing Containers Pallette

3. Add a new Menu to the Menu Bar and call it Applications

4. Add two Menu Items fo the above new Menu Bar called
File Chooser Demo
Xml Demo

5. Add two new JInternalFrames to the Project called:
FileChooserFrame
XmlDemoFrame

6. To each of the above new Frames in 5 above, add the respective Menu Bars and
JTextArea objects

7. Add code to implement the menuitems in 6 above

8. Go back to the MainFrame called InticasView.java (this is the parent Frame)
and implement the code for the FileChooser menuitem and XmlDemo menuitem.

9. To enable the two new frames to be called, u need to add them to the Desktop Pane
created in Step 2 above:

private void fileChooseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
FileChooserFrame fc = new FileChooserFrame();
desktop.add(fc);
fc.setVisible(true);

}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
XmlDomFrame xf = new XmlDomFrame();
desktop.add(xf);
xf.setVisible(true);
}


desktop is the name of the JDesktopPane

Note that if you do not add them to the JDesktopPane, the child windows will not be able to become the top focussed window when u click on them, and also will not be able to iconify (minimize).