1 / 12

Java Graphics

Java Graphics. Swing Graphics. Empty Swing containers have no visual appearance except for a background color Every JComponent must have a paintComponent method that is called when the component is first made visible or needs to be redrawn for some reason

mullinsk
Download Presentation

Java 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. Java Graphics

  2. Swing Graphics • Empty Swing containers have no visual appearance except for a background color • Every JComponent must have a paintComponent method that is called when the component is first made visible or needs to be redrawn for some reason • The JPanel component is a lightweight container, making it suitable as a drawing area • A common way to do graphics is to extend the JPanel class and override the paintComponent method

  3. The paintComponent Method • Called by the JVM when this component needs to be redrawn • A single argument, the component's graphics context (class: Graphics), is passed when paintComponent is called • A Graphics object contains: • the component on which it draws • the current color and font • location origin • clipping information • and more

  4. Graphics vs. Graphics2D • The Graphics class has limitations: • Cannot use real number coordinates • Cannot draw dotted, dashed, or variable-width lines • Cannot easily draw complex curves or fill complex shapes • Cannot use textures or gradient colors to fill shapes • The newer Graphics2D class extends Graphics and provides these capabilities • All GUI components use a Graphics2D object but paintComponent passes a Graphics object for backward compatibility

  5. General Approach public class MyPanel extends JPanel { // instance variables public MyPanel() { // public constructor } // public methods // private helper methods public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; // drawing messages sent to g2d ... } }

  6. The paintComponent Method • super.paintComponent(g) is called first to ensure that painting responsibilities defined in JPanel are carried out • You should not call paintComponent directly; it is called by the JVM when it needs to • You can indirectly call paintComponent on a component by using component.repaint()

  7. Some Basic Graphics Methods • void setColor(Color color) • void setFont(Font font) • void drawString(String text, int x, int y) • (x,y)is the coordinate of the lower left corner of the drawn string's leftmost character

  8. Graphics Example import javax.swing.*; import java.awt.*; public class GraphicsPanel extends JPanel { public GraphicsPanel() { setPreferredSize(new Dimension(200,200)); setBackground(Color.magenta); // panel color } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; g2D.setColor(Color.blue); // drawing color g2D.setFont(new Font("Helvetica", Font.BOLD, 24)); g2D.drawString("Hello World", 25, 25); } }

  9. Graphics Example (cont'd) import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame { public MainFrame() { setSize(new Dimension(500,300)); setLocation(100,100); addWindowListener(new WindowAdapter () { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); getContentPane().setLayout( new FlowLayout(FlowLayout.CENTER)); GraphicsPanel gp = new GraphicsPanel(); getContentPane().add(gp); setVisible(true); } public static void main(String[] args) { new MainFrame(); } }

  10. Display

  11. Notes On The Example • GraphicsPanel extends JPanel so that the paintComponent method can be overridden • If you forget to call super's paintComponent method, you can get pixels from another desktop frame as background garbage • The background color is associated with the panel; the paint color with the Graphics2D object • The MainFrame class extends JFrame and an instance of it is created in the main method

  12. Drawing Shapes • You can draw any object that implements the java.awt.Shape interface. • Example: suppose g2D is a Graphics2D object: Shape s = ...; g2D.draw(s); The Java library supplies a number of classes that implement the Shape interface type.

More Related