Java tutorial
/* * TuxCourser * Copyright (C) 2003-2004 Adam Elliott * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package net.ovres.tuxcourser; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import net.ovres.tuxcourser.output.CommercialOutput; import net.ovres.tuxcourser.output.PPRacer05Output; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class TuxCourser { private final static Logger logger = Logger.getLogger(TuxCourser.class.getName()); public final static int TYPE_TUXRACER11 = 1; public final static int TYPE_PPRACER05 = 2; public static void main(String[] argss) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("WARN|INFO|FINE").hasArg() .withDescription("Set the log level. Valid values include WARN, INFO, and FINE.") .withLongOpt("loglevel").create("l")); options.addOption("h", "help", false, "Displays this help and exits"); options.addOption("v", "version", false, "Displays version information and exits"); options.addOption(OptionBuilder.withArgName("TYPE").hasArg() .withDescription("Sets the output type. Valid values are tux11 and ppracer05").withLongOpt("output") .create()); CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, argss); } catch (ParseException exp) { System.err.println(exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar TuxCourser.jar [Options]", "Options", options, ""); System.exit(-1); } if (line.hasOption("loglevel")) { String lvl = line.getOptionValue("loglevel"); Level logLevel = Level.parse(lvl); Logger.getLogger("net.ovres.tuxcourser").setLevel(logLevel); } if (line.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar TuxCourser.jar [Options]", "Options", options, ""); return; } if (line.hasOption("version")) { System.out.println("TuxCourser Version " + Settings.getVersion()); return; } String[] remaining = line.getArgs(); // Initialize all the different item and terrain types ModuleClassLoader.load(); if (remaining.length == 0) { // Just show the gui. //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); new net.ovres.tuxcourser.gui.TuxCourserGui().setVisible(true); } else if (remaining.length == 3) { new TuxCourser().convertCourse(new File(remaining[0]), new File(remaining[1]), remaining[2], TYPE_TUXRACER11); } else { usage(); System.exit(-1); } } private static void usage() { // Just print out the help... System.out.println("TuxCourser version " + Settings.getVersion()); System.out.println("To bring up the GUI:"); System.out.println(" java -jar tuxcourser.jar [options]"); System.out.println(); System.out.println("To run in console mode:"); System.out.println(" java -jar tuxcourser.jar [options] <InputCourseDir> <ContribDir> <your_name>"); System.out.println(); System.out.println("To display a list of options:"); System.out.println(" java -jar tuxcourser.jar --help"); System.out.println(); System.out.println("If you need more info, please see the documentation."); } public TuxCourser() { } public void convertCourse(File courseDir, File contribDir, String cup, int outputType) throws IOException { // Start with the course properties... CourseProperties prop = new CourseProperties(courseDir); Preview prev = new Preview(prop); Elevation elev = new Elevation(prop); Trees trees = new Trees(prop); Terrain terr = new Terrain(prop); // We validate (or at least mention) if the image files are different sizes. int x = elev.getBufferedImage().getWidth(); int y = elev.getBufferedImage().getHeight(); if (trees.getBufferedImage().getWidth() != x || trees.getBufferedImage().getHeight() != y || terr.getBufferedImage().getWidth() != x || terr.getBufferedImage().getHeight() != y) { logger.warning("Source images not all the same dimensions"); logger.warning(" Elevation: " + x + " by " + y); logger.warning(" Terrain: " + terr.getBufferedImage().getWidth() + " by " + terr.getBufferedImage().getHeight()); logger.warning(" Trees: " + trees.getBufferedImage().getWidth() + " by " + trees.getBufferedImage().getHeight()); } // Everything read (and looks moderately ok), now write... if (outputType == TYPE_TUXRACER11) { new CommercialOutput().writeCourse(contribDir, cup, prop, elev, prev, terr, trees); } else if (outputType == TYPE_PPRACER05) { new PPRacer05Output().writeCourse(contribDir, cup, prop, elev, prev, terr, trees); } else { throw new IllegalArgumentException("Unknown type: " + outputType); } } }