CS 121 Intro to Programming:Java - Lecture 14 Announcements

Similar documents
Using A Frame for Output

CS 335 Lecture 06 Java Programming GUI and Swing

public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

Graphical User Interfaces

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Remote Method Invocation

Homework/Program #5 Solutions

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Programming with Java GUI components

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Java Mouse and Keyboard Methods

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Mouse Event Handling (cont.)

core 2 Handling Mouse and Keyboard Events

GUI Components: Part 2

How to Convert an Application into an Applet.

Advanced Network Programming Lab using Java. Angelos Stavrou

Providing Information (Accessors)

The following four software tools are needed to learn to program in Java:


The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Extending Desktop Applications to the Web

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

During the process of creating ColorSwitch, you will learn how to do these tasks:

Event-Driven Programming

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Swing. A Quick Tutorial on Programming Swing Applications

GUI Event-Driven Programming

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Essentials of the Java(TM) Programming Language, Part 1

Lösningsförslag till tentamen

Animazione in Java. Animazione in Java. Problemi che possono nascere, e loro soluzione. Marco Ronchetti Lezione 1

Schueler-Organisiertes Lernen am Beispiel von Grafischen Benutzer-Schnittstellen in Java Tag der Offenen Tür - GTS 2008

JiST Graphical User Interface Event Viewer. Mark Fong

Skills and Topics for TeenCoder: Java Programming

Essentials of the Java Programming Language

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

11. Applets, normal window applications, packaging and sharing your work

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Artificial Intelligence. Class: 3 rd

How Scala Improved Our Java

The Basic Java Applet and JApplet

Tema: Encriptación por Transposición

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Lecture VII JAVA SWING GUI TUTORIAL

Analysis Of Source Lines Of Code(SLOC) Metric

Karsten Lentzsch JGoodies SWING WITH STYLE

Event processing in Java: what happens when you click?

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

Assignment # 2: Design Patterns and GUIs

The class JOptionPane (javax.swing) allows you to display a dialog box containing info. showmessagedialog(), showinputdialog()

JIDE Action Framework Developer Guide

How To Write A Program For The Web In Java (Java)

Construction of classes with classes

Tutorial Reference Manual. Java WireFusion 4.1

public class demo1swing extends JFrame implements ActionListener{

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM COURSE TITLE: ADVANCE JAVA PROGRAMMING (COURSE CODE: )

The Virtual Pest as a Java Applet Shannon McGinnis Tosolt, Nov. 2002

Dev Articles 05/25/07 11:07:33

Syllabus for CS 134 Java Programming

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Software Design: Figures

Simple Line Drawing. Description of the Simple Line Drawing program

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Summer Internship 2013

Introduction to the Java Programming Language

There are some important differences between an applet and a standalone Java application, including the following:

History OOP languages Year Language 1967 Simula Smalltalk

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

LAB4 Making Classes and Objects

Java Appletek II. Applet GUI

java.applet Reference

Java is commonly used for deploying applications across a network. Compiled Java code

Part IV: Java Database Programming

GOPAL RAMALINGAM MEMORIAL ENGINEERING COLLEGE. Rajeshwari nagar, Panapakkam, Near Padappai, Chennai

Flash MX Image Animation

16. Images in Java Applets and Applications

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CODE WIDTH HEIGHT ARCHIVE ALIGN VSPACE, HSPACE ALT APPLET APPLET ALIGN IMG

Java GUI Programming

Adobe Illustrator CS6. Illustrating Innovative Web Design

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Schoolwires Staff Website Reference Guide

1 Hour, Closed Notes, Browser open to Java API docs is OK

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Transcription:

CS 121 Intro to Programming:Java - Lecture 14 Announcements Next Owl assignment due friday, ch 13 up - the end Programming assignment 8 up - will be a work with a friend assignment. An alternate assignment also possible.. Final exam: 12/19, 8 AM Today: Graphics and the event model II - the mouse!

import java.awt.*; import javax.swing.*; public class DisplayWindow extends JFrame{ private Container c; public DisplayWindow(){ super("display"); c = this.getcontentpane(); public void addpanel(jpanel p){ p.setpreferredsize(new Dimension(500,400)); c.add(p); public void showframe(){ this.pack(); this.setvisible(true); this.setdefaultcloseoperation(jframe.exit_on_close);

import java.awt.*; import javax.swing.*; public class TryoutPanel extends JPanel{ private Color mycolor; public TryoutPanel(Color c){ mycolor = c;

Java s event model What modern computing is like.. What Scanner-based interactions are like.. (a script) Your program needs to know: 1) What to listen for 2) Clarify who s listening 3) Provide a mechanism for reacting properly to events

import java.awt.*; public class BabyButtonDriver{ public static void main(string[] args){ DisplayWindow d = new DisplayWindow(); BabyControlPanel p = new BabyControlPanel(); d.addpanel(p); d.showframe();

import java.awt.*; import javax.swing.*; import java.awt.event.*; // needed for event handling public class BabyControlPanel extends JPanel implements ActionListener{ JButton quit = new JButton("Quit"); // make button object public BabyControlPanel(){ setbackground(color.red); this.add(quit); // place button in panel quit.addactionlistener(this); // panel is listener for button public void actionperformed(actionevent e){ // what happens if (e.getsource() == quit) // when panel hears button System.exit(0);

public class BabyControlPanel2 extends JPanel implements ActionListener{ 1 JButton quit = new JButton("Quit"); // make button object JButton blue = new JButton("Blue"); // button to change color public BabyControlPanel2(){ // constructor 2 setpreferredsize(new Dimension(700,300)); setbackground(color.red); this.add(quit); // place button in panel this.add(blue); // add blue button quit.addactionlistener(this); // panel is listener for button blue.addactionlistener(this); // panel listener for blue button 4 3

public void actionperformed(actionevent e){ // what happens if (e.getsource() == quit) System.exit(0); if (e.getsource() == blue) setbackground(color.blue); 5 // when panel hears quit button // when panel hears blue button

Now we re going to start working directly with the mouse: FUN Here s a simple program - as you ve seen in the book - that writes out locations when you click the mouse

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LineClicker extends JPanel implements MouseListener{ int oldx = Integer.MIN_VALUE; int oldy = Integer.MIN_VALUE; int newx = 0; int newy = 0; public LineClicker(){ addmouselistener(this);

public void paintcomponent(graphics g){ g.drawline(oldx, oldy, newx, newy); public void mouseclicked(mouseevent e){ newx = e.getx(); newy = e.gety(); repaint(); public void mouseentered(mouseevent e){ public void mouseexited(mouseevent e){ public void mousepressed(mouseevent e){ public void mousereleased(mouseevent e){

The main points: MouseListener - not ActionListener The listener registration code is different There are five methods in the interface - but here we re only using 1! Another interface: MouseMotionListener (deals with mouse motion, not just button pressing..)

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Mouse2 extends JPanel implements MouseMotionListener{ int oldx = 0; int oldy = 0; int newx = 0; int newy = 0; public Mouse2(){ addmousemotionlistener(this);

public void paintcomponent(graphics g){ g.drawline(oldx, oldy, newx, newy); public void mousedragged(mouseevent e){ oldx = newx; oldy = newy; newx = e.getx(); newy = e.gety(); repaint(); public void mousemoved(mouseevent e){

Layouts Classes that allow you to organized GUI components physically FlowLayout GridLayout BorderLayout Another deeper point: Panels can be nested (that is, embedded in other panels) Following the lead of the text, we ll do a dummy calculator: +,-.*,/, and four other components

Flow Layout

public FlowDisplay(){ setlayout(new FlowLayout()); add(plus); add(minus); add(times); add(divide); add(enterlabel); add(enter); add(result); add(quit);

Grid Layout

public GridDisplay(){ setlayout(new GridLayout(4,2)); add(plus); add(minus); add(times); add(divide); add(enterlabel); add(enter); add(result); add(quit);

North West Center East South Border Layout Zones

Border Layout

import java.awt.*; import javax.swing.*; public class BorderDisplay extends JPanel { JButton plus = new JButton("+"); JButton minus = new JButton("-"); JButton times = new JButton("x"); JButton divide = new JButton("/"); JButton quit = new JButton("Quit"); JLabel enterlabel = new JLabel("enter: "); JTextField enter = new JTextField(8); JLabel result = new JLabel("0.0"); JPanel northpanel = new JPanel(); JPanel centerpanel = new JPanel();

public BorderDisplay(){ setlayout(new BorderLayout()); northpanel.add(plus); northpanel.add(minus); northpanel.add(times); northpanel.add(divide); add(northpanel,borderlayout.north); centerpanel.add(enterlabel); centerpanel.add(enter); centerpanel.add(result); add(centerpanel,borderlayout.center); add(quit,borderlayout.south);

MouseStretch extends JPanel implements ActionListener,Item CenterPanel centerpanel = new CenterPanel(); Private class CenterPanel extends JPanel implements MouseListener void mousepressed.. void mousereleased.. void paintcomponent..

public class MouseStretch extends JPanel implements ItemListener, ActionListener{ private ImageIcon mao = new ImageIcon("Mao.png"); private ImageIcon arches = new ImageIcon("Arches.png"); private ImageIcon ada = new ImageIcon("Ada.png"); ImageIcon which = mao; int width = 0; int height = 0; JButton quit = new JButton("Quit"); CheckboxGroup PixGroup = new CheckboxGroup(); Checkbox MaoBox = new Checkbox("Mao", true, PixGroup); Checkbox ArchesBox = new Checkbox("Arches", false, PixGroup); Checkbox AdaBox = new Checkbox("Ada", false, PixGroup); JPanel northpanel = new JPanel();

public MouseStretch(){ setlayout(new BorderLayout()); add(quit,borderlayout.south); quit.addactionlistener(this); CenterPanel centerpanel = new CenterPanel(); northpanel.add(maobox); MaoBox.addItemListener(this); northpanel.add(archesbox); ArchesBox.addItemListener(this); northpanel.add(adabox); AdaBox.addItemListener(this); add(northpanel,borderlayout.north); add(centerpanel,borderlayout.center);

private class CenterPanel extends JPanel implements MouseListener{ //(x1,y1), (x2,y2), are corners of rectangle that will hold //current image int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; private CenterPanel(){ addmouselistener(this); public void mousepressed(mouseevent e){ x1 = e.getx(); y1 = e.gety();

public void mousereleased(mouseevent e){ x2 = e.getx(); y2 = e.gety(); repaint(); public void paintcomponent(graphics g){ if(x1!= 0){ width = x2-x1; height = y2-y1; g.drawimage(which.getimage(),x1,y1,width, height,this); public void mouseentered(mouseevent e){ public void mouseexited(mouseevent e){ public void mouseclicked(mouseevent e){ // end of embedded class // end of main class