Presentation is loading. Please wait.

Presentation is loading. Please wait.

Field Trip #19 Animations with Java By Keith Lynn.

Similar presentations


Presentation on theme: "Field Trip #19 Animations with Java By Keith Lynn."— Presentation transcript:

1 Field Trip #19 Animations with Java By Keith Lynn

2 Overview JButton JPanel LayoutManagers Events The paint method The Graphics object Threads Drawing a stick figure and making it move

3 JButton A JButton is a simple component that allows us to put a push button on a container It is found in the javax.swing package A JButton by default will have margins. If the label won't display, we can obtains the insets and change the left and right margin We can attach a listener to a JButton so that clicks are detected

4 JPanel A simple container that holds other containers It can be used to segment a larger frame into smaller independent pieces We can change the layout of the JPanel and it won't affect other Jpanels We can set the preferred size of a Jpanel with the method setPreferredSize which will accept a Dimension object. e.g. setPreferredSize(new Dimension(50,20));

5 LayoutManagers LayoutManagers define how components are layed out in a Container There are predefined LayoutManagers: FlowLayout (the components are layed out linearly), GridLayout (the components are layed out in a grid), GridBagLayout (allows us to assign weights to components so that they can take up more than one row or column) If we want to layout a Container ourselves, we can remove the LayoutManager by setting it to null

6 Events Events occur when we do something to a component, e.g. clicking a button In order to act on the event, we assign a listener to the component Typical listeners are ActionListener for button clicks and ItemListener for selections ActionListener and ItemListener are interfaces that require us to implement certain methods If a listener is properly registered, then the corresponding method will be called when the event happens

7 The paint method There is a special method inherited by components that allow us to draw on a Container JApplet and JFrame inherit the paint method Subclasses of JComponent, e.g. JPanel, inherit the paintComponent method which should be used instead of paint When a Component first becomes visible or is repainted, then the paint or paintComponent methods will be called if possible

8 The Graphics object The paint and paintComponent methods accept a parameter of type Graphics Graphics is an abstract class. Therefore we cannot create an instance of a Graphics object. We rely on the interpreter to create a Graphics object for us Using the Graphics objects, we can draw lines, rectangles, ovals and polygons

9 Threads When an applet is executed, something called a thread is created A thread is a line of execution We should not place a loop in the applet's thread because it will not be able to respond to events Instead we create a seperate thread

10 Threads con't Thread implements the Runnable interface To create a new Thread, we can  Subclass Thread and override its run method  Create a Runnable class, and then create a Thread using the class We should never call the run method directly In order to start a Thread we call the start method To keep a thread from running forever, we can use a boolean to determine how long it should run

11 Drawing a Stick Figure and Making it Move In order to make the stick figure appear to move, we draw several stick figures We then display each of those figures in order When we repeat this, the figure moves


Download ppt "Field Trip #19 Animations with Java By Keith Lynn."

Similar presentations


Ads by Google