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