Graphics in Java Starring: NetBeans Co-Starring: Java.awt.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
A graphical user interface (GUI) is a pictorial interface to a program. A good GUI can make programs easier to use by providing them with a consistent.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
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.
Applets. 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 browser You.
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.
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Programming Task: Task 1 Controlled Assessment Practice.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Io package as Java’s basic I/O system continue’d.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Applets Java API.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
JAPPLET.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Java Applets. 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 browser.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Alice in Action with Java Chapter 14 Events and GUIs.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Applets.
A First Look at GUI Applications
Lecture 09 Applets.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
UNIT-5.
Java Applets.
Presentation transcript:

Graphics in Java Starring: NetBeans Co-Starring: Java.awt

Graphics – An Introduction  In order to show you Java graphics, we will walk you through the basics of making a project using the AWT package  AWT is a group of classes that work together to create programs that the user can interact with graphically

Graphics  AWT creates a GUI (graphical user interface) allowing you to avoid using the console which can only display a limited range of data  We will also be basing our code on events – things that the user does to move the program along such as clicking a button or pressing a key

Applets  Our code will be based upon applets, which are similar to applications, but are designed to operate off of an html enabled web browser  Consequently, they are driven by a short html code sequence, rather than an SPVM  Fortunately, most of this is abstracted from the user, so the coding is very similar to that of an application

Lets Get Started!  Open up NetBeans™  Click New Project ->new as you usually do  Select “General” AND “Java Class Library”  Fill in the same boxes you would with an application, be sure to name the applet and select an appropriate location (folder)

Creating an AWT Applet  You need to create a new java file in which to build your applet.  Right-click the project “Source Package” Folder  Select “Java Classes” and Applet or JApplet  Name your Applet and associate it with the Source Packages  Finish

Creating an AWT Applet  You COULD use NetBeans GUI capability to generate a frame that behaves much like a VB form  With this Frame you can add objects like you did in VB  However, we are leaving out this topic for now, However, you can utilize ANY capability of NetBeans (on your own)

The Code  Take note of the absence of an SPVM public class MyApplet extends java.applet.Applet { /** Initialization method that will be called after the applet is loaded * into the browser. */ public void init() {

 The Init() is where we put the initial behaviors we want to execute when the applet “loads”  Remember Applets execute from HTML, to have the HTML generated, RUN the java applet  If you want to see the html code that our applet will run upon, look in the projects “Build” Folder in Windows

Applet HTML Page Applet HTML Page Generated by NetBeans IDE

 At This Point, you could EXECUTE your applet simply by clicking on the HTML Document  It will invoke Internet Explorer and IE will generate your applet

The Java File  Find the init () method  That is where the code creating objects goes  We will code the creation of objects manually, but you can use Netbeans to graphically build your GUI (again, we will discuss this at a later time, however, feel free to experiment now)

Hello World  Lets generate our first applet that displays the infamous “Hello World”  Create a paint method as follows: public void paint(Graphics g) { g.drawString("Hello World", 50, 25); }

Hello World  Make sure we import the required java classes, so include these at the top of the program: import java.applet.Applet; import java.awt.Graphics; import java.awt.*; import java.awt.event.*;  Now, run your applet by r-clicking on the java file and selecting RUN

 Now, we will extend out applet to:  Add a button and a label  Set properties of each  Allow button events to be captured and processed  Use “interfaces” to trap KEY EVENTS

 Use the class handout or the completed applet online to add a button and a label to your applet  Remember to run your applet by r-click on the java file and selecting RUN  Sample output …

Classes  Recognize that all of the things you add to the applet are objects, and can be viewed in JavaDocs  Consequently they all have methods and attributes  Try looking up Button in JavaDocs and find its methods and attributes

More on Classes  Right after the class declaration and before the constructor, you should find the button declaration: button1 = new Button()  Notice the new, indicating the creation of an object

Still More on Classes  Now go down to the init () method  Look at the various “set” methods called on the button  These methods set the values for the various attributes of the button object  Finally, look at the “add” call on the button, which places the button in the applet

Mess Around  Try changing the various properties of the button  Then create a label, a textbox, and whatever else you want  Try changing the properties of these objects, and see what you can do

Now For Some User Input  Graphics allow the user to control various aspects of the program  User inputs such as pressing a button or hitting a key are called ActionEvents  You can write code to make your project react to these ActionEvents and do virtually anything

ActionListener  If you want a graphics component to respond to some user input, such as the clicking of a button, an “ActionEventListener” must be added to the component.  This listener does what it sounds like—it “listens” for the activation of a component  The listener in turn activates an outside method that includes the actual code that is executed when a component is activated (button is clicked, etc)

What it looks like…

Interpretation  In the previous slide, you can see the ActionListeners for the button.  On the bottom are the “ActionPerformed methods. In them goes the code describing what the buttons should do when they are clicked

Additional Events  Of course, you might also need to handle additional events, such as moving the mouse over a component, or clicking and dragging. Thus, there are numerous other types of events that all operate in a similar manner  Take a look at JAVADOCS to see what additional events exist in Java  For your purposes, ActionEvents are sufficient for the time being, as they are the simplest and are automatically configured for the default actions of every component.

Experimentation  Try fooling around with ActionListeners and “ActionPerformed” methods to get a better feel for how they operate.  As a short exercise, try making an applet with a button and a label, where the color of the label changes when you click the button.

Solution

Control Arrays  In more complex Applets, the amount of components increases to the point where it becomes inefficient to code for the individual ones if they operate in a similar manner.  If you have ten buttons that do similar things, it might be convenient to create an array of buttons, to hold them in an organized way.

Tic-Tac-Toe  Your first real project is to complete a Tic- Tac-Toe program using graphics as well as a control array.  Naturally, because you would need nine buttons, one for each square in the 3x3 grid, an array would be perfect for this project.  This would reduce some of the redundant code, as all of the ActionEvent handling would occur in the same ActionPerformed method!  So instead of nine ActionPerformed methods, we only need one.

Creating the Control Array  You would use a for loop to instantiate all of the buttons  Remember—no “new,” no new object!  Take a look at the sample code.

Using the control array  When instantiating the control array, it is useful to set the ActionListeners in the loop, but make sure that they point to the same ActionPerformed method, such as boardActionPerformed where board is a 3x3 control array of buttons.  In the ActionPerformed, use the getSource() method from the ActionEvent class, which returns the actual button that has been clicked.

Using the control array  For example,  public void boardActionPerformed(ActionEvent e)  {  Button b = ((Button) e.getSource());  //more code follows  }  That will do the trick.

Swing  In addition to the awt hierarchy of classes used for graphics, there is another hierarchy, called swing.  Swing is built upon awt, and it provides additional features but can be harder to use  Feel free to experiment with swing and look at the documentation for the swing classes in JAVADOCS  You can tell the difference between awt and swing very easily—swing class names all have the letter ‘J’ at the beginning  For example, JButton and JFrame are in swing, whereas Button and Frame are in awt.

Vectors  You can also have JAVA draw shapes and lines on the screen through the use of vectors  Java can make them move around with a Thread  Threads basically allow for the motion of time in a project  A BufferedReader allows for the smooth motion of time via a thread  Let’s take a look at a project that uses Vectors to better understand how they operate

Stealth Project (Created by Alex Carpenter)  This project uses several different.java files, all of which work together to create the project  Open it up, and take a look at the main stealth file.  As you might realize, it has a variety of objects, and Event Listeners (sound familiar?)  The paint method does what it sounds like – it paints the screen with the component objects  Calling.repaint() invokes the paint method again

Project #2 – Change Stealth  Increase the functionality of the Stealth project  Change the project so that the small semicircle will move based on the arrow keys on the keyboard, not the mouse.  You will need to use keyListeners and keyEvents (look them up in JAVADOCS)  Most of what is initially there can be reused – you don’t need to reinvent what already works

Project #3  Dice Rolling!  You must make a project that simulates a dice rolling game, where 2 dice are displayed graphically.  It must involve 2 players, who alternate turns.  There must be text fields at the beginning, where the 2 players can write their names  There must be a “roll” button, so the players can roll the 2 dice  The cumulative scores (sum of the dice rolls) must appear near the players’ names.  The first player to reach 50 wins, and the program must keep track of the wins

The End…Or is it?  There is much more to Graphics in JAVA than what is described in this presentation  We have given you the basics, but there is much more to graphics in JAVA than meets the eye  It is up to you to decide how much Graphics you want to learn!