spring 2009 programming fundamentals i java programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Spring 2009 Programming Fundamentals I Java Programming PowerPoint Presentation
Download Presentation
Spring 2009 Programming Fundamentals I Java Programming

Loading in 2 Seconds...

play fullscreen
1 / 15

Spring 2009 Programming Fundamentals I Java Programming - PowerPoint PPT Presentation


  • 144 Views
  • Uploaded on

Spring 2009 Programming Fundamentals I Java Programming. XuanTung Hoang tung_hx@icu.ac.kr. Lecture No. 18. Java GUI. Introduction Java Graphics. GUI in a nutshell. Other topics: Drawing Capabilities. Drawing on a panel. Sometimes it is necessary to paint on a GUI component

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Spring 2009 Programming Fundamentals I Java Programming' - vida


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
spring 2009 programming fundamentals i java programming

Spring 2009 Programming Fundamentals IJava Programming

XuanTung Hoang

tung_hx@icu.ac.kr

Lecture No. 18

java gui

Java GUI

Introduction

Java Graphics

gui in a nutshell

GUI in a nutshell

Other topics: Drawing Capabilities

drawing on a panel
Drawing on a panel
  • Sometimes it is necessary to paint on a GUI component
    • For example: Paint on a JPanel, paint on a JButton, …
  • How to paint on a GUI component?
    • Create a subclass of the component (inheritance)
    • Override paintComponent(Graphics g)method
      • The argument g is the object that provide drawing capabilities
    • Use the object g to paint
      • Frequently used methods: getClipBounds(), setColor(), getColor(), clearRect(), drawRect(), fillRect(), drawLine(), drawOval()
example drawing on a panel
Example: Drawing on a Panel
  • Create our own panel class by extending JPanel
    • Override paintComponent() method and put our drawing code there
  • Create our own frame and use our panel class as a Swing component
slide7
Graphics class:
    • Class that represents “graphic contexts”
      • The area that paintComponent() will draw on
      • Control font styles/line patterns/fill patterns/colors
      • Provide drawing support methods
slide8

Rectangle class

  • Getting drawing area from Graphics object

Rectangle r specifies drawing area of Graphics object g in current drawing context coordinate system

slide10
Put our drawing panel into a frame and show the frame
    • NOTE: setLayout before adding drawing panel
example make a snake that runs
Example: Make a snake that runs
  • We will make a snake that runs across a panel
  • Solution:
    • A snake = a horizontal line = a point (x, y) (the head) , a length, and a direction
    • We use a timer, after each timer event we change position of the head and repaint the snake on a panel

(0, 0)

dir

(x, y)

LEFT = 0

RIGHT = 1

length

DOWN = 2

UP= 3

(MAXX, MAXY)

render and image on a panel
Render and image on a panel
  • Import java.awt.image.*
  • Import javax.imageio.*
  • Create a BufferedImage object using static method read of ImageIO class
    • BufferedImage img = ImageIO.read(new FileInputStream(“Image.jpg” ));
  • Draw the image with drawImage() method of class Graphics
    • g.drawImage(img, x, y, width, height, null);