Docsity

GUIs Part Two - Introduduction to Jave Programming - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download GUIs Part Two - Introduduction to Jave Programming - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

GUIs part 2

Docsity.com

Announcements

  • Lab Monday will be non-graded GUI lab (You may come and work on previous lab)
  • Homework due Monday Docsity.com

ImageIcon class

Previously we used JLabels to hold text We can also use JLabel to hold an image:

  1. Make an ImageIcon object
  2. Make a JLabel object
  3. Link them together (See: DrawImages.java) Docsity.com

Extending JFrame

To do some more advanced drawing, we will have to extend JFrame (to override methods): Then we need to override the paint() method: DrawCircle is just an example name Docsity.com

When to repaint?

Java normally updates your GUI automatically Sometimes you need to update manually: repaint - redraw the whole window pack - resize the window revalidate - layout is redone (in case you removed any objects) If your window seems to not be correct (or not updating) try running one/all of theseDocsity.com

Graphics + Mouse

We have seen how to detect when you click the mouse, so let's use that to draw! To do this we need to make a MouseListener:

  • When press the mouse down, mark this as one corner of the rectangle
  • When we release the mouse, mark this as the other end of the rectangle (mini-paint!) (See: MouseDraw.java) Docsity.com

JColorChooser

The JColorChooser is a pre-built class that allows you to choose a color (from a GUI) To run the GUI, use showDialog() (returns Color): (See: RainbowMouseDraw.java) associated window title of color GUI initial color selected Docsity.com

drawString()

You can also use the Graphics class to draw a more customizable String than JLabel To do need to simply call drawString(): Can also change Font with: (See: DrawStringExample.java) starting x pixel starting y pixel point size Docsity.com