Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21 Drawing In Java • Background – Windowed applications in Java use the Swing framework • Swing – part of the Java Foundation Classes that provide a (nearly) platform independent way of making GUI applications • Provides things like windows, buttons, text fields, radio buttons, etc. – Apps with 2D graphics use the 2DGraphics API – All of this is object oriented • a button is an object, etc. Let’s Make a Window import javax.swing.*; Where lots of GUI stuff lives in the Java API public class SimpleJFrame extends JFrame { public SimpleJFrame () { this.setTitle("This is Awesome!"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleJFrame s = new SimpleJFrame(); } } Let’s Make a Window import javax.swing.*; public class SimpleJFrame extends JFrame { We’re extending the functionality of the JFrame class public SimpleJFrame () { this.setTitle("This is Awesome!"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleJFrame s = new SimpleJFrame(); } } Create a new SimpleJFrame Object Let’s Make a Window import javax.swing.*; public class SimpleJFrame extends JFrame { public SimpleJFrame () { this.setTitle("This is Awesome!"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleJFrame s = new SimpleJFrame(); } } Calling superclass methods that are part of JFrame… Let’s Make a Window import javax.swing.*; public class SimpleJFrame extends JFrame { We’re extending the functionality of the JFrame class public SimpleJFrame () { this.setTitle("This is Awesome!"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleJFrame s = new SimpleJFrame(); } } Create a new SimpleJFrame Object Now, with a button //New Import import java.awt.*; import javax.swing.*; public class SimpleFrameWithComponents extends JFrame { public SimpleFrameWithComponents () { this.setTitle("This is more Awesome!"); this.setSize(500, 500); this.setLayout(new FlowLayout()); JButton jb = new JButton("Press me!"); this.getContentPane().add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleFrameWithComponents s = new SimpleFrameWithComponents(); } } Coordinates in a Window [0,0] x Pixel y Let’s Draw import import import import java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; javax.swing.JFrame; Some new Imports for graphics and drawing public class SimpleDraw extends JFrame{ SimpleDraw() { Calling superclass methods this.setSize(300, 300); this.setVisible(true); to set up the window this.setBackground(Color.BLUE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fillOval(50, 50, 20, 20); g2.setColor(Color.MAGENTA); g2.drawString("Hello There", 100, 100); } public static void main (String [] args) { SimpleDraw s = new SimpleDraw(); } } Let’s Draw import import import import java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; javax.swing.JFrame; public class SimpleDraw extends JFrame{ Sets the background color of the frame. Color options: SimpleDraw() { this.setSize(300, 300); Some color this.setVisible(true); this.setBackground(Color.BLUE); Color.BLUE this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Color.RED } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fillOval(50, 50, 20, 20); g2.setColor(Color.MAGENTA); g2.drawString("Hello There", 100, 100); } public static void main (String [] args) { SimpleDraw s = new SimpleDraw(); } } options: Color.MAGENTA Color.PINK Color.BLACK etc. Let’s Draw import import import import java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; javax.swing.JFrame; public class SimpleDraw extends JFrame{ SimpleDraw() { this.setSize(300, 300); this.setVisible(true); this.setBackground(Color.BLUE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fillOval(50, 50, 20, 20); g2.setColor(Color.MAGENTA); g2.drawString("Hello There", 100, 100); } public static void main (String [] args) { SimpleDraw s = new SimpleDraw(); } } method that is part being overridden in our class.. From the super class. Let’s Draw import import import import java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; javax.swing.JFrame; public class SimpleDraw extends JFrame{ SimpleDraw() { this.setSize(300, 300); this.setVisible(true); this.setBackground(Color.BLUE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fillOval(50, 50, 20, 20); g2.setColor(Color.MAGENTA); g2.drawString("Hello There", 100, 100); } public static void main (String [] args) { SimpleDraw s = new SimpleDraw(); } } Turns the graphic object parameter into a 2D graphics object Paints an oval, then a string Some Options of what you can draw • void drawRect(int x, int y, int width, int height); • void drawOval(int x, int y, int width, int height) • void drawLine(int x1, int y1, int x2, int y2) • void fillOval(int x, int y, int width, int height) • void fillRect(int x, int y, int width, int height) Can You Draw This? Can You Draw This? Breakout: Use the graphics drawing functionality to draw a picture of the playing field including the boundaries, lines, center line, box and robot!