240 likes | 306 Views
CS 240 Week 9. EventQueue.invokeLater. In Swing, all user interface operations must occur on the “UI thread” All components should be created on the UI thread All method calls on UI components should happen on the UI thread EventQueue.invokeLater runs the specified code on the UI thread
E N D
EventQueue.invokeLater • In Swing, all user interface operations must occur on the “UI thread” • All components should be created on the UI thread • All method calls on UI components should happen on the UI thread • EventQueue.invokeLater runs the specified code on the UI thread • The main method for Swing programs should call EventQueue.invokeLater to create the UI • The main thread exits immediately after calling EventQueue.invokeLater, but the UI thread keeps the program running • EXAMPLE: EmptyFrame
JFrame • Use JFrame class to create top-level windows • setTitle method sets the window’s title • setDefaultCloseOperation method specifies what should happen when the user clicks the window’s close icon • setLocation method sets the window’s location on the desktop • setVisible method shows or hides the window • setSize method sets the window’s size • EXAMPLE: EmptyFrame
JFrame • add method adds a new subcomponent to the window • pack method sets the window’s size according to the preferred size and layout of the window’s subcomponents • EXAMPLE: SimpleFrame
JComponent • User interface components are subclasses of JComponent that provide custom drawing and event handling functionality • getSize and setSize methods get and set component’s width and height • getWidth and getHeight methods return component’s width and height individually • setBackground method is used to set the component’s background color • setPreferredSize, setMinimumSize, setMaximumSize methods are used to express the components preferred, min, and max sizes • paintComponent method draws the contents of the component • Graphics2D class is used to perform drawing operations in a component • EXAMPLE: Drawing
Drawing • Color • Red, Green, Blue, Alpha (transparency) components • new Color(210, 180, 140, 192) • Graphics.setColor method sets the current drawing color • Drawing origin is the component’s top-left corner. X values increase as you move right. Y values increase as you move down. • Representing points • Point2D superclass • Point2D.Float and Point2D.Double subclasses (nested inside Point2D) • Point2D.Double pt = new Point2D.Double(x, y)
Drawing Rectangles • Rectangles • Graphics.drawRect(x, y, width, height) • Graphics.fillRect(x, y, width, height) • OR • Rectangle2D superclass • Rectangle2D.Float and Rectangle2D.Double subclasses (nested inside Rectangle2D) • Rectangle2D rect = new Rectangle2D.Double(x, y, w, h) • Graphics2D.draw(rect) • Graphics2D.fill(rect) • EXAMPLE: Drawing
Drawing Ellipses • Ellipses • Graphics.drawOval(x, y, width, height) • Graphics.fillOval(x, y, width, height) • OR • Ellipse2D superclass • Ellipse2D.Float and Ellipse2D.Double subclasses (nested inside Ellipse2D) • Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h) • Graphics2D.draw(ellipse) • Graphics2D.fill(ellipse)
Drawing Lines • Lines • Graphics.drawLine(x, y, width, height) • OR • Line2D superclass • Line2D.Float and Line2D.Double subclasses (nested inside Line2D) • Line2D line = new Line2D.Double(x1, y1, x2, y2) • Graphics2D.setStroke method sets the line thickness and style • g2d.setStroke(new BasicStroke(5)); // line 5 pixels wide • Graphics2D.draw(line) • EXAMPLE: Drawing
Drawing Text • Font class represents fonts • Font font = new Font(name, style, size) • Font font = new Font(“SansSerif”, Font.PLAIN, 72); • Graphics.setFont method sets the current font • g2d.setFont(font); • Graphics2D.drawString(string, x, y) • (x, y) is location of text’s baseline • g2d.drawString(“Hi There”, 100, 200); • EXAMPLE: Drawing
Drawing Text • Calculating text metrics (width, height, etc.) • FontRenderContext context = g2d.getFontRenderContext(); • Rectangle2D bounds = font.getStringBounds(message, context); • double stringWidth = bounds.getWidth(); • doublestringHeight = bounds.getHeight(); • double ascent = -bounds.getY(); • If you need descent and leading • LineMetrics metrics = font.getLineMetrics(message, context); • float descent = metrics.getDescent(); • float leading = metrics.getLeading();
Drawing Images • The BufferedImage class can be used to store and manipulate images in memory. You can: • Load an existing image into a BufferedImage • Modify an image by changing the pixel values in a BufferedImage • Create a new image by creating an empty BufferedImage and modifying its pixel values • Save a BufferedImage to a file
Drawing Images • The ImageIO class can be used to load images from disk or the web, and to save images to disk • Load image from disk: • String filename = "..."; • Imageimage = ImageIO.read(new File(filename)); • EXAMPLE: Drawing • Load image from URL: • String urlname = "..."; • Imageimage = ImageIO.read(new URL(urlname));
Drawing Images • Graphics.drawImage(image, destX1, destY1, destX2, destY2, srcX1, srcY1, srcX2, srcY2) • “dest” is the destination rectangle where the image should be drawn in the component • “src” is the source rectangle in the image to be drawn • Can be only part of the image • “dest” and “src” do not have to be the same size. The drawImage method will scale the source image to fit in the destination rectangle. • This is one way to scale an image • EXAMPLE: Drawing
Event Handling • User actions are called “events” • Low-level window events • Window changes • Low-level component events • Mouse events, Keyboard events, Component changes • High-level component-specific events • Button events, Menu events, List events, etc. • Events are handled by attaching “event listeners” to windows and components • Swing defines a “listener interface” for each category of events • Examples: WindowListener, MouseListener, MenuListener
Event Handling • A “listener” is an object that implements a listener interface • Listeners are attached to windows and components by calling the appropriate “addXXXListener” methods • addWindowListener, addMouseListener, addMenuListener, etc. • The window or component will notify all attached listeners about all relevant events • windowActivated, mouseClicked, menuSelected, etc. • Listener methods accept an EventObject parameter that describes the event that occurred • WindowEvent, MouseEvent, MenuEvent, etc. • The EventObject.getSourcemethod returns the window or component that generated the event
Event Handling • Listeners are typically implemented as inner classes • Named inner class • class MyMouseHandler implements MouseListener { … } • this.addMouseListener(new MyMouseHandler()); • Anonymous inner class • this.addMouseListener(new MouseListener() { … });
Event Handling • Event Adapter classes • Frequently, a program only wants to handle a few of the events defined by a listener interface • However, implementing the listener interface requires implementations for all event methods • Swing provides Adapter superclasses that provide empty implementations of all methods in a listener interface • WindowAdapter, MouseAdapter, KeyAdapter, etc. • This allows a program to create a listener by subclassing an adapter class, and overriding only the event methods it cares about • Some adapter classes implement multiple listener interfaces • EXAMPLE: MouseAdapter implements MouseListener, MouseMotionListener, and MouseWheelListener
Mouse Events • MouseListener interface • MouseMotionListener interface • MouseWheelListener interface • MouseAdapter class • EXAMPLE: Dragging and scrolling shapes in Drawing
Component Events • ComponentListener interface • ComponentAdapter class • EXAMPLE: Updating width and height in text shapes in Drawing
Window Events • WindowListener interface • WindowStateListener interface • WindowAdapter class • EXAMPLE: Frame window in Drawing
Keyboard Events • Keyboard Focus • When the user types a key, which component gets the key event? • The component with the “keyboard focus” • Only one window has the keyboard focus at a time • Within that window, only one component has the keyboard focus at a time • A component can request the keyboard focus by calling Component.requestFocus or Component.requestFocusInWindow • EXAMPLE: A text field requests the keyboard focus when the user clicks on it so it will receive key events
Keyboard Events • When a window receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events • WindowFocusListener interface • When a component receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events • FocusListener interface • EXAMPLE: In Drawing, when the frame window receives keyboard focus, DrawingFrame calls requestFocusInWindow on the DrawingComponent so it will receive keyboard events
Keyboard Events • KeyListener interface • KeyAdapter class • EXAMPLE: Moving shapes with arrow keys in Drawing