1 / 20

Graphics

Graphics. Coordinates The upper-left coordinate of a GUI component is (0,0) The upper left coordinate (0,0) is actually behind the title bar of the window Graphics is an abstract class that provides adevice -independent graphics interface java.awt.Graphics. Graphics.

kasi
Download Presentation

Graphics

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphics • Coordinates • The upper-left coordinate of a GUI component is (0,0) • The upper left coordinate (0,0) is actually behind the title bar of the window • Graphics is an abstract class that provides adevice-independent graphics interface • java.awt.Graphics

  2. Graphics Drawing Lines, Rectangles, Ovals • drawLine(intx1, int y1, int x2, int y2); • drawRect(int x, int y, int width, int height); • drawOval(int x, int y, int width, int height); • g.drawOval(100,100, 40,70); Filling Shapes • fillRect(int x, int y, int width, int height); • g.fillRect(100,100, 40,70); Drawing Strings: • drawString(String s , int x ,int y); • g.drawString(“Hello”,20,100);

  3. Drawing Polygons and Polylines • Polygons are multisided shapes. • Polylines are a series of connected points. Some methods require a Polygon object • drawPolygon (intxPoints[], intyPoints[], intNumPoints[]); • drawPolyline(intxPoints[], intyPoints[], intNumPoints); • Constructor: • Polygon(intxPoints[], intyPoints[], intNumPoints • drawPolygon (Polygon p) int [] xvalues = {100, 200,150}; int [] yvalues = {50, 50,75}; Polygon p = new Polygon (xvalues,yvalues,3); g.drawPolygon(p);

  4. Color • Color is a class with different private instance variables for RGB value (red, green and blue levels) • Each value range from dark (0) to light (255) • Black is (0,0,0) • White is (255,255,255) • Red is (255,0,0) • Color constants (uppercase accepted) • Color.orange, Color.red, Color.darkGray, Color.YELLOW • You can create a new color using the constructor and specifying the RGB values • Color c = new Color(255,0,0) • void setColor (Color c) is used to set the color of an object • g.setColor(Color.yellow); • Color getColor () – returns the color of an object. • g.getColor()

  5. Font To change the font you must create a new Font object. Fontf = new Font(String fontName, intfontStyle, intfontSize) • fontName • Monospaced//Courier • SanSerif //Times New Roman • Serif or //Helvetica • fontSyle (static Constants) • Font.PLAIN • Font.ITALIC • Font.BOLD • Font.ITALIC+ Font.BOLD • fontSize (12, 18, 20) • Use void setFont (Font f) to set the font to a new style • button.setFont(f); • g.setFont(new Font(“SanSerif”,Font.ITALIC+Font.BOLD,12));

  6. Font • If your system supports other fonts such as “Times New Roman” you can use it to create a Font Object. • g.setFont(new Font(“Times New Roman”,Font.ITALIC+Font.BOLD,12)); • To find available fonts • Create an instance of GraphicsEnvironment and use its method getAvailableFontFamilyNames() method to find available fonts // import java.awt.GraphicsEnvironment GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = e.getAvailableFontFamilyNames(); for (String f: fontNames) System.out.println(f);

  7. Drawing on JFrame • What is the paint method? • The method responsible for displaying a component on the screen • public void paint (Graphics g) • Why call the super.paint(g) first? • This ensures that the viewing area is clear before a new drawing is displayed • When is the paint method called? • Documentation: “ Called when JFrame is first displayed and any time the JFrame needs to be redisplayed as when it is obscurred by another window” • Force the Frame to be redisplayed • Call repaint() request the JFrame to be redisplayed.

  8. Drawing on a JFrame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Drawing2 extends JFrame { public Drawing2() { setSize(100,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { super.paint(g); System.out.println("In JFrame's Paint"); g.setColor(Color.red); g.drawOval(50,50,50,50); } public static void main (String [] args) { new Drawing2(); } } • The JFrame’s paint method should automatically be called when the window is obscure. • What happens when the … • windows overlap? • window is minimized then maximized? • window is resized?

  9. Drawing on a Frame include a JPanel public class Drawing3 extends JFrame { JPanelpanel = new JPanel(); public Drawing3() { add(panel); panel.setBackground(Color.BLUE); setSize(100,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { super.paint(g); System.out.println("In JFrame's Paint"); g.setColor(Color.red); g.drawOval(50,50,50,50); } public static void main (String [] args) { new Drawing3(); } } • Add a panel to the center of the JFrame • Make the panel background BLUE • The panel covers the JFrame

  10. Drawing on a Frame include a JPanel public class Drawing3 extends JFrame { JPanelpanel = new JPanel(); public Drawing3() { add(panel,BorderLayout.WEST); panel.setBackground(Color.BLUE); setSize(100,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { super.paint(g); System.out.println("In JFrame's Paint"); g.setColor(Color.red); g.drawOval(50,50,50,50); } public static void main (String [] args) { new Drawing3(); } } • Add a panel to the WEST of the JFrame • Make the panel background BLUE Blue panel Activity: Change panel width: panel.setPreferredSize(new Dimension(50,200));

  11. Drawing on Panels • Instead of drawing on the JFrame, you can confine your drawing space to a JPanel. • Instead of overriding JPanel’s paint(), you should override paintComponent() • public void paintComponent(Graphics g) • Override the paintComponent method for most Swing objects (except JFrame and JApplet) • Call super.paintComponent(g) first. • To update display, call JPanel’s repaint()

  12. Drawing on Panel public class Drawing5 extends JFrame{ MyPanelpanel = new MyPanel(); public Drawing5() { add(panel); setSize(300,300); //set size of entire Frame setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class MyPanel extends JPanel {public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("In MyPanel's Paint “); g.setColor(Color.green); g.fillOval(50,50,30,30); } } public static void main (String [] args) { new Drawing5();} } • Create a class for your new panel that extendsJPanel • Override paintComponent • Create an instance of the new subclass • What happens if the … • windows overlap? • window is minimized then maximized? • window is resized?

  13. Activity • Draw a car on the screen in a panel • Declare instance fields x and y and initialize them to (50,100) • Make all coordinates relative to x and y. (x,y) 10 10 (x+50,y+50)

  14. Event Handling Part 2 HCI

  15. KeyEvent • Key events enable the use of the keys to control and perform actions or get input from the keyboard • KeyEvent has predefined static constants VK_A to VK_Z The letter keys from A to Z VK_0 to VK_9 The number keys from 0 to 9 VK_UP, VK_DOWN Up and down arrow keys VK_LEFT,VK_RIGHT Left and right arrow keys VK_CONTROL The control key

  16. KeyEvent • Key events enable the use of the keys to control and perform actions or get input from the keyboard • KeyListener • public void keyPressed(KeyEvent e) • public void keyReleased(KeyEvent e) • public void keyTyped (KeyEvent e) • Will not be invoked for keys that do not have a Unicode( function keys, control keys, action keys) • KeyEvent methods • getKeyChar() -   Returns the character associated event • getKeyCode() -   Returns the keycodeassociatedevent

  17. Activity • Use the left and right arrow keys to move the car • Hint: In the Key Listener when the user presses a Right Arrow, update the y by 10 • y+=10; • Repaint the panel

  18. Timer Objects • Regularly generates action events at programmer-specified time intervals • Timer class is in javax.swing package • Timer (int delay, ActionListener listener) • delay is measured in milliseconds • A delay of 1000 is 1 second

  19. Timer class methods • Timer ( int delay, ActionListener listener) • void addActionListener( ActionListener listener) • intgetDelay() • booleanisRunning(); • void setDelay(int delay) • void start(); • void stop(); Timer timer; timer = new Timer (30, new MyTimerListener()); Timer.start(); class MyTimerListenerimplements ActionListener { public static void actionPeformed(ActionEvent e) { System.out.println(“timer triggered”); } }

  20. Activity • Create an application that displays a circle at random positions • Every 3 seconds, randomly generate a new position and display circle.

More Related