Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.

Similar presentations


Presentation on theme: "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21."— Presentation transcript:

1 CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21

2 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.

3 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(); } Where lots of GUI stuff lives in the Java API

4 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(); } We’re extending the functionality of the JFrame class Create a new SimpleJFrame Object

5 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…

6 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(); } We’re extending the functionality of the JFrame class Create a new SimpleJFrame Object

7 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(); }

8 Coordinates in a Window [0,0] x y Pixel

9 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import 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(); } Let’s Draw Some new Imports for graphics and drawing Calling superclass methods to set up the window

10 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import 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(); } Let’s Draw Sets the background color of the frame. Color options: Some color options: Color.BLUE Color.RED Color.MAGENTA Color.PINK Color.BLACK etc.

11 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import 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(); } Let’s Draw method that is part being overridden in our class.. From the super class.

12 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import 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(); } Let’s Draw Turns the graphic object parameter into a 2D graphics object Paints an oval, then a string

13 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)

14 Can You Draw This?

15

16 Breakout: Use the graphics drawing functionality to draw a picture of the playing field including the boundaries, lines, center line, box and robot!


Download ppt "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21."

Similar presentations


Ads by Google