1 / 16

CSE 1341 Honors

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

archer
Download Presentation

CSE 1341 Honors

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. 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 Where lots of GUI stuff lives in the Java API 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) { SimpleJFrames = new SimpleJFrame(); } }

  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) { SimpleJFrames = 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) { SimpleJFrames = 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) { SimpleJFrames = 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("Thisis more Awesome!"); this.setSize(500, 500); this.setLayout(newFlowLayout()); JButtonjb = new JButton("Press me!"); this.getContentPane().add(jb); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main (String [] args) { SimpleFrameWithComponentss = new SimpleFrameWithComponents(); } }

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

  9. Let’s Draw 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(Graphicsg) { 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) { SimpleDraws = new SimpleDraw(); } } Some new Imports for graphics and drawing Calling superclass methods to set up the window

  10. Let’s Draw 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(Graphicsg) { 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) { SimpleDraws = new SimpleDraw(); } } Sets the background color of the frame. Color options: Some color options: Color.BLUE Color.RED Color.MAGENTA Color.PINK Color.BLACK etc.

  11. Let’s Draw 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(Graphicsg) { 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) { SimpleDraws = new SimpleDraw(); } } method that is part being overridden in our class.. From the super class.

  12. Let’s Draw 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(Graphicsg) { 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) { SimpleDraws = new SimpleDraw(); } } 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(intx, inty, int width, int height); • void drawOval(intx, inty, int width, int height) • void drawLine(int x1, int y1, int x2, int y2) • void fillOval(intx, inty, int width, int height) • void fillRect(intx, inty, int width, int height)

  14. Can You Draw This?

  15. Can You Draw This?

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

More Related