{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

100_lecture17 - Introduction to Computation and Problem...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 17: Class 17: Lab Lab : The Graphics 2D API : The Graphics 2D API 2 The Origins of the Java Graphics 2D API The original Java GUI toolkit, the AWT, was a quick and dirty solution. It used native peer components to draw all widgets. Swing draws all components except the top level containers using Java methods rather than relying on platform specific widgets. To do this, Swing required improved graphics. The Java 2D API was born as enabling technology for Swing. There is now a Java 3D API also. See tutorial at http://java.sun.com/docs/books/tutorial/2d/index.html 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3 Java Graphics Architecture Platform Graphics Architecture Platform Windowing System Java AWT Java 2D Graphics API Swing 4 NgonApp i JLabel Custom Draw ng Swing Components: and JTextField 2
Background image of page 2
5 So, How Do I Draw in Java the main thread. thread. You might think that if you want to do your own drawing in Java, you do it in the main thread just as you create a GUI out of components in the main thread. In fact, you have to create components on which you draw like all your other GUI components in But the drawing has to be done in the event And to understand why, we need to start by considering when drawing happens. 6 When Does a GUI Get Drawn? program gets started) refresh ( repaint( ) ). tick() } On initial display (but not necessarily when the When the display gets “damaged”. For example when it gets hidden and then re-exposed. When the content changes, and Swing or the code written by the programmer calls for a Remember the method from the previous lab? public void tick() { minutes++; // increment # of minutes repaint(); // refresh clock display 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
7 occur in rapid succession. Swing calls the following three methods in order (on the event thread): paintComponent() paintBorder() paintChildren() children. How does a GUI Get Drawn? Swing schedules the drawing. It may combine multiple redraw requests that The last recursively paints a container's 8 How to Do Custom Drawing JLabel and JComboBox use paintComponent() themselves. container class, usually JPanel , and override paintComponent() . paintComponent() JPanel Override getPreferredSize() or call setPreferredSize() JPanel Standard Swing components like to draw If you want to do custom drawing, extend a Use calls from the 2D API in to draw what you want on the background. to size to your drawing. 4
Background image of page 4
9 The Graphics Class paintComponent() Graphics g , that serves as a drawing toolkit In more recent versions of the JDK, the argument Graphics2D Graphics for the 2D API . So cast it. Graphics public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g; //drawing commands go here } is called with argument initialized to the component's defaults.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}