Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
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.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
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.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
A Simple Applet.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
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 Swing - Lecture 1 An Introduction Milena Radenkovic slides originally by Chris Coleman.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Applets.
JAPPLET.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
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.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Applets Yong Choi School of Business CSU, Bakersfield.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
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.
Basics of GUI Programming Chapter 11 and Chapter 22.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Java Applets.
Object Oriented Programming
Building Java Programs
Java Applets.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Ellen Walker Hiram College
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
UNIT-5.
Java Applets.
Java Applets.
Java Applets.
Java applets 1/3/2019.
Lecture 4: Standard Java Graphics
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use it to enhance the previous projects (You won’t be learning the cutting- edge graphics for games, just the basics.)

Here are some terms that you’ll encounter in your lesson on graphics: AWT Swing Applet/JApplet Graphics object init() GUI

Graphics can be simple or complex, but they are just data like a text document or sound. Java is very good at graphics, especially for the web and small devices like phones.

Java can write applications or applets, as you know by now. It can make graphics in either one, and has two libraries to do it with: Swing (the newer kind) or AWT (Abstract Windowing Toolkit, the older kind).

To start, here’s a basic applet that demonstrates Java graphics using AWT: import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics Try to compile this code and run it as an applet. What do you see?

import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics The first lines are import statements, to load the AWT and Applet libraries. If you were making a Swing version, you would load Swing libraries.

import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics Our class is called BasicGraphics, and it extends Applet to inherit Applet properties.

import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics Inside the applet, we have just one method: paint() (Remember from Applets that other methods are optional.) It has one parameter, called the “abstract Graphics object”, and we call it “g”. Get used to this, you need to tell paint() what to paint on!

import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics paint() has two methods inside of it: setColor and fillRect g.setColor(Color.red); is the command to color whatever graphic “thing” we have “red”. The computer still doesn’t know what Graphic “thing” g is going to be!

import java.awt.*; import java.applet.Applet; public class BasicGraphics extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics g.fillRect(10, 20, 40, 40) tells the applet to make g into a “fillRect”, which is a “filled rectangle”. 10 is the starting x- position in the applet, 20 is the starting y- position in the applet 40 is the width and the other 40 is the height. Remember what color will fill it? Red!

By they way, a computer screen has reverse geometry, kind of. It’s still (x, y), but the “origin” is the upper left corner. Then you add to the right and/or down. The box on the left demonstrates how you would measure an 800x600 screen. “X” is at about (400, 100). 0,0 800, ,0 0,600

How do you remember all of this stuff? You don’t! All of this is listed in the Java API. To see the methods that you can use on “g”, our Graphics object, look at this link: ummary Want to see a simple edit? Change g.fillRect to g.fillOval in your program. You can do whatever changes you want, just find it in the API and make sure your parameters are filled in correctly!

What would that applet look like if we used Swing instead of the old AWT? import javax.swing.*; import java.awt.*; public class BasicSwingGraphics extends javax.swing.JApplet { public void paint(Graphics g) { /* Draw the square. */ g.setColor(Color.red); g.fillOval(10, 20, 40, 40); } // end paint() } // end class BasicSwingGraphics

Why is there a Swing and an AWT? AWT was the original graphics library for Java. However, all programs looked like their “host”. That is, when they ran on a Mac, they looked like Mac programs, and when they ran on Windows, they looked like Windows programs. Programmers wanted to force the “look and feel”, so they built Swing on top of AWT. Yes, Swing includes all of AWT. AWT is still seen as better for applets, but that’s fading due to AJAX, another web programming technique.

Applets need a web page to run, but applications can run on their own. Now we’ll take a look at an application written in Swing!

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from First, we import the Swing libraries (notice it’s javax.swing) to support our work.

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from Then, we begin our class. We don’t extend Applet, we extend JFrame, because applications use them to hold graphics.

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from The class constructor creates: -location and size of the frame

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from The class constructor creates: -the way it closes (this one is that little “X” box on the top right

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from The class constructor creates: -visibility (you can see it) -title (the blue bar on top)

import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setBounds(50,100,400,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setTitle("This is a test frame!"); } public static void main(String[] args) { new SimpleFrame(); } } //from Finally, we have a main method to create the frame! Compile and run it!

Now you’re an expert on Java Graphics – NOT!!! However, you did create your first “Graphical User Interface”, or GUI (pronounced “gooey”, like something on your shoe at the movies). It’s a vast subject, but plenty of free resources. See more at: – examples of Java game and cellphone graphics - The beginner’s tutorial book to Swing graphics And search the web for other examples. Don’t forget the API!

Now try the “Graphics Lab”. You’ll build on the applets you saw in this lesson.