Java Programming for Designers

5,113 views

Published on

A quick introduction to Java programming for people who want to do interesting stuff without becoming a real programmer... that's the rest of us.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,113
On SlideShare
0
From Embeds
0
Number of Embeds
141
Actions
Shares
0
Downloads
484
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Java Programming for Designers

  1. 1. Java Programming (for designers)
  2. 2. What is a Program? A list of detailed instructions that the computer carries out To make a cup of coffee the following would not be sufficient: The instructions would have to be much more detailed: You have to say exactly what to do in the right order with no ambiguity 1. Boil water 2. Put coffee in cup 3. Pour in hot water 4. Add milk 1.1. Go to kettle 1.2. Check kettle has water in it 1.3. If not 1.3.1 Take kettle to cold tap 1.3.2 Remove lid of kettle 1.3.3 Put kettle under cold tap 1.3.4 Turn on tap 1.3.5 When kettle full turn off tap 1.3.6 Return kettle to worktop 1.4. Plug kettle into mains 1.5.Switch on electricity …… .
  3. 3. Java is… <ul><li>Platform-independent </li></ul><ul><li>Apps for mobile phones and other consumer devices </li></ul><ul><li>Secure, portable, multithreaded </li></ul><ul><li>Online, server-side applications </li></ul><ul><li>Simple, Object oriented </li></ul><ul><li>GPL libraries </li></ul>X X X ? ☺ X ☺
  4. 4. Get started <ul><li>Two components: </li></ul><ul><ul><li>Java Virtual Machine (VM) </li></ul></ul><ul><ul><ul><li>Base for the Java platform, install Win version </li></ul></ul></ul><ul><ul><li>Java Application Programming Interface (API) </li></ul></ul><ul><ul><ul><li>Prewritten code, organized into packages of similar topics. Each package or library contains classes and interfaces that you can call and extend. Example: </li></ul></ul></ul><ul><ul><ul><ul><li>Rectangle2D = new Rectangle2D(loc, width, height) </li></ul></ul></ul></ul><ul><li>http://java.sun.com/learning/new2java </li></ul>
  5. 5. Acronyms <ul><li>JDK or J2SE, Development Kit </li></ul><ul><ul><li>Set of Java development tools, consisting of the API classes, a Java compiler, and the Java virtual machine (VM) interpreter </li></ul></ul><ul><ul><li>The JDK is used to compile Java applications and applets. The most current version is the J2SE 5 </li></ul></ul><ul><li>J2SE, Runtime Environment 5.0 </li></ul>
  6. 6. Set up: Download the JDK <ul><li>And set the classpath in Windows </li></ul>Include the directories where you will run Java apps
  7. 7. Set Up a Development Environment <ul><li>NetBeans IDE, JEdit, Eclipse, JCreator </li></ul><ul><li>Or use a simple text editor, and compile and run from the command line </li></ul>
  8. 8. Set Up <ul><li>Download J2SE 5.0 Documentation </li></ul><ul><li>Look for useful libraries </li></ul>
  9. 9. Some Useful Libraries <ul><li>Colt: or High Performance Scientific and Technical Computing </li></ul><ul><li>Weka 3: Data Mining Software in Java </li></ul><ul><li>JHotDraw is a two-dimensional graphics framework for structured drawing editors </li></ul><ul><li>JGAP is a genetic algorithms component written in the form of a Java package </li></ul><ul><li>OpenAI: a full suite of Artificial Intelligence components: Agents, Neural Nets, GAs… </li></ul><ul><li>JUNG:Java Universal Network/Graph Framework </li></ul><ul><li>JOONE: JGAP's genetic algorithms to evolve neural nets </li></ul><ul><li>HyperGraph: to work with hyperbolic geometry and especially with hyperbolic trees </li></ul><ul><li>Repast: an agent modeling toolkit (like Swarm) </li></ul><ul><li>JExcel or JXL provides the ability to read, write, and modify Microsoft Excel spreadsheets </li></ul>
  10. 10. Standard Java Libraries <ul><li>Language </li></ul><ul><li>Maths </li></ul><ul><li>Graphics </li></ul><ul><li>2D </li></ul><ul><li>3D </li></ul><ul><li>File I/O </li></ul><ul><li>JAI: Advanced Networking </li></ul><ul><li>Imaging </li></ul><ul><li>XML </li></ul><ul><li>Print </li></ul>
  11. 11. Swing <ul><li>GUI-control (widget) library in Java 2 </li></ul><ul><li>Built-in controls, flexible and customizable </li></ul><ul><li>Features aimed at interface design </li></ul><ul><ul><li>Buttons, tabbed panes, dockable toolbars, scroll panes, tooltips, tables, etc. </li></ul></ul><ul><li>Look & feel can be changed </li></ul>
  12. 12. Java 2D <ul><li>Standard drawing library in Java 2 </li></ul><ul><li>Drawing attributes </li></ul><ul><ul><li>Fill patterns and images </li></ul></ul><ul><ul><li>Fonts </li></ul></ul><ul><ul><li>Line thicknesses and dashing patterns </li></ul></ul><ul><ul><li>Colour mixing rules and transparency </li></ul></ul><ul><li>Transformations </li></ul><ul><ul><li>Floating-point coordinate system </li></ul></ul><ul><ul><li>Mapping from memory coords to screen or printer coords </li></ul></ul><ul><ul><li>Affine transforms: translate, scale, rotate, and shear </li></ul></ul>
  13. 13. Java 3D <ul><li>Extension to Java </li></ul><ul><ul><li>Not part of “core” Java language like Java 2D </li></ul></ul><ul><li>Built on Direct3D or OpenGL, depending on platform </li></ul><ul><li>Scene-graph based model, not primarily immediate-mode rendering </li></ul>
  14. 14. Java Versions <ul><li>Java 1.0, 1.1… “Java 2” refers to 1.2 onwards </li></ul><ul><li>Popular: 1.4.2 </li></ul><ul><li>New: 1.5. The leading &quot;1.&quot; is dropped, now it is Java Platform Standard Edition 5.0 </li></ul><ul><li>But… version 5.0 also keeps the version number 1.5.0 in some places visible only to developers </li></ul><ul><li>This release is also known as “Tiger” (not the latest Mac OS X!) </li></ul>
  15. 15. Jargon <ul><li>Application is a stand-alone Java program </li></ul><ul><li>Applet is a browser-based Java program </li></ul>
  16. 16. Basic Procedure <ul><li>Write a .java file </li></ul><ul><li>Compile it with the javac command </li></ul><ul><li>Now you have a .class file </li></ul><ul><li>Run it with the java command </li></ul><ul><li>Voilá! </li></ul>
  17. 17. Running a Java Application Write Java code javac MyProg.java java MyProg Java code: MyProg.java Bytecode: MyProg.class IDE or Text Editor Output Save the file with a .java extension Run the Java compiler ' javac ' Execute the bytecode with the command ' java ' This creates a file of bytecode with a .class extension User to interact with the system Program to execute and produce a data set, graphs, etc.
  18. 18. Java program layout <ul><li>A typical Java file looks like: </li></ul>import java.awt.*; import java.util.*; public class SomethingOrOther { // field and method definitions . . . } This must be in a file named SomethingOrOther.java !
  19. 19. What does it mean? import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 50, 50); } } These 2 lines tell the computer to include (import) two standard libraries awt (Abstract Window Toolkit) and applet. This line tells the computer to display some text (a string) on the screen. This line announces that the program (class) can be run by anyone (public), is called Greetings and is an Applet. This line declares what follows in the { } as a method called paint. This is where it is displayed in pixels across and down from the top left hand corner This is what is displayed
  20. 20. A Java Class <ul><li>Java programs are collections of classes </li></ul><ul><li>OOP: </li></ul><ul><ul><li>A class that represents a rectangle would contain variables for its location, its width, and its height </li></ul></ul><ul><ul><li>The class can also contain a method that calculates the area of the rectangle </li></ul></ul><ul><ul><li>An instance of the rectangle class could contain the information for a specific rectangle, such as the dimensions of a room </li></ul></ul>
  21. 22. An Applet is a Panel is a Container… is an Object java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet … so you can do things with it that you’d do with applets, panels, containers, components, and objects.
  22. 23. Object Oriented (OO) Programming A key aspect is Inheritance . You don’t have to describe similar objects completely. You can define a superclass (sometimes called base class ) that has the common attributes and methods and inherit these adding only the ones that are different. Vehicle Bus Lorry Attributes: Speed, Colour Methods: Start, Stop Attributes: Max Load Methods: Pick up Load Attributes: Max Passengers Methods: Pick up Passengers These are inherited
  23. 24. What classes, why? <ul><li>A file can contain multiple classes, but only one can be declared public , and that one’s name must match the filename </li></ul><ul><ul><li>Usually 1 class = 1 file </li></ul></ul><ul><li>Most difficult part is to plan the program: the classes, the methods, the variables, the procedure </li></ul><ul><ul><li>If you can define it in paper & pencil, the rest is easy </li></ul></ul>
  24. 25. Methods <ul><li>Define a group of statements that perform a particular operation </li></ul><ul><li>With or without arguments </li></ul>calculateArea(shape) { // here geom operations } createShape() { // new square... // calculateArea(square) }
  25. 26. Primitives and Objects <ul><li>Java distinguishes two kinds of entities </li></ul><ul><ul><li>Primitive types </li></ul></ul><ul><ul><li>Objects </li></ul></ul><ul><li>Primitive types: integers, doubles, booleans, strings… </li></ul><ul><li>Objects are associated with reference variables which store an object’s address </li></ul>
  26. 27. Expressions <ul><li>Assignment statements: = , += , *= etc. </li></ul><ul><li>Arithmetic uses the familiar + - * / % </li></ul><ul><li>Java uses ++ and -- </li></ul><ul><li>Java has boolean operators && || ! </li></ul><ul><li>Java has comparisons < <= == != >= > </li></ul>
  27. 28. Control statements <ul><li>if (x < y) smaller = x; </li></ul><ul><li>if (x < y){ smaller=x; sum += x;} else { smaller = y; sum += y; } </li></ul><ul><li>while (x < y) { y = y - x; } </li></ul><ul><li>do { y = y - x; } while (x < y) </li></ul><ul><li>for (int i = 0; i < max; i++) sum += i; </li></ul>
  28. 29. The main method <ul><li>Every Java application must contain a main method: </li></ul><ul><ul><li>public static void main(String[] args) </li></ul></ul><ul><li>An application executes the main method first. The array of Strings can be empty or it can receive arguments to customise how the program runs (number of iterations, number of agents, etc). </li></ul>
  29. 30. Example An Evolutionary Design System
  30. 38. <ul><li>public static void main(String[] args) { </li></ul><ul><li>new Model(); </li></ul><ul><li>} </li></ul><ul><li>public Model() { </li></ul><ul><li>EvoDesign frame = new EvoDesign(); </li></ul><ul><li>frame.setVisible(true); </li></ul><ul><li>} </li></ul>
  31. 39. <ul><li>public EvoDesign() { </li></ul><ul><li>jbInit(); </li></ul><ul><li>} </li></ul><ul><li>private void jbInit() throws Exception { </li></ul><ul><li>setSize(new Dimension(860,680)); </li></ul><ul><li>setTitle(&quot;Evolutionary Design&quot;); </li></ul><ul><li>jMenuFile.setText(&quot;File&quot;);… </li></ul><ul><li>setJMenuBar(jMenuBar); </li></ul><ul><li>JComboBox jGens… </li></ul><ul><li>JList jListFns… </li></ul><ul><li>JButton jRun… </li></ul><ul><li>jTabs.addTabs… </li></ul><ul><li>contentPane.add(…); </li></ul><ul><li>} </li></ul><ul><li>void jTabs_clicked(ChangeEvent e) { … } </li></ul><ul><li>void jLists_mouseClicked(MouseEvent e) { … } </li></ul>
  32. 40. <ul><li>Image makeFitnessLines(Image im) { </li></ul><ul><li>LineGraph bg = new LineGraph(); </li></ul><ul><li>bg.setDefaultLineThickness(0.1); </li></ul><ul><li>… </li></ul><ul><li>try { </li></ul><ul><li> new ImageOutput(im); </li></ul><ul><li> out.render(bg); im = out.getImage(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>void jMenuExit_actionPerformed(ActionEvent e) { </li></ul><ul><li>System.exit(0); </li></ul><ul><li>} </li></ul><ul><li>class drawPanel extends JPanel { </li></ul><ul><li>public void paint(Graphics g) { </li></ul><ul><li>g.setFont(EvoDesign.myFont); </li></ul><ul><li>g2.drawImage(new BufferedImage(0, 50, 2); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  33. 41. <ul><li>public void setupGA() throws Exception { </li></ul><ul><li>fitnessHistory = new DoubleArrayList(); </li></ul><ul><li>topFitness.clear(); </li></ul><ul><li>Configuration conf = new DefaultConfiguration(); </li></ul><ul><li>conf.addGeneticOperator( new MutationOperator(mut) ); </li></ul><ul><li>conf.addGeneticOperator( new CustomCrossover(cross) ); </li></ul><ul><li>FitnessFunction myFunc = new FitnessFn(chrom); </li></ul><ul><li>conf.setFitnessFunction(myFunc); </li></ul><ul><li>Gene[] g = new Gene[chrom]; </li></ul><ul><li>g[0] = new BooleanGene(); </li></ul><ul><li>g[i] = new IntegerGene(1, alle); </li></ul><ul><li>Chromosome sampleChromosome = new Chromosome(g); </li></ul><ul><li>conf.setSampleChromosome(sampleChromosome); </li></ul><ul><li>conf.setPopulationSize(EvoDesign.pop); </li></ul><ul><li>genPopulation = Genotype.randomInitialGenotype(conf); </li></ul><ul><li>Chromosome best = genPopulation.getFittestChromosome(); </li></ul><ul><li>} </li></ul>
  34. 42. <ul><li>public FitnessFn(int a) { </li></ul><ul><li>} </li></ul><ul><li>public int evaluate(Chromosome a) { </li></ul><ul><li>int fitness = 0; </li></ul><ul><li>Gene[] gs = a.getGenes(); </li></ul><ul><li>if (EvoDesign.fnCrit.equals(&quot;minMean&quot;)) { </li></ul><ul><li> fitness += ( EvoDesign.alle - Descriptive.mean(sd) ); </li></ul><ul><li>} </li></ul><ul><li>return Math.max(1, fitness); </li></ul><ul><li>} </li></ul>
  35. 43. <ul><li>public Colours01() { </li></ul><ul><li>setColorPalettes(); </li></ul><ul><li>} </li></ul><ul><li>public static BufferedImage makeOutputImage() { </li></ul><ul><li>for (int i = 0; i < topSolutions.size(); i++) { </li></ul><ul><li> Chromosome s = topSolutions.get(i); </li></ul><ul><li> for (int j = 0; j < s.size(); j++) { </li></ul><ul><li>Gene si = s.getGene(j); </li></ul><ul><li>g2.setColor( getAllele() .intValue()); </li></ul><ul><li>g2.fillRect(boxX, boxY, stepX, 25); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>return bf; </li></ul><ul><li>} </li></ul>
  36. 44. Code <ul><li>Get these slides and the source code for the example at: </li></ul><ul><ul><li>http://www.arch.usyd.edu.au/~rsos7705/programming.html </li></ul></ul>

×