Joined: Jul 26, 2011
Post Count: 62
Status:
Offline
How to run a new class in SH3D source
Hello again,
I have started programming and I created a new package in the existing SH3D source code. In this new package I have created a class that is used to create a new Home, with walls and furniture. When I run this class, the interface of SH3D is opened but nothing is created. (I run this class using the instructions of README.txt and having as a main class com.eteks.sweethome3d.SweetHome3D). Could you tell me what I am doing wrong??
Joined: Jul 26, 2011
Post Count: 62
Status:
Offline
Re: How to run a new class in SH3D source
package Blender; /* * Read_File_and_Save.java Aug 11, 2011 * * Creation of a class that reads a text file and saves it in an array * Afterwards, it creates a SH3D project which is according to user's preferences. */
/** Basic external class used to create a room and import furniture in it according to user's preferences. * @throws Exception * * */ public static void main(String args[]) {
// Maximum elements of the array:108 (200 obligatory, 88 optional) String[] str = new String[108];
try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("/Users/dimitramicha/Desktop/SweetHome3D1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line int i=0; while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println (strLine); //System.out.println(i); str = strLine; System.out.println(str); i++; // Check each line - Don't forget to make them comments afterwards!!!!! }
//Close the input stream in.close(); } catch (Exception e) { //Catch exception if any System.err.println("Error: " + e.getMessage()); }
// Save in another array the list of furniture. The maximum of this list is 91 // (108 values in total - 17 already mentioned above)
int j; for (j=0;j<91;j++) { String List_of_Furniture = (String) str[j+17]; } //Check the Shape_of_Room //Else - If Statement
if (Shape_of_Room == "Parallelogram") {
// Create a home and a wall listener that updates lists when notified Home Parallelogram = new Home();
final List<Wall> addedWalls = new ArrayList<Wall>(); final List<Wall> deletedWalls = new ArrayList<Wall>(); final List<Wall> updatedWalls = new ArrayList<Wall>(); final PropertyChangeListener wallChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent ev) { updatedWalls.add((Wall)ev.getSource()); } };
Parallelogram.addWallsListener(new CollectionListener<Wall> () { public void collectionChanged(CollectionEvent<Wall> ev) { switch (ev.getType()) { case ADD : addedWalls.add(ev.getItem()); ev.getItem().addPropertyChangeListener(wallChangeListener); break; case DELETE : deletedWalls.add(ev.getItem()); ev.getItem().removePropertyChangeListener(wallChangeListener); break; } } });
//Create a Room Room Parallelogram_Room = new Room(null); Parallelogram_Room.addPoint(0, 0); Parallelogram_Room.addPoint(0, 400); Parallelogram_Room.addPoint(625, 400); Parallelogram_Room.addPoint(625, 400); Parallelogram_Room.addPoint(0, 0);
// Add them to home Parallelogram.addWall(Left_Wall); Parallelogram.addWall(North_Wall); Parallelogram.addWall(Right_Wall); Parallelogram.addWall(South_Wall);
// Set as User Preferences the default User Preferences //UserPreferences preferences = new DefaultUserPreferences();
// Set as Texture image the first texture that is available
// Join end point of one wall to start point of the other wall (4 times - 4 walls) Left_Wall.setWallAtEnd(North_Wall); North_Wall.setWallAtEnd(Right_Wall); Right_Wall.setWallAtEnd(South_Wall); South_Wall.setWallAtEnd(Left_Wall);
if (Floor_Display == "true") { Parallelogram_Room.setFloorVisible(true); }
if (Shiny_Matt == "Matt") { Left_Wall.setRightSideShininess(0); North_Wall.setRightSideShininess(0); Right_Wall.setRightSideShininess(0); South_Wall.setRightSideShininess(0); } else if (Shiny_Matt == "Shiny") {
// http://cloford.com/resources/colours/500col.htm Color(int rgb) // Create a string array which will be used to check the color of each wall // Create an array of integers which includes the rgb numbers of the colors
String[] color = {"Grey","White","Red","Green","Yellow","Blue"}; int[] rgb = {13882323,16119285,6974207,10210715,9170175,15641692};
int k;
for(k=0;k<6;k++) { if (Left_Wall_Color == color[k]) {
Left_Wall.setRightSideColor(rgb[k]); } if (North_Wall_Color == color[k]) {
North_Wall.setRightSideColor(rgb[k]); } if (Right_Wall_Color == color[k]) {
Right_Wall.setRightSideColor(rgb[k]); } if (South_Wall_Color == color[k]) {
South_Wall.setRightSideColor(rgb[k]); }
}; } (it continues with the same code, else -if state but different objects are created) }
France
Joined: Nov 7, 2005
Post Count: 9426
Status:
Offline
Re: How to run a new class in SH3D source
Ok, I think I understood what you want to do now. If you want to display a home built by program from a derived version of Sweet Home 3D, you should: - create a subclass of com.eteks.sweethome3d.SweetHome3D application class, - add a main method that will instantiate and init the application from the arguments - override the createHome method to return a com.eteks.sweethome3d.model.Home instance initialized the way you want. Here's a base example:
public static String[] str = new String[108]; public static String Shape_of_Room = str[2]; public static String Shiny_Matt = str[3]; public static String Right_Wall_Color = str[5]; public static String Right_Wall_Texture = str[6]; public static String Left_Wall_Color = str[7]; public static String Left_Wall_Texture = str[8]; public static String North_Wall_Color = str[9]; public static String North_Wall_Texture = str[10]; public static String South_Wall_Color = str[11]; public static String South_Wall_Texture = str[12]; public static String Ceiling_Display = str[13]; public static String Ceiling_Texture = str[14]; public static String Floor_Display = str[15]; public static String Floor_Texture = str[16]; public static String[] List_of_Furniture;
public static void main(String [] args) {
// First save the furniture in an array // Maximum elements of the array:108 (91 obligatory, 88 optional) int j;
// Declare the size of the List_of_Furniture List_of_Furniture = new String[92];
for (j=0;j<91;j++) { List_of_Furniture[j] = str[j+17]; }
try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("/Users/dimitramicha/Desktop/SweetHome3D1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read File Line By Line int i=0; while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); System.out.println(i); str = strLine; System.out.println(str); i++; // Check each line - Don't forget to make them comments afterwards!!!!! }
// Close the input stream in.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); }
// Run the application SweetHome3D and run method createHome() Blender blender = new Blender(); blender.init(args); blender.createHome();
}
// The ability of a subclass to override a method allows a class to inherit from // a superclass whose behavior is "close enough" and then to modify behavior as needed. // The overriding method has the same name, number and type of parameters, and return type // as the method it overrides. An overriding method can also return a subtype of the type // returned by the overridden method.
@Override public Home createHome() {
Home home = super.createHome();
if (Shape_of_Room=="Parallelogram") { // Create a home and a wall listener that updates lists when notified Home Parallelogram = new Home();
// Create a Room - Room (float[][] points) // Any of these numbers can be followed by "F" (or "f") // to make it a float instead of the default double float[][] points = {{0f,0f},{0f,400f},{625f,400f},{625f,400f},{625f,0f},{0f,0f}}; Room Parallelogram_Room = new Room(points); Parallelogram_Room.setAreaVisible(true);
// Add new Room to Home Parallelogram.addRoom(Parallelogram_Room);
// Notify Listeners
final List<Wall> addedWalls = new ArrayList<Wall>(); final List<Wall> deletedWalls = new ArrayList<Wall>(); final List<Wall> updatedWalls = new ArrayList<Wall>(); final PropertyChangeListener wallChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent ev) { updatedWalls.add((Wall)ev.getSource()); } };
Parallelogram.addWallsListener(new CollectionListener<Wall> () { public void collectionChanged(CollectionEvent<Wall> ev) { switch (ev.getType()) { case ADD : addedWalls.add(ev.getItem()); ev.getItem().addPropertyChangeListener(wallChangeListener); break; case DELETE : deletedWalls.add(ev.getItem()); ev.getItem().removePropertyChangeListener(wallChangeListener); break; } } });
// Add them to home Parallelogram.addWall(Left_Wall); Parallelogram.addWall(North_Wall); Parallelogram.addWall(Right_Wall); Parallelogram.addWall(South_Wall);
// Set as User Preferences the default User Preferences //UserPreferences preferences = new DefaultUserPreferences();
// Set as Texture image the first texture that is available
// Join end point of one wall to start point of the other wall (4 times - 4 walls) Left_Wall.setWallAtEnd(North_Wall); North_Wall.setWallAtEnd(Right_Wall); Right_Wall.setWallAtEnd(South_Wall); South_Wall.setWallAtEnd(Left_Wall);
};
if (Shape_of_Room == "Square") {
// Create a home and a wall listener that updates lists when notified Home Square = new Home();
};
if (Shape_of_Room == "Trapeze-1") {
// Create a home and a wall listener that updates lists when notified Home Trapeze_1 = new Home();
};
if (Shape_of_Room == "Trapeze-2") {
// Create a home and a wall listener that updates lists when notified Home Trapeze_2 = new Home();
France
Joined: Nov 7, 2005
Post Count: 9426
Status:
Offline
Re: How to run a new class in SH3D source
You shouldn't call blender.createHome(); directly and your createHome method doesn't return the home to which you added all your walls! Instantiate only once the Home class with super.createHome(); call and add the walls to this instance.
----------------------------------------
Emmanuel Puybaret, Sweet Home 3D creator