programming of interactive systems
play

Programming of Interactive Systems Week 4: Graphics & Images - PowerPoint PPT Presentation

Programming of Interactive Systems Week 4: Graphics & Images Anastasia.Bezerianos@lri.fr (Nolwenn Maudet) Anastasia.Bezerianos@lri.fr 1 2 Java 2D API Designing components A window (panel) is a canvas on which applications draw:


  1. Programming of Interactive Systems Week 4: Graphics & Images Anastasia.Bezerianos@lri.fr (Nolwenn Maudet) Anastasia.Bezerianos@lri.fr 1 2 Java 2D API Designing components A window (panel) is a canvas on which applications draw: Provides 2D graphics, text & image API components (e.g., buttons), done by Java capabilities The rest (that is up to you!) Wide range of geometric primitives Mechanisms for hit detection of shapes, text, images Color & transparency Transformations Printing JButton Control of the quality of rendering 3 4

  2. coordinate system Pixel = picture element Almost cartesian : (0,0) top left (0,0) (width,0) (0,height) (width, height) 5 6 windows and subwindows windows and subwindows Each component has its own design space: its subwindow Each component has its own design space: its subwindow Subwindow = the rectangular space inside the parent of Subwindow = the rectangular space inside the parent the component, where the component is designed. of the component, where the component is designed. It has its own coordinate system It has its own coordinate system Clipping, rules: a component can not draw outside its subwindow, nor on one of its own (0,0) (width,0) components (0,0) JPanel JButton (0,0) JButton (w b , h b ) (width, height) (0,height) (w p , h p ) 7 8

  3. my own graphical component drawing • inherits from Component, JComponent ou JPanel import java.awt.Graphics import java.awt.Graphics2D // Java2 • redefines the function paint void paint (Graphics G_Arg) { 1. get a hold of the “graphics context” of your Graphics2D g2 = (Graphics2D) G_Arg; component } • inherits the function repaint(), that in turn calls Graphics g = myJPanel.getGraphics( ); Graphics2D g2 = (Graphics2D) g; paint(…) We call repaint() if we want to update our component 2. draw different shapes Asynchronous, Java deals with paint in Graphics g2.drawLine(x1,y1, x2,y2); • inherits methods (that we need to keep track of) setSize(), setEnable(), setBackground(), setFont(), setForeground()... 9 10 new component, an example new component, an example public class MyPanel extends JPanel { public class MyPanel extends JPanel { public void paintComponent(Graphics g) { public void paintComponent (Graphics g){ super.paintComponent(g); // erases background Graphics2D g2 = (Graphics2D) g; // cast for java2 super.paintComponents(g); // erases background // my graphics: Graphics2D g2 = (Graphics2D)g; //cast for java2 g2.setColor(new Color(255, 0, 0)); g2.fillRect(10, 10, 200, 50); // my graphics: g2.setColor(new Color(0, 0, 0)); Hello World g2.drawString("Hello World", 20, 20); g2.setColor(new Color(255,0,0)); } g2.fillRect(10,10,200,50); // left, top, width, height g2.setColor(new Color(0,0,0)); public static void main(String[] args) { g2.drawString("Hello World", 20, 20);// S, left, BOTTOM JFrame frame = new JFrame(“my panel”); } JPanel jp = new MyPanel(); } frame.getContentPane().add(jp); Hello World frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setSize(250, 200); frame.setVisible(true); } 11 12 }

  4. my own graphical component void paint (Graphics G_Arg) { • inherits from Component, JComponent ou JPanel An instance of Graphics is provided by Java for components to draw on it • redefines the function paint void paint (Graphics G_Arg) { The Graphics object has a state: Graphics2D g2 = (Graphics2D) G_Arg; Translation from the origin for rendering: translate() } 0,0 = top left by default • inherits the function repaint(), that in turn calls Effect Zone (!= rectangle) = Clip paint(…) by default : entire component, but we can constraint it We call repaint() if we want to update our component Color of design Asynchronous, Java deals with paint in Graphics Color col1 = new Color (255, 0, 0); RGB but can have HSV • inherits methods (that we need to keep track of) Character font setSize(), setEnable(), setBackground(), setFont(), Font font1 = new Font("SansSerif", Font.BOLD, 12); setForeground()... 13 14 import javax.swing.*; import javax.swing.*; import java.awt.*; import java.awt.*; public class SwingDemo7 extends JFrame { public class SwingDemo7 extends JFrame { public JPanel panel; public JPanel panel; public void init() { public void init() { Container cp = getContentPane(); Container cp = getContentPane(); this.setTitle("example 7"); this.setTitle("example 7"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setDefaultCloseOperation(EXIT_ON_CLOSE); panel = new JPanel(); panel = new JPanel(); cp.add(panel); cp.add(panel); } } public static void main(String[] args) public static void main(String[] args) { { SwingDemo7 frame = new SwingDemo7(); SwingDemo7 frame = new SwingDemo7(); frame.init(); frame.init(); What is going on here? frame.setSize(250,250); frame.setSize(250,250); frame.setVisible(true); frame.setVisible(true); Graphics g = frame.panel.getGraphics(); Graphics g = frame.panel.getGraphics(); Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g; g2.setColor( Color.RED ); g2.setColor( Color.RED ); for (int i = 0; i <100 ;++i) { for (int i = 0; i <100 ;++i) { g2.drawLine( g2.drawLine( (int)(250*Math.random()), (int)(250*Math.random()), (int)(250*Math.random()), (int)(250*Math.random()), (int)(250*Math.random()), (int)(250*Math.random()) ); (int)(250*Math.random()), (int)(250*Math.random()) ); } } } } } } 15 16

  5. when to repaint() ? a screen with an application The screen is a single painting layer All windows are drawn on the same layer Windows do not know what they may hide Need to repaint every time a new zone of the screen becomes visible repainting events (handled by the system) window open, resizing, bring to front/back, minimize when other windows modify the screen 17 18 a screen with 3 applications a screen, after closing one application -1 closing window notifies system of state, screen sends msg for repaint 19 20

  6. a screen, after closing one application - 2 a screen, after closing one application - 3 remaining windows receive the message to repaint themselves each window asks their components to repaint themselves 21 22 a screen, after closing one application - 4 painting in Java : repaint() all updated! Repaint event: Java Swing components can receive repain t events They call their function paintComponent ( ) E ach component can inherit and redefine its paintComponent() We call repaint( ), it calls paintComponent() or paint() paint() vs paintComponent() paint() comes from AWT, in Swing paint() triggers: paintComponent(), paintBorder(), and paintChildren() 23 24

  7. import javax.swing.*; import javax.swing.*; import java.awt.*; import java.awt.*; public class SwingDemo7 extends JFrame { public class SwingDemo7 extends JFrame { public JPanel panel; public JPanel panel; public void init() { public void init() { Container cp = getContentPane(); Container cp = getContentPane(); this.setTitle("example 7"); this.setTitle("example 7"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setDefaultCloseOperation(EXIT_ON_CLOSE); panel = new JPanel(); panel = new JPanel(); cp.add(panel); cp.add(panel); } } public void paint(Graphics g) { public void paint(Graphics g) { g = this.panel.getGraphics(); g = this.panel.getGraphics(); Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g; What is going on here? g2.setColor(Color.RED); g2.setColor(Color.RED); for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) { g2.drawLine((int) (250 * Math.random()), g2.drawLine((int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random())); (int) (250 * Math.random())); } } } } public static void main(String[] args) { public static void main(String[] args) { SwingDemo7 frame = new SwingDemo7(); SwingDemo7 frame = new SwingDemo7(); frame.init(); frame.init(); frame.setSize(250,250); frame.setSize(250,250); what happens after resizing? what happens after resizing? frame.setVisible(true); frame.setVisible(true); } } 25 26 } } import javax.swing.*; import java.awt.*; painting in Java : repaint() public class SwingDemo7 extends JFrame { public JPanel panel; public void init() { Automatic repainting of windows can be intelligent, Container cp = getContentPane(); this.setTitle("example 7"); and not repaint if things have not changed in the this.setDefaultCloseOperation(EXIT_ON_CLOSE); window. panel = new JPanel(); cp.add(panel); } public void paint(Graphics g) { super.paintComponents(g); As a designer it is best to be have the state of your g = this.panel.getGraphics(); graphical objects ready to repaint at any given time Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); for (int i = 0; i < 100; ++i) { g2.drawLine((int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random()), (int) (250 * Math.random())); } } public static void main(String[] args) { SwingDemo7 frame = new SwingDemo7(); frame.init(); what happens after resizing? frame.setSize(250,250); frame.setVisible(true); 27 28 } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend