Simple Java Eric Roberts CS 106A January 11, 2016.

Slides:



Advertisements
Similar presentations
Chapter 2—Programming by Example The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Programming by Example C H A P T E R 2.
Advertisements

Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Classes and Objects Java programs are written as collections of classes, which serve as templates for individual objects. Each object is an instance of.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
Inheritance in C++ Eric Roberts CS 106B March 1, 2013.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
Graphical Structures Eric Roberts CS 106A January 29, 2010.
Inheritance CS 2302 SPRING Inheritance In this part we introduce a new relationship between classes: inheritance. This is the fundamental feature.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
Classes. Preparation Scene so far has been background material and experience –Computing systems and problem solving –Variables –Types –Input and output.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Simple Java Eric Roberts CS 106A January 11, 2010.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Control Statements Eric Roberts CS 106A January 15, 2010.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Five-Minute Review 1.What steps do we distinguish in solving a problem by computer? 2.What are essential properties of an algorithm? 3.What is a definition.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
Five-Minute Review What steps do we distinguish in solving a problem by computer? What are essential properties of an algorithm? What is a definition of.
Strings and Graphics Jerry Cain CS 106J April 12, 2017.
Review.
Object Oriented Programming
Beyond Console Programs
reading: Art & Science of Java,
CS 106A, Lecture 12 More Graphics
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
slides courtesy of Eric Roberts
Chapter 2—Programming by Example
Graphics -- Introduction
SSEA Computer Science: Track A
Graphics.
Chapter 2—Programming by Example
Presentation transcript:

Simple Java Eric Roberts CS 106A January 11, 2016

Once upon a time...

The “Hello World” Program One of the important influences on the design of Java was the C programming language, which was developed at Bell Labs in the early 1970s. The primary reference manual for C was written by Brian Kernighan and Dennis Ritchie. On the first page of their book, the authors suggest that the first step in learning any language is to write a simple program that prints the message “hello, world” on the display. That advice remains sound today. 1.1 Getting Started The only way to learn a new programming language is to write programs in it. The first program to write is the same for all languages: Print the words hello, world This is the big hurdle; to leap over it you have to be able to create the program text somewhere, compile it, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy. In C, the program to print “ hello, world ” is #include main() { printf("hello, world"); }

Simple Java

Evolution of Computer Languages

The 2002 ACM Turing Award Kristen NygaardOle Johan Dahl The most prestigious prize in computer science, the ACM Turing Award, was given in 2002 to two Norwegian computing pioneers, who developed the first object-oriented programming language in 1967.

Systems of Classification In the mid-18th century, the Scandinavian botanist Carl Linnaeus revolutionized the study of biology by developing a new system for classifying plants and animals in a way that revealed their structural relationships and paved the way for Darwin’s theory of evolution a century later. Carl Linnaeus (1707–1778) Linnaeus’s great contribution was to recognize that organisms fit into a hierarchical classification scheme in which the placement of individual species within the hierarchy reflects their anatomical similarities.

Biological Class Hierarchy CrustaceaArachnidaInsecta AnnelidaBrachiopodaMolluscaChordataArthropoda PlantsFungiAnimals Living Things Animals Arthropoda Insecta Hymenoptera Formicidae Iridomyrmex purpureus Kingdom Phylum Order Class Family Genus Species Classification of the red ant Iridomyrmex purpureus Every red ant is also an animal, an arthropod, and an insect, as well as the other superclasses in the chain.

Instances vs. Patterns Drawn after you, you pattern of all those. —William Shakespeare, Sonnet 98 In thinking about any classification scheme—biological or otherwise—it is important to draw a distinction between the category defined by a particular class and specific instances of that class. In the most recent example, the designation Iridomyrmex purpureus is not itself an ant, but rather a class of ant. There can be (and usually are) many ants, each of which is an individual of that class. Each of these fire ants is an instance of the general category encompassing all ants of its class. Each instance is of the species purpureus, the genus Iridomyrmex, the family Formicidae (which makes it an ant), and so on. Thus, each ant is not only an ant, but also an insect, an arthropod, and an animal.

The Program Hierarchy Applet JApplet Program DialogProgramGraphicsProgramConsoleProgram Java class hierarchies are similar to the biological class hierarchy from the previous slide. This diagram shows the hierarchy formed by the classes in the acm.program package. Every ConsoleProgram is also a Program, a JApplet, and an Applet. That means that every ConsoleProgram can run as an applet on the web. The same is true for every DialogProgram and GraphicsProgram.

import acm.program.*; public class HelloProgram extends ConsoleProgram { public void run() { println("hello, world"); } Hello World as a Console Program import acm.program.*; public class HelloProgram extends ConsoleProgram { public void run() { println("hello, world"); } HelloConsole hello, world

Hello World as a Dialog Program import acm.program.*; public class HelloProgram extends DialogProgram { public void run() { println("hello, world"); } import acm.program.*; public class HelloProgram extends DialogProgram { public void run() { println("hello, world"); }

Hello World as a Graphics Program import acm.graphics.*; import acm.program.*; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); } HelloProgram hello, world import acm.graphics.*; import acm.program.*; public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); }

The Java Coordinate System Positions and distances in a graphics program are measured in terms of pixels, which are the individual dots that cover the screen. Unlike traditional mathematics, Java defines the origin of the coordinate system to be in the upper left corner. Values for the x coordinate increase as you move rightward across the screen; y coordinate values increase as you move downward. HelloProgram hello, world (100, 100) Creating a JLabel at a particular x and y position means that the baseline of the first character in the label appears at that point, as follows:

The GObject Hierarchy The classes that represent graphical objects form a hierarchy, part of which looks like this: GObject GRectGOvalGLineGLabel The GObject class represents the collection of all graphical objects. The four subclasses shown in this diagram correspond to particular types of objects: labels, rectangles, ovals, and lines. The class diagram makes it clear that any GLabel, GRect, GOval, or GLine is also a GObject. Operations on graphical objects are defined at each level of the hierarchy. Operations that apply to all graphical objects are specified at the GObject level, where they are inherited by each subclass. Operations that apply to a particular subclass are specified as part of the definition of that class.

Operations on the GObject Class object.setColor( color ) Sets the color of the object to the specified color constant. object.setLocation( x, y ) Changes the location of the object to the point (x, y). object.move( dx, dy ) Moves the object on the screen by adding dx and dy to its current coordinates. The following operations apply to all GObject s : The standard color names are defined in the java.awt package: Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE Color.RED Color.YELLOW Color.GREEN Color.CYAN Color.BLUE Color.MAGENTA Color.ORANGE Color.PINK

Drawing Geometrical Objects Constructors new GRect( x, y, width, height ) Creates a rectangle whose upper left corner is at (x, y) of the specified size. new GOval( x, y, width, height ) Creates an oval that fits inside the rectangle with the same dimensions. Methods shared by the GRect and GOval classes object.setFilled( fill ) If fill is true, fills in the interior of the object; if false, shows only the outline. object.setFillColor( color ) Sets the color used to fill the interior, which can be different from the border. new GLine( x 0, y 0, x 1, y 1 ) Creates a line extending from (x 0, y 0 ) to (x 1, y 1 ).

Operations on the GLabel Class Constructor new GLabel( text, x, y ) Creates a label containing the specified text that begins at the point (x, y). Methods specific to the GLabel class label.setFont( font ) Sets the font used to display the label as specified by the font string. The font is typically specified as a string in the form " family - style - size " where family is the name of a font family style is either PLAIN, BOLD, ITALIC, or BOLDITALIC size is an integer indicating the point size

The End