IT101. Graphical User Interface

Similar documents
Lecture 3: Java Graphics & Events

Lecture 5: Java Graphics

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

G51PRG: Introduction to Programming Second semester Applets and graphics

Computer Science 210: Data Structures. Intro to Java Graphics

Object-oriented programming in Java (2)

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

CSE 331 Software Design & Implementation

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Introduction to Computer Science I

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

CS 251 Intermediate Programming GUIs: Components and Layout

Graphics and Painting

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

Java Programming. Computer Science 112

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Programming Languages and Techniques (CIS120)

Programmierpraktikum

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

First Name: AITI 2004: Exam 2 July 19, 2004

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Object Orientated Programming in Java. Benjamin Kenwright

Exercise NMCGJ: Animated Graphics

AP CS Unit 12: Drawing and Mouse Events

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Graphical User Interface (GUI)

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Graphic User Interfaces. - GUI concepts - Swing - AWT

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Java Programming Lecture 6

What have we learnt last week? Wireless Online Game Development for Mobile Device. Introduction to Thread. What is Thread?

Strategy Pattern. What is it?

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Avoid Common Pitfalls when Programming 2D Graphics in Java: Lessons Learnt from Implementing the Minueto Toolkit

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

First Name: AITI 2004: Exam 2 July 19, 2004

CSC System Development with Java Introduction to Java Applets Budditha Hettige

2IS45 Programming

CS 11 java track: lecture 3

CS 2113 Software Engineering

1.00 Lecture 14. Lecture Preview

Class 14: Introduction to the Swing Toolkit

Here is a list of a few of the components located in the AWT and Swing packages:

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

CSC207 Week 4. Larry Zhang

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

AP CS Unit 11: Graphics and Events

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

ming 3 Resize the Size to [700, 500]. Note that the background containers in the program code: reference the then under () { 153, 255, 0 ) );

Introduction to Processing

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

CS Exam 1 Review Suggestions

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Graphical User Interfaces (GUIs)

UI Software Organization

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CS Fall Homework 5 p. 1. CS Homework 5

Introduction to the JAVA UI classes Advanced HCI IAT351

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Graphical User Interface (GUI)

Programming Language Concepts: Lecture 8

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

User interfaces and Swing

Programming: You will have 6 files all need to be located in the dir. named PA4:

AplusBug dude = new AplusBug(); A+ Computer Science -

Graphic Interface Programming II Events and Threads. Uppsala universitet

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

2. (10 pts) Which of the following classes implements the Shape interface? Circle all that apply. a) Ellipse2D b) Area c) Stroke d) AffineTransform e)

2110: GUIS: Graphical User Interfaces

CSE 331 Software Design & Implementation

Come & Join Us at VUSTUDENTS.net

CS/120 Final Exam. Name

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Windows and Events. created originally by Brian Bailey

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Mobile Systeme Grundlagen und Anwendungen standortbezogener Dienste. Location Based Services in the Context of Web 2.0

Garfield AP CS. Graphics

Midterm assessment - MAKEUP Fall 2010

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Chapter 12 Advanced GUIs and Graphics

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Lecture 18 Java Graphics and GUIs

Transcription:

IT101 Graphical User Interface

Foundation Swing is a platform-independent set of Java classes used for user Graphical User Interface (GUI) programming. Abstract Window Toolkit (AWT) is an older Java GUI technology, some of which is still useful. Create a new Java Application project in NetBeans; Create a main class named TennisGame; Import javax.swing.*, java.awt.* and java.awt.geom.* packages into the TennisGame class. Make your TennisGame class extend the JPanel class. JPanel is a painting canvas, by extending which you will be able to paint and display objects in TennisGame. A painting canvas must be contained in a window frame. JFrame is the class that serves as a frame for our TennisGame canvas. Create a new JFrame object inside the TennisGame's main method. Create an instance of the TennisGame class and add the TennisGame object reference to the frame. Set the frame's size using the frame's setsize method. Set the frame's visibility to "true". Make sure the Java thread that runs your program shuts down when the frame is closed. Build and run your program. You should see an empty 300px by 300px box.

Paint the Objects Start painting the tennis ball by overriding the JPanel's paint method: public void paint(graphics g); The paint method receives by parameter a Graphics object. We will use a newer object: Graphics2D, which extends from Graphics. In order to do that cast Graphics g into Graphics2D. To draw something inside the canvas we should indicate in which position we are going to start painting. For this, each of the points in the canvas has an associated position (x,y) being (0,0) the point of the topleft corner. Set the color. Fill the tennis ball with color. Draw the ball. Build and run your program. You should see the red tennis ball at the top left of your canvas.

Animate Every time we paint something we have to define its position (x,y). To make the ball move, modify the position (x,y) each time and repaint the circle in the new position. In our example, we keep the current position of our circle in two properties called "x" and "y". We also create a method called moveball() which will increase in 1 both "x" and "y", each time we call it. At the end of the main method we start an infinite loop "while (true)" where we repeatedly call moveball() to change the position of the circle and then call the JFrame's repaint() method. "Thread.sleep(10)" tells the processor that the Java thread which is being run must sleep for 10 milliseconds, which allows the processor to execute other threads and in particular the AWT thread which calls the paint method. Build and run your program. You should see the red tennis ball moving through the canvas.

Encapsulate the Ball Create a class called Ball which isolates everything that has to do with the ball. Make the ball go in different directions. Integers "xa" and "ya" represent the speed in which the ball is moving. If xa=1, the ball moves to the right, one pixel every round of the Game Loop, if xa=-1, the ball moves to the left. In the same way ya=1 moves the ball down and ya=-1 moves the ball up. This is done with the lines, "x = x + xa" and "y = y + ya" of the moveball() method. Verify that the ball does not go out of the borders of the canvas. For example, when the ball gets to the right border, or when (x + xa > getwidth() - 30), what we`ll do, is change the direction of the movement on the "x": "xa = -1".

Encapsulate the Racquet Create a class called Racquet which isolates everything that has to do with the racquet. Unlike "Ball", "Racquet" does not have any properties for the position "y" or for the speed "ya". This is because the racquet doesn t change its vertical position; it will only move left or right, never up or down. In the paint method, the g.fillrect(x, 330, 60, 10) instruction defines a rectangle of 60 by 10 pixels in the position (x,y)=(x,330). As we can see "x" can change but "y" is fixed in 330 pixels from the top border of the canvas. When someone presses a key, the keypressed method of "Racquet" will be called and this will set "xa" to 1, if the key pressed is the right direction (KeyEvent.VK_RIGHT), that will move the racquet to the right. In the same way if we press the key KeyEvent.VK_LEFT it will move to the left. When a key is released, the method keyreleased is called and "xa" changes its value to zero, which makes the racquet stop.

Revised TennisGame.java Instantiate Ball and Racquet objects in TennisGame; Add a TennisGame constructor. To read from the keyboard it is necessary to register an object which is in charge of "listening if a key is pressed". This object is known as "Listener" and it will have methods that will be called when someone presses a key. In our example the Listener is included in the TennisGame constructor. Add the move method to TennisGame. move calls Ball and Racquet move methods. Change the while loop in the main method to call the TennisGame's move method.

Homework We will continue the Tennis Game project next week. You should have the TennisGame, Ball and Racquet classes ready (as shown above) for the next class. Build and run your project. Make sure your code compiles without errors, and that you see the Ball moving across the canvas, and that you can control the Racquet movement using the left and right arrow keyboard keys.