Programmierpraktikum

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

H212 Introduction to Software Systems Honors

Introduction to the JAVA UI classes Advanced HCI IAT351

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

Window Interfaces Using Swing Objects

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Window Interfaces Using Swing Objects

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!

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

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

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

CS 251 Intermediate Programming GUIs: Components and Layout

Programming Languages and Techniques (CIS120e)

Systems Programming Graphical User Interfaces

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Lecture 9. Lecture

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Lecture 5: Java Graphics

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Graphical User Interfaces. Swing. Jose Jesus García Rueda

IT101. Graphical User Interface

AP CS Unit 11: Graphics and Events

Java. GUI building with the AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphical User Interfaces. Comp 152

Graphical User Interface (GUI)

G51PRG: Introduction to Programming Second semester Applets and graphics

Object-oriented programming in Java (2)

Java: Graphical User Interfaces (GUI)

Programming graphics

Programming Languages and Techniques (CIS120)

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Part 3: Graphical User Interface (GUI) & Java Applets

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

RobotPlanning.java Page 1

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Programmierpraktikum

CSC 1214: Object-Oriented Programming

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

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

Swing from A to Z Some Simple Components. Preface

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

COMPSCI 230. Software Design and Construction. Swing

Graphical User Interface (GUI)

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

Computer Science 210: Data Structures. Intro to Java Graphics

Graphical User Interfaces 2

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Graphical interfaces & event-driven programming

CS 2113 Software Engineering

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Packages: Putting Classes Together

User interfaces and Swing

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

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 :) )

HW#1: Pencil Me In Status!? Random tips and pointers. Summary. Today. Handouts. 2 Handout for today! Assigned Work Reminder:

PIC 20A GUI with swing

Final Examination Semester 2 / Year 2010

Lecture 3: Java Graphics & Events

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

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

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Building Graphical User Interfaces. GUI Principles

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

SINGLE EVENT HANDLING

Introduction. Introduction

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces (GUIs)

Building Graphical User Interfaces. Overview

Final Examination Semester 2 / Year 2011

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

CompSci 230 S Programming Techniques. Basic GUI Components

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Programming Language Concepts: Lecture 8

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

CHAPTER 2. Java Overview

Assignment 2. Application Development

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Graphical User Interface (GUI)

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

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

MIT AITI Swing Event Model Lecture 17

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Chapter #1. Program to demonstrate applet life cycle

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

DM503 Programming B. Peter Schneider-Kamp.

Swing. By Iqtidar Ali

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Transcription:

Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 25 17/01/13 11:45

Swing Graphical User Interface (GUI) 2 of 25 17/01/13 11:45

Graphical User Interface (GUI) interaction and control of a program via a graphical interface for most humans GUI == program 3 of 25 17/01/13 11:45

4 of 25 17/01/13 11:45

Swing example 1. import javax.swing.*; // 'x' for extension 2. import java.util.*; // for sleeping 3. 4. 5. 6. 7. 8. public class SimpleFrame { public static void main(string args[]) throws InterruptedException // needed for sleeping { JFrame jf = new JFrame(); // instantiation of a JFrame 9. JLabel jlb = new JLabel("Hello World"); // instantiation of a JLabel 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. jf.getcontentpane().add(jlb); // add label to content of frame jf.setsize(300, 200); // set frame size jf.setdefaultcloseoperation(jframe.dispose_on_close); // what to to when clicken away the window: remove it, free resources jf.setvisible(true); // show frame System.out.println("JFrame panel lauched"); // Thread.sleep(3000); // wait 3 seconds jf.setlocationrelativeto(null); // move to center of screen System.out.println("JFrame panel relocated"); 5 of 25 17/01/13 11:45

20. // 21. 22. 23. 24. 25. 26. Thread.sleep(3000); jlb.setfont(jlb.getfont().derivefont(20f)); // set font size System.out.println("JLabel font resized"); // // end of class SimpleFrame 6 of 25 17/01/13 11:45

layers (panes) computer graphics images have several layers JFrame contains several pane objects: root, content, layered, glass normal applications only use the content pane, it contains all content frame and window hierarchy JFrame extends Frame (with title and border) Frame extends Window (no title and border) Window extends Container Container extends Component Component extends Object 7 of 25 17/01/13 11:45

Container: Abstract Window Toolkit (AWT), contains component objects Component: interactive graphical object, buttons, checkboxes, scrollbars,... many things can be done using a Frame or a Window, a JFrame offers most utilities 8 of 25 17/01/13 11:45

moving colored JFrame Color(red,green,blue): rgb color Point point comp.getlocation(null): a point object is a 2D vector (point.x,point.y) 1. 2. 3. import javax.swing.*; // for the frame import java.awt.*; // for colors import java.util.*; // for sleeping 4. 5. public class MovingScreen extends JFrame { 6. 7. 8. 9. 10. 11. 12. 13. 14. /* Constructor of MovingScreen. * Using 'this' is not necessary but increases readability. * 'title', 'size',.. are member variables of JFrame. **/ public MovingScreen() { this.settitle("moving colors"); this.setsize(300, 200); this.setdefaultcloseoperation(exit_on_close); // exit program on close 9 of 25 17/01/13 11:45

15. 16. 17. public static void main(string[] args) throws InterruptedException { 18. 19. 20. 21. 22. 23. 24. 25. MovingScreen ex = new MovingScreen(); ex.setlocationrelativeto(null); ex.setvisible(true); int screencenter_x = ex.getlocation(null).x; int screencenter_y = ex.getlocation(null).y; for (int i=0;i<255;i++) { Thread.sleep(40); // instantiation // relative to screen center // Point.x // Point.y 26. 27. 28. 29. 30. ex.getcontentpane().setbackground(new Color(0,i,255-i)); ex.setlocation(screencenter_x+i,screencenter_y+i); // end of MovingScreen.main() // end of class MovingScreen 10 of 25 17/01/13 11:45

adding content to a frame adding content using a container container.add(component comp) is inherited by the JFrame the JPanel container is convenient (comes with a layout manager): jframe.add(jpanel jp) JPanel extends JComponent JComponent extends Container adding content directly to the ContentPane jframe.getcontentpane.add(component comp) 11 of 25 17/01/13 11:45

doese not come with a predefined layout manger: jframe.getcontentpane.setlayout(layoutmanger) 12 of 25 17/01/13 11:45

Swing components there are several layout managers for arranging components on contentpane 1. 2. import java.awt.*; // for the container import javax.swing.*; // for the frame 3. 4. public class AddContentPane { 5. 6. 7. 8. 9. 10. public static void main(string[] args) { JFrame frame = new JFrame("Various Components"); frame.setsize(600, 150); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.dispose_on_close); 11. 12. 13. Container content = frame.getcontentpane(); // add to contentpane content.setlayout(new FlowLayout()); // one is necessary 14. 15. 16. content.add(new JButton("JButton")); content.add(new JToggleButton("JToggleButton")); 13 of 25 17/01/13 11:45

17. 18. 19. content.add(new JCheckBox("JCheckBox")); content.add(new Label("Label")); content.add(new JRadioButton("JRadioButton")); 20. 21. Font font = new Font("Arial Black",Font.PLAIN,20); // select a new font 22. 23. Component[] allcomp = content.getcomponents(); // get all components 24. 25. for (Component comp : allcomp) // iteration { 26. comp.setbackground(new Color(153,204,153)); // of component 27. 28. 29. comp.setforeground(new Color(204,51,51)); // change font color comp.setfont(font); // change font sizes 30. 31. content.setbackground(new Color(204,204,204)); // of contentpane 32. 33. frame.setvisible(true); // show 34. 14 of 25 17/01/13 11:45

events - making things happen a GUI needs to be interactive react on user action Java listener listen to what happens in the world: other components of the program communication with remote hosts incoming emails,... ActionListener: waits until event occurs and then performs.actionperformed() register action listener with component 1. public class MyClass implements ActionListener { 15 of 25 17/01/13 11:45

2. // ActionListener is an Interfaces not a class and hence 3. 4. 5. 6. 7. // implemented and not extended public void actionperformed (){ // mandatory: do stuff when action has been triggered 16 of 25 17/01/13 11:45

simple Swing events actionevent.getsource() returns an Object: the source of the event 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. import java.awt.event.*; import java.awt.*; import javax.swing.*; public class SimpleEvent extends JFrame implements ActionListener { JButton button1, button2, button3; JLabel label; /* Constructor. **/ public SimpleEvent(){ this.settitle("click buttons"); // for events // for colors // for JFrame // extending an class // implementing an interface // needed several times // title 17 of 25 17/01/13 11:45

17. this.setsize(400, 200); // size 18. this.setdefaultcloseoperation(jframe.dispose_on_close); 19. 20. 21. 22. 23. 24. 25. 26. this.setlocationrelativeto(null); this.label = new JLabel(); this.button1 = new JButton("text"); this.button2 = new JButton("color"); this.button3 = new JButton("exit"); // position // initially empty 27. 28. 29. this.button1.addactionlistener(this); // register listeners this.button2.addactionlistener(this); // 'this' implements this.button3.addactionlistener(this); // an ActionListener 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(label); panel.validate(); this.add(panel); // end of constructor // for the content 18 of 25 17/01/13 11:45

40. /* Main. 41. **/ 42. 43. 44. 45. 46. public static void main(string[] args) { SimpleEvent window = new SimpleEvent(); window.setvisible(true); 47. 48. 49. 50. /* ActionPerformed is mandatory since SimpleEvent * implements ActionListener. **/ 51. 52. 53. 54. 55. public void actionperformed (ActionEvent ae){ if(ae.getsource() == this.button1){ label.settext("you successfully clicked button 1"); 56. 57. 58. 59. 60. if(ae.getsource() == this.button2){ label.settext(("changing colors of button 2")); button2.setbackground(new Color(230,230,130)); 61. 62. if (ae.getsource() == this.button3){ 19 of 25 17/01/13 11:45

63. 64. 65. 66. System.exit(0); // end of SimpleEvent.actionPerformed() // end of SimpleEvent 20 of 25 17/01/13 11:45

list of events event typ event processor event trigger Action JButton button activation Adjustment JScrollBar on change CaretEvent JTextComponent moving cursor Component Component changing visbility, size Container Container changing content Focus JComponent new focus (for typing) Key JComponent keyboard activation Menu JMenu on selection Mouse JComponent mouse in/out MouseMotion JComponent mouse motion 21 of 25 17/01/13 11:45

graphics and drawing all graphics operations onto a graphics object Graphics2D extends Graphics: more options JComponent.paintComponent(Graphics g) graphics objects automatically painted/repainted interactive components / graphics 1. 2. 3. 4. import javax.swing.*; import java.awt.*; public class DrawingColor { // for JFrame, JCompoent // for graphics objects 5. 6. 7. 8. 9. 10. 11. public static void main(string[] args) { JFrame frame = new JFrame("drawing colors"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(500,300); frame.setlocationrelativeto(null); frame.getcontentpane().add(new MyComponent()); // add content frame.setvisible(true); 22 of 25 17/01/13 11:45

12. 13. // end of class DrawingColor 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. /** User defined component, extending the * abstract class JComponent. */ class MyComponent extends JComponent{ /** Paints the component when first shown or needed. * Overrides the super.paintcomponent of the * JComponent. Painting and drawing is down on a * Graphics object. Graphics2D extends Graphics and * has more options. */ public void paintcomponent(graphics g){ int width = 200; int height = 120; // g.setcolor(color.blue); // select color g.filloval(20,20,width,height); // filled elipse g.setcolor(color.cyan); g.filloval(24,24,width,height); // g.setcolor(color.gray); 23 of 25 17/01/13 11:45

35. 36. 37. 38. 39. g.fillrect(270+width/2,20+height/2,width/2,height/2); // filled rectangle // Graphics2D g2d = (Graphics2D)g; // cast Graphics to Graphics2D g2d.setstroke(new BasicStroke(5)); // only availabe in Graphics2D g2d.setcolor(color.red); 40. 41. 42. 43. 44. 45. 46. g2d.drawrect(270,20,width/2,height/2); // int[] xpoints = new int[200]; int[] ypoints = new int[200]; for (int x=0; x<200;x++) { xpoints[x] = 50 + 2*x; // borders thickness is stroke 47. 48. 49. 50. 51. 52. ypoints[x] = 200 + (int)(40.0*math.sin(math.pi*x/25.0)); g.setcolor(color.black); g.drawpolyline(xpoints,ypoints,xpoints.length); // end of MyComponent.paintComponent() // end of class MyComponent 24 of 25 17/01/13 11:45

exercise: GUI for Kepler problem Write a GUI for the numerical solution of the Kepler problem with Runge-Kutta integration. add an JFrame with suitable layout manager to your Kepler program the GUI should allow the user to select all parameters starting position/velocity power of the force field integration method the orbits are drawn to a reserved part of the GUI 25 of 25 17/01/13 11:45