Top Banner
Computer Science A 11: 20/2
25

Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Computer Science A 11: 20/2

Page 2: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Java and swingGraphics programming:

• Windows with menus

• Buttons, textfields, scroll panels etc

• Animations, images,

• Events from mouse clicks, keyboard etc.

Can be quite complicated…

Page 3: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

EmptyFrame.javaimport javax.swing.JFrame;

public class EmptyFrame{ public static void main(String args[]){ JFrame frame=new JFrame("EmptyFrame"); frame.setSize(600,600); frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}

Page 4: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

EmptyFrame

Page 5: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Add a canvas to the windowpublic static void main(String args[]){ JFrame frame=new JFrame(); frame.setSize(600,600); frame.setTitle("DrawShapes"); frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE); JCanvas canvas = new JCanvas(); frame.add(canvas); frame.setVisible(true);

..}

Page 6: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Coordinate system:

Page 7: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

drawLine(135,230,335,370)

Page 8: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

drawOval(135,230,200,140)

Page 9: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

What to draw on a canvas:• Line

• Oval/ellipsis

• Arcs (parts of an ellipsis

• Rectangles

• Curves, quadratic, qubic, bezier

• Images

• Text

Page 10: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

The state when drawing:Current FontCurrent Stroke (width of lines)Current Paint (color of areas)Current Transform (scaling, rotation,

deformation of drawings)Current Composite (new items can be made

partially transparentCurrent clipping area. (limit the area in which

you can draw)

Page 11: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

setFont(new Font("Serif",Font.BOLD,150));drawString("(GyÑ)",50,200);

Page 12: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Paint canvas.setPaint(Color.red); canvas.fillRect(50,50,100,100); canvas.setPaint(Color.blue); canvas.fillOval(250,50,100,100); canvas.setPaint(Color.green); canvas.fillArc(450,50,100,100,45,270);

canvas.setPaint(new GradientPaint( 50,350,Color.red,450,350,Color.blue)); canvas.fillRect(50,300,500,100);

Page 13: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Paint

Page 14: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

More to comeTransformations

Partially transparent

Images

Other components: buttons etc

Events from windows.

Page 15: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Java swing componentsWindows and components

Page 16: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Example

Page 17: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JLabelA bit of text and/or an icon

new JLabel(”Label 1”,new IconImage(..))

Page 18: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JButtonA button you can press.

Text and/or an image (icon)

Page 19: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

Radiobutton and checkboxPress to select or de-select

Page 20: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JTextFieldTextField: a line where you can enter text

using the keyboard.

Page 21: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JTextAreaTextArea: a multi-line area for entering text

Page 22: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JSliderA slider: select a value

By dragging a knob

Page 23: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JSpinnerSelect a value

by stepping through

a bounded range

of values

Page 24: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JComboBoxSelect a value from

a drop-down list

Page 25: Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.

JListSelect an a value

or an interval

of values from

a list