Day 6 Some Basic Graphics

Size: px
Start display at page:

Download "Day 6 Some Basic Graphics"

Transcription

1 Introduction Day 6 Some Basic Graphics The preceding days have focused on basic Java syntax and those concepts fundamental to object-oriented programming: encapsulation, inheritance and polymorphism. We now change directions a bit and move into a how-to phase. In the following section, we will describe the classes and methods necessary to set up a simple GUI (graphical user interface) and do some graphics programming. Finally, we use a few graphics methods and a little recursion to draw a simple fractal, Sierpienski s gasket. Java Graphics Terms We begin with a short vocabulary of graphics terms: A Component is anything that is displayed on the screen, e.g., windows, buttons, text boxes, labels etc. All component classes extend the Component class. Components are sometimes called widgets. A Container is an object that can hold components. A container is a component that can contain other objects. The class Container is a subclass of Component so a Container is-a Component. A Window object is a window without a title bar. A Frame is a window with a title bar and border. To create a user interface you must first create a frame (or applet) to hold the components of the interface. A frame contains sub-elements called Panes. Most of the time you will use a window pane, called the content pane. The content pane is where you normally add components. A Panel is an invisible container that holds user-interface components. Normally you add components such as buttons to a panel and then you add the panel to the content frame. Component hierarchy The Abstract Windows Toolkit and Swing Java provides two libraries that contain the classes for graphics programming: the original Abstract Windows Toolkit (AWT) and the newer Swing library. AWT was the original class library that Sun provided for graphics programming. While programs designed with AWT components (buttons, text fields, menus etc.) run on any platform (e.g., Windows, Unix) such components used the interface elements of the particular platform. In other words, a button on a Windows machine may not look or behave the same way as a button on a Unix machine or an Apple. GUI programs designed on one platform did not always work well on another. The more modern Swing library paints the components on the screen so that the look and feel of a GUI is consistent from platform to platform. Swing does not replace AWT; in fact, Swing uses many AWT classes. Swing does provide new user interface components (e.g., buttons, textboxes, check boxes etc.) which are platform independent. The following chart compares Swing and AWT. 199

2 AWT in java.awt package each component was mapped to a corresponding platform-dependent interface called a peer. platform specific and prone to platform specific bugs. components may look different on different platforms. Components have the look of a particular platform. Swing in javax.swing package no platform dependent peers. Components have a Java look. code written in Java. components are called lightweight. components are all prefixed with J, e.g., JButton, JLabel. components are called heavyweight. The Java component hierarchy has the following form: 200

3 Component Class Methods: Every component in the hierarchy inherits the following methods whose purposes are evident from their names. void setsize(int width, int height) void setlocation(int xcoord, int ycoord) // move the top left corner of the component to point (x, y) of the container. //The upper left corner of a container has position (0, 0) and (x, y) is the point located x //pixels to the right of and y pixels down from (0, 0). void setbounds(int x, int y, int width, int height) // moves component to position (x, y) and resizes void setenabled(boolean x) // e.g., a button can be greyed0ut void setvisible(boolean x) void setname(string name) int getheight() int getwidth() int getx() //gets x-coordinate of the component int gety() // gets y-coordinate String getname() boolean isenabled() boolean isvisible() //Methods of the Frame Class: void settitle(string title) void setresizable(boolean x) void show() void hide() void toback() // sends window to the back void tofront() //sends window to the front 201

4 Example: The following program will create a frame with title I ve been framed. You should note that the upper left corner of the screen has coordinates (0, 0) and that the screen point (x, y) is located x pixels to the right of and y pixels down from (0, 0). Consequently, a call to setbounds (x, y, width, height) by a JFrame object places the upper left corner of the frame at position (x, y) relative to the upper left corner of the screen. Also, notice the close() method in main(). import javax.swing.*; public class MyVeryFirstFrame extends JFrame public MyVeryFirstFrame () settitle("i've been framed!"); setbounds(267,200,267,200); public MyVeryFirstFrame (String title) settitle(title); setbounds(267,200,267,200); public class GUI1 JFrame frame = new MyVeryFirstFrame ("------I've been framed!----"); frame.show(); frame.setresizable(false); public class GUI0 //alternate version JFrame frame = new JFrame(); frame.settitle("i ve been framed!"); frame.setbounds(250,250,200,200); frame.setvisible(true); frame.setresizable(true); 202

5 Centering a Frame You can use methods from the Toolkit class (in AWT) to center a frame. Simply instantiate a Toolkit object and invoke the following methods: Toolkit getdefaulttoolkit() //static method returns Toolkit object for the current system Dimension getscreensize() //not static The Dimension class has two accessible fields: int height int width Thus if d is a Dimension object, d.height and d.width represent the height and width of the screen. The following program centers a frame relative to the screen. Example: import java.awt.*; import javax.swing.*; public class AnotherFrameClass extends JFrame public AnotherFrameClass() settitle("i am framed!"); setbounds(267,200,267,200); public AnotherFrameClass(String title) settitle(title); setbounds(0,0,300,300); public class GUI2 JFrame frame = new AnotherFrameClass("------I am framed!----"); frame.show(); frame.setresizable(false); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getscreensize(); int width = 300; int height = 300; System.out.println("Screen: "+d.width+" "+d.height); System.out.println("Top corner: " + ( d.width-width)/2+ " "+ (d.height-height)/2); frame.setbounds((d.width-width)/2, (d.height-height)/2,width,height); 203

6 The picture below may be a bit deceiving because the frame is placed in the middle of the screen and not the DOS window. 204

7 The window (or its subclasses Frame /JFrame) is a top-level container. You cannot add components directly to a frame. You must first add the components to the ContentPane and then add the ContentPane to the frame. A Frame is called a top-level Container and a ContentPane an intermediate level Container. Adding a Button to a Frame How to add components to the content pane: To add components to the content pane, invoke the getcontentpane() method in JFrame. This method returns the content pane as a Container object. The method add(component x) (in the Container class), adds the component to the calling container. Example: The following example will add a simple button to a frame. A button is a member of the JButton class: JButtonClass Constructors: JButton() JButton(String text) //Other methods in JButton void settext(string text) String gettext() void setenabled(boolean x) We can now add a button. public class GUI3 JFrame frame = new AnotherFrameClass("------I am framed----"); Container contentpane = frame.getcontentpane(); JButton button = new JButton("Hello"); contentpane.add(button); frame.show(); 205

8 Example: Here we add two buttons--- or do we? public class GUI3a JFrame frame = new AnotherFrameClass("------I am framed!----"); Container contentpane = frame.getcontentpane(); JButton button1 = new JButton("Hello"); JButton button2 = new JButton("Bye"); contentpane.add(button1); contentpane.add(button2); frame.show(); 206

9 Aren t there supposed to be two buttons? What happened to the Hello button? Well, yes, there are two buttons, but the second was placed on top of the first. This is not a particularly desirable action. However, Java provides layout managers to help with the placement of components in a frame (content pane). Containers use layout managers (from the LayoutManager classes) to arrange their components. In the previous example, the default LayoutManager placed both buttons in the ContentPane in the center and one on top of the other. However, there are several LayoutManager classes available so that you can have a bit more control over the placement of components. A LayoutManager object will help with placement of components. The first and simplest of the Layout Managers is the FlowLayoutManager. The FlowLayoutManager arranges components in a container left to right, in the order in which they are added to the container. Constructors of FlowLayout Class FlowLayout() constructs a FlowLayout manager with centered alignment, i.e., components will be centered in a Container. FlowLayout(Alignment) constructs a FlowLayout manager with specified alignment: CENTER, LEFT, or RIGHT FlowLayout(Alignment, int x, int y) constructs a FlowLayout manager with specified alignment. Parameters x and y represent horizontal and vertical space between components. The alignment fields of the FlowLayout class, CENTER, LEFT, and RIGHT are accessed as FlowLayout.CENTER etc. 207

10 Method of the Container class needed to layout components setlayout(layoutmanager m) sets the layout manager for this container For example: If cp represents the content pane for some frame then cp.setlayout (new FlowLayout()); causes components to be added to the content pane using a FlowLayout object. Example: In the following example 10 buttons are added to the content pane of a frame. The buttons are left justified. public class GUI4 JFrame frame = new AnotherFrameClass("------I am framed----"); Container contentpane = frame.getcontentpane(); contentpane.setlayout(new FlowLayout(FlowLayout.LEFT)); for(int i = 0; i<10; i++) contentpane.add(new JButton("Button "+i)); frame.show(); 208

11 Here is a different version with center alignment: Java and Object Oriented Programming public class GUI4a JFrame frame = new AnotherFrameClass("------I have been framed----"); Container contentpane = frame.getcontentpane(); JButton button; contentpane.setlayout(new FlowLayout(FlowLayout.CENTER)); for(int i = 0; i<10; i++) button = new JButton("Button "+i); contentpane.add(button); frame.show(); 209

12 BorderLayout Manager The BorderLayout manager divides the window into five areas: NORTH WEST SOUTH EAST CENTER Constructors: BorderLayout() BorderLayout(int horizontalgap, int verticalgap) Component method: add(component, region) Constants: NORTH WEST SOUTH EAST CENTER Example: public class GUI5 JFrame frame = new AnotherFrameClass("------I am framed----"); Container contentpane = frame.getcontentpane(); JButton button; contentpane.setlayout(new BorderLayout()); contentpane.add(new JButton("Dewey"), BorderLayout.NORTH); contentpane.add(new JButton("Hughie"), BorderLayout.EAST); contentpane.add(new JButton("Louie"), BorderLayout.WEST); frame.show(); 210

13 GridLayout Manager The GridLayout manager arranges the components in a grid/table/matrix like fashion, left to right, top to bottom, row by row. Constructors GridLayout(int rows, int cols) GridLayout(int rows, int columns, int x, int y) x and y are the horizontal and vertical gaps between components Gridlayout() one column in a single row Example: public class GUI6 JFrame frame = new AnotherFrameClass("------I am framed----"); Container contentpane = frame.getcontentpane(); JButton button; contentpane.setlayout(new GridLayout(3,2)); for(int i = 1; i <= 6; i++) button = new JButton("Button " + i); contentpane.add(button); frame.show(); 211

14 Here is a grid with gaps between the buttons: Java and Object Oriented Programming public class GUI6a JFrame frame = new AnotherFrameClass("------I am framed----"); Container contentpane = frame.getcontentpane(); JButton button; frame.setbounds(267,200,367,200); contentpane.setlayout(new GridLayout(2,3,20,20)); for(int i = 1; i <= 6; i++) button = new JButton("Button " + i); contentpane.add(button); frame.show(); 212

15 Panels A panel is an invisible container used for grouping components. A panel may contain sub-panels as well as other components. A panel is an intermediate level container. A panel may have a layout manager. For example, you might place five buttons on panel1 using a flow layout manager, and on panel2 you might arrange four textboxes using a grid layout manager. Now, you can place these two panels or groups of components on the content pane using, say, the BorderLayout manager. The program in the following example places buttons on three different panels and then places the three panels on the content pane. public class GUI7 JFrame frame = new AnotherFrameClass("Using Panels"); JButton button; Container contentpane = frame.getcontentpane(); // constructors for 3 panels JPanel p1= new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(new FlowLayout()); // p1 uses a GridLayout manager p1.setlayout(new GridLayout(3,2)); for (int i = 1; i <= 6; i++) button = new JButton(" "+i); p1.add(button); //p2 uses a BorderLayout manager p2.setlayout(new BorderLayout()); p2.add(new JButton("East"), BorderLayout.EAST); p2.add(new JButton("West"), BorderLayout.WEST); //p3 uses a FlowLayout as set in its constructor for (int i = 1; i <= 4; i++) button = new JButton("Button "+ i); p3.add(button); //contentpane uses a Border Layout manager contentpane.setlayout(new BorderLayout()); contentpane.add(p1, BorderLayout.WEST); contentpane.add(p2, BorderLayout.EAST); contentpane.add(p3, BorderLayout.SOUTH); frame.show(); 213

16 . Here is another form of the same program: public class NewFrame extends JFrame public NewFrame()// default constructor JButton button; Container contentpane = this.getcontentpane();//this is not necessary JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); p1.setlayout(new GridLayout(3,2)); for (int i = 1; i <= 6; i++) button = new JButton(" "+i); p1.add(button); p2.setlayout(new BorderLayout()); p2.add(new JButton("East"), BorderLayout.EAST); p2.add(new JButton("West"), BorderLayout.WEST); p3.setlayout(new FlowLayout()); for (int i = 1; i <= 4; i++) button = new JButton("Button "+ i); p3.add(button); contentpane.setlayout(new BorderLayout()); contentpane.add(p1, BorderLayout.WEST); contentpane.add(p2, BorderLayout.EAST); contentpane.add(p3, BorderLayout.SOUTH); public class GUI7a JFrame frame = new NewFrame(); frame.setbounds(267,200,267,200); frame.settitle("using Panels"); frame.show(); 214

17 Placing Components on a Frame Without a Layout Manager A layout manager is a convenience but not a necessity. You may place components on a frame without a layout manager. To do this you must first set the Layout of your container to null. The following program places buttons onto a frame without using a layout manager. public class AnotherFrameClass extends JFrame public AnotherFrameClass() settitle("my First Frame"); setbounds(250,250,200,200); public AnotherFrameClass(String title) settitle(title); setbounds(0,0,300,300); public class GUI8 JFrame frame = new AnotherFrameClass("NO LAYOUT MANAGERS"); Container contentpane = frame.getcontentpane(); contentpane.setlayout(null); // not even the default one JButton button1 = new JButton("Moe"); button1.setbounds(30,30,70,50); // inherited from Container contentpane.add(button1); JButton button2 = new JButton("Larry"); button2.setbounds(30,200,70,30); contentpane.add(button2); JButton button3 = new JButton("Curley"); button3.setbounds(200,115,70,70); contentpane.add(button3); frame.show(); 215

18 The paint and paintcomponent Methods Almost all containers and components have a paint (or paintcomponent) method. The paintcomponent method is used for JComponents (e.g., JPanel) otherwise the paint method is used. (Note: JFrame is not a JComponent.) The paint(component) method for a particular component draws the component on the screen. The paint(component) method is called automatically whenever the component has to be drawn on the screen. For example, if a component is resized the paint method is called. Do not invoke the paint(component) method directly. The paint(component) method for each component knows how to draw that component. Thus the paint method for a JFrame knows how to draw a frame. Nonetheless, all frames are not equal. Frames come in various shapes, sizes and colors. Each frame (or other component) must supply a bit more information (e.g., a location) to the paint method. This information is passed to the paint method via a parameter a Graphics object: void paint(graphics g) This parameter, g, supplies information about how to draw the particular component, where g is an instance of a Graphics subclass class. The Graphics class. Graphics is an abstract class. Every component that can be drawn on the screen has an associated Graphics object. Thus a button has an associated Graphics object, so does a panel, a frame etc. A program does not directly create a Graphics object with a constructor. Java takes care of that. A component s Graphics object encapsulates the graphics content of a component (i.e., the dimensions of the component, the location, the color, the font etc.) which is all the information necessary for drawing the component. 216

19 When the paint method is (automatically) invoked, the Graphics object of the calling object is passed to paint. For example, if a JFrame frame, must be painted, then frame.paint(g) is (automatically) invoked. The object g is the Graphics object associated with frame -- the graphics context of frame. The object g contains information used by the paint method to draw the JFrame frame on the screen. If it is necessary to repaint a component, the Component class supplies a method, repaint(). The repaint() method calls paint(graphics g). The Graphics class also has a number of methods. Four of the most commonly used methods of the graphics class are: void drawstring(string message, int x, int y) // (x, y) is starting position. void setcolor(color c) void setfont(font f) void drawimage(image img, int x, int y, ImageObserver observer) where an Image object is an image (e.g.,.jpg or.gif file), x and y designate the position of the image, and observer is the object on which the image is drawn, usually this. (In subsequent examples, you will see how to load an image.) Notice the parameters, Color and Font. Color and Font both extend Object: The Color Class Constructor: Color(byte r, byte g, byte b) //where r, g, and b are in the range Example: Color c = new Color(255, 0, 0); //c is a red color object Color c = new Color(100,100,100); The class also has a number of class constants available: red, white, blue, green, yellow, black, cyan, magenta, pink, orange, gray, lightgray, and darkgray. These colors are accessed with the class name, e.g., Color.red. The component class, which is the parent of all components, has methods: setbackground(color c) setforeground(color c). So, any component can invoke these methods as component.setbackground(color.red); or as: or Color c = new Color(255,0,0); component.setbackground(c); component.setbackground(new Color(255,0,0); 217

20 The Font class Constructor: Font(String name, int style, int size) Example: Font font = new Font( Courier, Font.BOLD, 12); style should be a class constant: Font.PLAIN, Font.BOLD, Font.ITALIC, with values 0,1,2. The constants may be joined Font.BOLD+Font.ITALIC (value 3) etc. So for example, to make a font both bold and italic instantiate the font as or less intuitively as Font font = new Font( Courier, Font.BOLD+Font.ITALIC, 12); Font font = new Font( Courier, 3, 12); //since BOLD+ITALIC = 1+2 = 3 Methods: public String getname() public int getsize() public int getstyle() public boolean isplain() public boolean isitalic() public boolean isbold() Drawing on Panels To draw on a panel, extend the JPanel class by overriding the paintcomponent(graphics g) method. Example: In the code below, NewPanel is-a JPanel. However, NewPanel has its own enhanced version of paintcomponent. When NewPanel is drawn, the customized paintcomponent method will automatically be invoked. import java.awt.*; import javax.swing.*; public class NewPanel extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.yellow); Font font= new Font("Sans Serif", Font.BOLD, 16); g.setfont(font); setbackground(color.red); //inherited from Component g.drawstring("alas, poor Yorick, I knew him well", 50,50); 218

21 public class GUI1 public static void main(string args[]) JFrame frame = new JFrame("Painting Panels"); frame.setbounds(0,0,400,200); Container cp = frame.getcontentpane(); NewPanel p = new NewPanel(); cp.add(p); frame.show(); Here the Graphics object associated with this component has a context where the background color is red, the font is bold, San Serif, yellow and 16 point. Notice, the paintcomponent of the super class is called first: super.paintcomponent(g); Example: Displaying an image The following example displays the picture (familiar to baby boomers) stored in the file peterpan1.jpg. You should note that the statement Image picture = new ImageIcon("peterpan1.jpg").getImage(); obtains the image (an object) that is stored in the file peterpan1.jpg. Image is an abstract class, ImageIcon is a subclass of Image, and getimage() is a method of the ImageIcon class. public class PeterPan extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); Image picture = new ImageIcon("peterpan1.jpg").getImage( ); g.drawimage(picture, 0, 0, this); 219

22 public class ShowPicture public static void main(string args[]) JFrame frame = new JFrame("I m Flyin "); frame.setbounds(0,0,300,300); Container cp = frame.getcontentpane(); PeterPan p = new PeterPan(); cp.add(p); frame.show(); Output: 220

23 Drawing Shapes The following methods of the Graphics class can be used to draw shapes. void drawline(int startx, int starty, int endx, int endy) void drawoval(int x, int y, int width, int height) //Draws an ellipse within the boundary of the rectangle specified void drawrect(int x, int y, int width, int height) void fillrect(int x, int y, int width, int height) void draw3drectangle(int x, int y, int w, int h, boolean raised) void drawstring(string message, int x, int y) // (x, y) is the starting position void filloval(int x, int y, int width, int height) void drawarc(int x, int y, int w, int h, int startangle, int spanangle) 221

24 Example: The following example uses the methods of Graphics to draw not-so-happy Yorick. import java.awt.*; import javax.swing.*; public class Face extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.black); Font font= new Font("Comic Sans Serif", Font.BOLD, 16); g.setfont(font); setbackground(color.white); // a method of Component g.drawoval(50, 50, 200, 200); // face g.filloval(100,100, 25,25); //left eye g.filloval(150,100, 25,25); // right eye g.drawline(125,135,100,160); // upper nose g.drawline(100,160, 120,160); // lower nose g.drawarc(75, 175, 100, 40, 350, 200); //mouth g.drawstring("alas, poor Yorick, I knew him well", 20,20); public class GUI7a public static void main(string args[]) JFrame frame = new JFrame("Painting Panels"); frame.setbounds(0,0,300,300); Container cp = frame.getcontentpane(); Face p = new Face(); cp.add(p); frame.show(); 222

25 The Polygon class The data of the Polygon class consists of a list of coordinates (x, y). Each ordered pair defines the vertex of a polygon and each successive pair represents a line segment that is an edge of a polygon. An edge also joins the first and last pairs. Constructors: Polygon() //empty polygon Polygon( int[] xarray, int[] yarray, int numpoints) Methods: drawpolygon(int[] x, int[] y, int num); drawpolygon( Polygon p); fillpolygon(int[] x,int[] y, int num); addpoint(int x, int y); Example: This simple program draws a parallelogram on a panel. import javax.swing.*; import java.awt.*; class PolygonPanel extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.magenta); setbackground(color.white); Polygon parallelogram = new Polygon( ); parallelogram.addpoint(100, 100); parallelogram.addpoint(200, 100); parallelogram.addpoint(225, 20); parallelogram.addpoint(125, 20); g.drawpolygon(parallelogram); 223

26 public class GUI8 JFrame frame = new JFrame("Polygon"); Container cp = frame.getcontentpane(); cp.add(new PolygonPanel()); frame.setbounds(0,0, 300, 200); frame.setvisible(true); Here is another version of the same program: public class Polygon2Panel extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.magenta); int[ ] x = 100, 200, 225, 125; int[ ] y = 100, 100, 20, 20; Polygon parallelogram = new Polygon(x, y, 4); g.drawpolygon(parallelogram); public class GUI9 JFrame frame = new JFrame("Drawing Polygons"); Container cp = frame.getcontentpane(); cp.add(new Polygon2Panel()); frame.setbounds(100,100, 300,300); frame.setvisible(true); 224

27 Example: Recursive drawing Java and Object Oriented Programming Below is a picture of what is called Sierpinski s gasket. To generate Sierpinski s gasket, begin with an equilateral triangle. Now, find the midpoint of each side and form three more triangles as in the diagram, discarding the triangle in the center. 225

28 Repeat the process giving 9 triangles. Continue forever producing 1, 3, 9, 27, 81, 243, 729,... triangles. The Sierpinski s gasket is the set of points that remain if the process is carried out indefinitely. The amazing thing about the Sierpinski s gasket is that Sierpinski s gasket has zero area, despite containing an infinite number of points. The next program draws Sierpinski s gasket on a panel. The program first calls the paintcomponent method that, in turn, makes the first call to the recursive method sier, which does the actual drawing. The recursion stops when the depth of recursion has exceeded

29 import javax.swing.*; import java.awt.*; public class Sierp extends JPanel int x1,y1,x2,y2,x3,y3,depth; // coordinates of a triangle public Sierp(int a, int b, int c, int d, int e, int f, int dep) x1 =a; y1 =b; x2 =c; y2 =d; x3 =e; y3 =f; depth = dep; public void sier(int x1, int y1, int x2, int y2, int x3, int y3, int depth, Graphics g) // set up an draw a triangle Polygon triangle = new Polygon(); triangle.addpoint(x1,y1); triangle.addpoint(x2,y2); triangle.addpoint(x3,y3); g.drawpolygon(triangle); // recursively draw three triangles using one original point and two midpoints if (depth > 0) sier(x1,y1, (x1+x2)/2, (y1+y2)/2, (x1+x3)/2, (y1+y3)/2, depth-1,g); sier((x1+x2)/2, (y1+y2)/2, x2,y2, (x3+x2)/2, (y3+y2)/2, depth-1,g); sier((x1+x3)/2, (y1+y3)/2, (x3+x2)/2, (y3+y2)/2, x3,y3, depth-1,g); public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.black); setbackground(color.white); // starts the recursion sier( x1, y1, x2, y2, x3, y3, depth, g); 227

30 public class GUI9 JFrame frame = new JFrame("Triangle"); Container cp = frame.getcontentpane(); // get a panel and pass the panel coordinates a triangle //as well as the maximum depth of recursion Sierp sp = new Sierp(210,410,10,10,410,10,10); cp.add(sp); frame.setbounds(0,0, 450,450); frame.setvisible(true); The output: 228

31 Example: A somewhat simpler recursive program produced the following megaphone. As in the previous example, the paintcomponent() method of the panel makes the first recursive call. Note that the paintcomponent() method itself is not recursive. Why? import javax.swing.*; import java.awt.*; public class Circles extends JPanel int x,y,w, h; // position (x, y), width and height public Circles(int a, int b, int c) //constructor x = a;//horizontal position y = b;//vertical position w = c;//width h = c; public void draw(int x1, int y1, int w, int h, Graphics g) g.drawoval(x, y, w, h) ; //draw a circle //recursively call draw adjusting location and dimensions if (w > 0) draw(x+10, y+10, w-10, h-10, g); public void paintcomponent(graphics g) super.paintcomponent(g); g.setcolor(color.black); setbackground(color.white); draw( x, y, w, h, g); 229

32 public class GUI10 JFrame frame = new JFrame("Circles"); Container cp = frame.getcontentpane(); //instantiate a panel width and height are both 400 Circles cir = new Circles(10, 10, 400); cp.add(cir); frame.setbounds(0,0, 450, 450); frame.setvisible(true); 230

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

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!

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! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

More information

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

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

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

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build

More information

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers Course Name: Advanced Java Lecture 19 Topics to be covered Java Swing Lists Trees Tables Styled Text Components Progress Indicators Component Organizers AWT to Swing AWT: Abstract Windowing Toolkit import

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

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

Here is a list of a few of the components located in the AWT and Swing packages: Inheritance Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Programming In A Graphical Environment Java is specifically designed

More information

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming Learn Java Programming, Dr.Hashamdar Getting Started with GUI Programming 1 Creating GUI Objects // Create a button with text OK JButton jbtok = new JButton("OK"); // Create a label with text "Enter your

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber Portion of the Java class hierarchy that include basic graphics classes and Java 2D API classes and interfaces. java.lang.object Java.awt.Color Java.awt.Component Java.awt.Container

More information

SIMPLE APPLET PROGRAM

SIMPLE APPLET PROGRAM APPLETS Applets are small applications that are accessed on Internet Server, transported over Internet, automatically installed and run as a part of web- browser Applet Basics : - All applets are subclasses

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS Objec&ves RouleQe Assign 8: Refactoring for Extensibility Programming Paradigms Introduc&on to GUIs in Java Ø Event handling Nov 6, 2015 Sprenkle - CSCI209 1 Nov 6, 2015 Sprenkle - CSCI209 2 Assign 8:

More information

Chapter 12 GUI Basics

Chapter 12 GUI Basics Chapter 12 GUI Basics 1 Creating GUI Objects // Create a button with text OK JButton jbtok = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblname = new JLabel("Enter your

More information

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

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

JFrame In Swing, a JFrame is similar to a window in your operating system

JFrame In Swing, a JFrame is similar to a window in your operating system JFrame In Swing, a JFrame is similar to a window in your operating system All components will appear inside the JFrame window Buttons, text labels, text fields, etc. 5 JFrame Your GUI program must inherit

More information

CS 251 Intermediate Programming GUIs: Components and Layout

CS 251 Intermediate Programming GUIs: Components and Layout CS 251 Intermediate Programming GUIs: Components and Layout Brooke Chenoweth University of New Mexico Fall 2017 import javax. swing.*; Hello GUI public class HelloGUI extends JFrame { public HelloGUI ()

More information

Unit 7: Event driven programming

Unit 7: Event driven programming Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 7: Event driven programming 1 1. Introduction 2.

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

CompSci 230 S Programming Techniques. Basic GUI Components

CompSci 230 S Programming Techniques. Basic GUI Components CompSci 230 S1 2017 Programming Techniques Basic GUI Components Agenda Agenda Basic GUI Programming Concepts Graphical User Interface (GUI) Simple GUI-based Input/Output JFrame, JPanel & JLabel Using Layout

More information

G51PRG: Introduction to Programming Second semester Applets and graphics

G51PRG: Introduction to Programming Second semester Applets and graphics G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting

More information

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

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

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

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

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

CS193k, Stanford Handout #3. Swing 1

CS193k, Stanford Handout #3. Swing 1 CS193k, Stanford Handout #3 Spring, 99-00 Nick Parlante Swing 1 OOP/GUI Libraries Common OOP application GUI's have a lot of standard, repeated behavior, so they are a common and natural domain to apply

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Java. GUI building with the AWT

Java. GUI building with the AWT Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore

More information

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

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

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

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I Electrical and Computer Engineering Object-Oriented Topic : Graphics GUI Part I Maj Joel Young Joel.Young@afit.edu 15-Sep-03 Maj Joel Young A Brief History Lesson AWT Abstract Window Toolkit Implemented

More information

Graphics Applets. By Mr. Dave Clausen

Graphics Applets. By Mr. Dave Clausen Graphics Applets By Mr. Dave Clausen Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to transported

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

Graphics and Painting

Graphics and Painting Graphics and Painting Lecture 17 CGS 3416 Fall 2015 November 30, 2015 paint() methods Lightweight Swing components that extend class JComponent have a method called paintcomponent, with this prototype:

More information

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs What is Widget Layout? Laying Out Components Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and position Main problem: what if

More information

AP CS Unit 12: Drawing and Mouse Events

AP CS Unit 12: Drawing and Mouse Events AP CS Unit 12: Drawing and Mouse Events A JPanel object can be used as a container for other objects. It can also be used as an object that we can draw on. The first example demonstrates how to do that.

More information

Assignment 2. Application Development

Assignment 2. Application Development Application Development Assignment 2 Content Application Development Day 2 Lecture The lecture covers the key language elements of the Java programming language. You are introduced to numerical data and

More information

2110: GUIS: Graphical User Interfaces

2110: GUIS: Graphical User Interfaces 2110: GUIS: Graphical User Interfaces Their mouse had a mean time between failure of a week it would jam up irreparably, or... jam up on the table--... It had a flimsy cord whose wires would break. Steve

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

Class 14: Introduction to the Swing Toolkit

Class 14: Introduction to the Swing Toolkit Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1 Class Preview Over the next 5 lectures, we will introduce

More information

2IS45 Programming

2IS45 Programming Course Website Assignment Goals 2IS45 Programming http://www.win.tue.nl/~wsinswan/programmeren_2is45/ Rectangles Learn to use existing Abstract Data Types based on their contract (class Rectangle in Rectangle.

More information

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible: Basics of Drawing Lines, Shapes, Reading Images To draw some simple graphics, we first need to create a window. The easiest way to do this in the current version of Java is to create a JFrame object which

More information

1.00 Lecture 14. Lecture Preview

1.00 Lecture 14. Lecture Preview 1.00 Lecture 14 Introduction to the Swing Toolkit Lecture Preview Over the next 5 lectures, we will introduce you to the techniques necessary to build graphic user interfaces for your applications. Lecture

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Building Java Programs Bonus Slides

Building Java Programs Bonus Slides Building Java Programs Bonus Slides Graphical User Interfaces Copyright (c) Pearson 2013. All rights reserved. Graphical input and output with JOptionPane JOptionPane An option pane is a simple dialog

More information

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

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

Laying Out Components. What is Widget Layout?

Laying Out Components. What is Widget Layout? Laying Out Components Interior Design for GUIs What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and position

More information

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Graphical User Interface (GUI) and Object- Oriented Design (OOD) Chapter 6,13 Graphical User Interface (GUI) and Object- Oriented Design (OOD) Objectives To distinguish simple GUI components. To describe the Java GUI API hierarchy. To create user interfaces using frames,

More information

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

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

CIS 162 Project 1 Business Card Section 04 (Kurmas)

CIS 162 Project 1 Business Card Section 04 (Kurmas) CIS 162 Project 1 Business Card Section 04 (Kurmas) Due Date at the start of lab on Monday, 17 September (be prepared to demo in lab) Before Starting the Project Read zybook chapter 1 and 3 Know how to

More information

Lecture 18 Java Graphics and GUIs

Lecture 18 Java Graphics and GUIs CSE 331 Software Design and Implementation The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction Lecture 18 Java Graphics and GUIs None

More information

Java Applet Basics. Life cycle of an applet:

Java Applet Basics. Life cycle of an applet: Java Applet Basics Applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. Applet is embedded in a HTML page using the APPLET or OBJECT tag

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! ADVANCED OBJECT-ORIENTATION 2 Object-Oriented Design classes often do not exist in isolation from each

More information

Java Coordinate System

Java Coordinate System Java Graphics Drawing shapes in Java such as lines, rectangles, 3-D rectangles, a bar chart, or a clock utilize the Graphics class Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing

More information

Graphics Applets. By Mr. Dave Clausen

Graphics Applets. By Mr. Dave Clausen Graphics Applets By Mr. Dave Clausen Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to transported

More information

Appendix F: Java Graphics

Appendix F: Java Graphics Appendix F: Java Graphics CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Appendix F: Java Graphics CS 121 1 / 1 Topics Graphics and Images Coordinate

More information

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM Objectives The objectives of this assignment are: to get your first experience with Java to become familiar with Eclipse Java

More information

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. Chapter 13 GUI Basics 1 Motivations The design of the API for Java GUI programming is an excellent example of how the object-oriented principle is applied. In the chapters that follow, you will learn the

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

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

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer,

More information

Programming Mobile Devices J2SE GUI

Programming Mobile Devices J2SE GUI Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame.

More information

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

Graphical User Interfaces. Swing. Jose Jesus García Rueda Graphical User Interfaces. Swing Jose Jesus García Rueda Introduction What are the GUIs? Well known examples Basic concepts Graphical application. Containers. Actions. Events. Graphical elements: Menu

More information

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

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Java Graphics and GUIs 1 The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction

More information

Chapter 12 Advanced GUIs and Graphics

Chapter 12 Advanced GUIs and Graphics Chapter 12 Advanced GUIs and Graphics Chapter Objectives Learn about applets Explore the class Graphics Learn about the classfont Explore the classcolor Java Programming: From Problem Analysis to Program

More information

Appendix F: Java Graphics

Appendix F: Java Graphics Appendix F: Java Graphics CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Appendix F: Java Graphics CS 121 1 / 15 Topics Graphics and Images Coordinate

More information

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components COSC User Interfaces Module 5 Laying Out Components What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and

More information

INTRODUCTION TO (GUIS)

INTRODUCTION TO (GUIS) INTRODUCTION TO GRAPHICAL USER INTERFACES (GUIS) Lecture 10 CS2110 Fall 2009 Announcements 2 A3 will be posted shortly, please start early Prelim 1: Thursday October 14, Uris Hall G01 We do NOT have any

More information

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

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1 Datenbank-Praktikum Universität zu Lübeck Sommersemester 2006 Lecture: Swing Ho Ngoc Duc 1 Learning objectives GUI applications Font, Color, Image Running Applets as applications Swing Components q q Text

More information

Using Graphics. Building Java Programs Supplement 3G

Using Graphics. Building Java Programs Supplement 3G Using Graphics Building Java Programs Supplement 3G Introduction So far, you have learned how to: output to the console break classes/programs into static methods store and use data with variables write

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

CS 2113 Software Engineering

CS 2113 Software Engineering CS 2113 Software Engineering Java 5 - GUIs Import the code to intellij https://github.com/cs2113f18/template-j-5.git Professor Tim Wood - The George Washington University Class Hierarchies Abstract Classes

More information

Building Java Programs

Building Java Programs Building Java Programs Supplement 3G: Graphics 1 drawing 2D graphics Chapter outline DrawingPanel and Graphics objects drawing and filling shapes coordinate system colors drawing with loops drawing with

More information

Programmierpraktikum

Programmierpraktikum 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

More information

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming Chapter 12 GUI Basics 1 Motivations The design of the API for Java GUI programming is an excellent example of how the object-orientedoriented principle is applied. In the chapters that follow, you will

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

BASICS OF GRAPHICAL APPS

BASICS OF GRAPHICAL APPS CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In

More information

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

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing Introduction to Graphical Interface Programming in Java Introduction to AWT and Swing GUI versus Graphics Programming Graphical User Interface (GUI) Graphics Programming Purpose is to display info and

More information

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM Objectives The objectives of this assignment are: to refresh your Java programming to become familiar with

More information

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

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

CHAPTER 2. Java Overview

CHAPTER 2. Java Overview Networks and Internet Programming (0907522) CHAPTER 2 Java Overview Instructor: Dr. Khalid A. Darabkh Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package

More information

Chapter 2 First Java Programs

Chapter 2 First Java Programs First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Why is Java an important programming language? What is the Java virtual machine and byte code? What are

More information

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

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API Agenda Coordinate systems Colors Fonts Drawing shapes Graphics2D API By: dr. Amal Khalifa 1 Programming Seminar @12:30 13:30 pm on Wednesday 9/4/2014 Location : 2.505.01 Painting components 2 Every component

More information

COMPSCI 230. Software Design and Construction. Swing

COMPSCI 230. Software Design and Construction. Swing COMPSCI 230 Software Design and Construction Swing 1 2013-04-17 Recap: SWING DESIGN PRINCIPLES 1. GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them)

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT-V TWO MARKS QUESTION & ANSWER 1. What is the difference between the Font and FontMetrics class? Font class is used to set or retrieve the screen fonts.the Font class maps the characters of the language

More information

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

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

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

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation CSE 331 Software Design and Implementation Lecture 18 Java Graphics and GUIs Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 6 due Thursday 8/2 Homework 7 due Thursday 8/2 Regression testing

More information

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class. Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

UI Software Organization

UI Software Organization UI Software Organization The user interface From previous class: Generally want to think of the UI as only one component of the system Deals with the user Separate from the functional core (AKA, the app

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

IT101. Graphical User Interface

IT101. Graphical User Interface 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

More information

Unit 6: Graphical User Interface

Unit 6: Graphical User Interface Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 6: Graphical User Interface 1 1. Overview of the

More information

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

Chapter 7: A First Look at GUI Applications

Chapter 7: A First Look at GUI Applications Chapter 7: A First Look at GUI Applications Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All

More information

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CISC124, WINTER TERM, 2011 FINAL EXAMINATION 7pm to 10pm, 26 APRIL 2011, Ross Gym Instructor: Alan McLeod If the instructor

More information

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using Java Provided Classes In this lesson we'll focus on using the Graphics class and its capabilities. This will serve two purposes: first

More information

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7 PROGRAMMING DESIGN USING JAVA (ITT 303) Graphical User Interface Unit 7 Learning Objectives At the end of this unit students should be able to: Build graphical user interfaces Create and manipulate buttons,

More information

Image Java Foundation Classes (JFC) java.awt.image JFC. Image. Image. Image PNG GIF JPEG

Image Java Foundation Classes (JFC) java.awt.image JFC. Image. Image. Image PNG GIF JPEG 11 2013 6 25 11.1.............................. 11 1 11.2.............................. 11 2 11.3................................... 11 5 11.4.............................. 11 6 11.5.......................................

More information