130 likes | 154 Views
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
E N D
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
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
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
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 ... } }
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()
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
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); } }
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(); } }
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
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.