160 likes | 240 Views
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
E N D
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 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(); } }
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
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…
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
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(); } }
Coordinates in a Window x [0,0] Pixel y
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
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.
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.
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
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)
Breakout: Use the graphics drawing functionality to draw a picture of the playing field including the boundaries, lines, center line, box and robot!