JFrame JComponent JFrame JComponent JFrame JComponent.

Post on 17-Dec-2015

240 views 2 download

Tags:

Transcript of JFrame JComponent JFrame JComponent JFrame JComponent.

Lecture 19COP3502: Introduction to CIS I

java swing

java swingimport javax.swing.*;

JFrame

JFrameJComponent

JFrameJComponent

JFrameJComponent

JComponents

JPanelJLabelJSlider

JScrollBarJPopupMenu

JMenuBar…. many more!

JComponents are abstract

Add functionality by creating subclass of a JComponent

public class MySuperAwesomePanel extends JPanel {

// Panel functionality}

nested classes

classes declared within the body of another class

“private static class”

good for managing many small classes that make up a larger class

window organization

graphicsimport java.awt.Graphics;

graphicsimport java.awt.Graphics;

1. contains all the information related to drawing graphics

eg. Background and foreground colors, location, dimensions

graphicsimport java.awt.Graphics;

1. contains all the information related to drawing graphics

eg. Background and foreground colors, location, dimensions

2. contains methods for actually drawing graphics to screen

eg. Shapes, text, and images

graphicsimport java.awt.Graphics;

Graphics is an abstract classYou cannot instantiate a Graphics object on your

own

typically created by the JComponent and given to the component’s paint() or paintComponent() method

paintComponent(Graphics g)

inherited from a JComponent

does nothing on its own

YOU must override and provide implementation

repaint()

You cannot call paintComponent(Graphics g) on your own

Only the system can!

repaint() is inherited from Jcomponent

calling it creates a Graphics object and calls paintComponent()

repaint()

Shape Classimport java.awt.geom.*;

eventsimport java.awt.event.*

stimulus-response model

source listener

responder(s)

event

four parts of process Java mechanisms

stimulus event (button press, timer tick, etc.)

communication (event record of what happened)

ActionEvent class (contains information about nature of stimulus)

receiving mechanism (how are the stimulus and responding objects connected?)

ActionListener interface (classes implementing can listen for a specific kind of event)

response (listening object’s reaction to stimulus)

actionPerformed (method called in response to stimulus)

moving our alien

- need a Timer to generate actions

- need a class listening and responding to actions

implement ActionListener by defining actionPerformed()

Plan:1. Create a Timer() object within our drawing panel

- takes two args: delay time and action2. Create an “inner class” MoveAlien that implements

ActionListener3. Define actionPerformed() to tell our panel what to do4. Our drawing panel decides what should move and

repaints itself

moving our alien with a mouse

implement MouseListener interface

define mouseClicked() to instead tell our panel what to do

moving our alien with a keyboard

request focus on component

implement KeyListener interface

define keyPressed() to instead tell our panel what to do