1 / 19

Frame Windows

Frame Windows. A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame class can be used to create an object of this type. http://java.sun.com/j2se/1.5.0/docs/api/

lewis-glass
Download Presentation

Frame Windows

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. Frame Windows • A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. • The JFrame class can be used to create an object of this type. http://java.sun.com/j2se/1.5.0/docs/api/ • The JFrame class is in the javax.swing package. JFrame frame = new JFrame();frame.setSize(300, 400);frame.setTitle("An Empty Frame");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);

  2. Component Coordinate System 0,0 largest_x, 0 0,largest_y largest_x, largest_y x increases  y increases i

  3. File LineViewer.java 01: import javax.swing.*; 02: 03:public class LineViewer 04: { 05:public static void main(String[] args) 06: { 07: JFrame frame = new JFrame(); 08: 09:final int FRAME_WIDTH = 300; 10:final int FRAME_HEIGHT = 400; 11: 12: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 13: frame.setTitle("An Empty Frame"); 14: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15: 16: frame.setVisible(true); 17: } 18: }

  4. A Frame Window This ia what LineViewer looks like when it runs.

  5. JComponent class Suppose you wish to draw something in the frame. You cannot draw directly onto the frame, but you can draw on an object of type JComponent. So the idea is to: 1) Create an object of type JComponent 2) Add the JComponent object to the frame. 3) The JComponent class is in javax.swing class.

  6. Creating a JComponent object • Create a class which ‘’extends’ the JComponent class. This class is then also of type JComponent. • The ALine class now ‘inherits’ everything that the JComponent class provides, in particular a method named paintComponent. • paintComponent is called whenever the JComponent needs to be repainted. ALine redefines the paintComponent method to draw specifically what the ALine component was defined to draw. import javax.swing.JComponent; public class ALine extends JComponent { public void paintComponent (Graphics gg) { …. Drawing statements … } }

  7. Drawing Shapes • Graphics class lets you manipulate the graphics state (such as current color) • Graphics2D class has methods to draw shape objects • Use a cast to recover the Graphics2D object from the Graphics parameter import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; public class ALine extends JComponent { public void paintComponent (Graphics gg) { Graphics2D g2 = (Graphics2D) gg; …. Drawing statements … } }

  8. Drawing Lines • To draw a line, you must create a line type object and then draw it. The Graphics2D class has a method draw(Shape ashape). Is the Line2D.Double object of type Shape? • http://java.sun.com/j2se/1.5.0/docs/api/ • Yes, Line2D.Double ‘implements’ Shape . If a class ‘implements’ Shape, an object of that class type is also of type Shape. Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); g2.draw(segment); //will draw the line

  9. // Aline.java import javax.swing.JComponent; import java.awt.geom.Line2D.Double; import java.awt.Graphics; import java.awt.Graphics2D; public class ALine extends JComponent { public void paintComponent (Graphics gg) { Graphics2D g2 = (Graphics2D) gg; Line2D.Double line; //create Line2D.Double object line = new Line2D.Double(100, 100, 300, 200); // Line2D is a “Shape” type class, so it // the line object can be passed to the draw method // of the Graphics2D object g2.draw(line); }

  10. Line Drawing Program Classes • ALine: its paintComponent method produces the drawing of the line • LineViewer: its main method constructs a frame and an ALine object , (which is a JComponent). The main method then adds the component to the frame, and makes the frame visible

  11. Updated LineViewer.java import javax.swing.JFrame; public class LineFrame { public static void main(String[] args) { final int SIDELEN = 400; //create frame, set up dimensions and title JFrame theFrame = new JFrame(); theFrame.setSize(SIDELEN, SIDELEN); theFrame.setTitle("Demo w/ a line"); //so user can close window theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create an ALine component, add it and view ALine lineDrawer = new ALine(); theFrame.add(lineDrawer); theFrame.setVisible(true); } }

  12. How do you modify the program to draw two lines? Hint: This would be a modification to the LineVIewer object. What happens if your Aline paintComponent method calls gg.draw(box) instead of g2.draw(box)? Hint: gg is an object of the Graphics class, Does the Graphics class provide this method?

  13. Graphical Shapes • Ellipse2D.Double,and Rectangle2D.Double classes also exist • These classes are inner classes–doesn't matter to us except for the import statement: • Must construct and draw the shape import java.awt.geom.Ellipse2D; // no .Double Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height);g2.draw(ellipse);

  14. An Ellipse Figure 6:An Ellipse and Its Bounding Box

  15. Drawing Strings g2.drawString("Message", 50, 100); //specify basepoint Figure 7:Basepoint and Baseline

  16. Colors • Standard colors Color.BLUE, Color.RED, Color.PINK etc. • Can create customized Color object by specifying red, green, blue between 0.0F and 1.0F(float values). Use constructor: Color (float red, float green, float blue) Color magenta = newColor(1.0F, 0.0F, 1.0F); • To set color in graphics context, use setColor method g2.setColor(magenta); • Color is used when drawing and filling shapes g2.fill(rectangle); // shape filled with current color g2.draw(rectangle); // shape outline with current color

  17. What are the RGB color values of Color.BLUE? How do you draw a yellow square on a red background? 0.0F, 0.0F, and 0.1F g2.setColor(Color.RED);g2.fill(new Rectangle2D.Double(0, 0, 200, 200));g2.setColor(Color.YELLOW);g2.fill(new Rectangle2D.Double(50, 50, 100, 100));

  18. import javax.swing.JComponent; • import java.awt.Graphics; • import java.awt.Graphics2D; • import java.awt.Color; • import java.awt.geom.Line2D; • // this class creates a Jcomponent which draws a square • public class Squares extends JComponent { • public void paintComponent (Graphics gg) { • Graphics2D g2 = (Graphics2D) gg; • Line2D.Double line; //object will be reused • double xpos, ypos; // upper left corner of square • //initialize position and color for square • //Note that all lines are specified relative to the initial //position which will make MOVING the square much easier • xpos = 250.0; • ypos = 100.0; • g2.setColor(Color.red); • // now create lines and draw the square • ………… Continued on next slide……………

  19. line = new Line2D.Double(xpos, ypos, xpos + 100, ypos); • g2.draw(line); //draw first line • //next three lines drawn reuse the same object • line.setLine(xpos + 100, ypos, xpos + 100, ypos + 100); • g2.draw(line); //draw second line • line.setLine(xpos + 100, ypos + 100 ,xpos, ypos + 100); • g2.draw(line); // draw third line • line.setLine(xpos, ypos + 100, xpos, ypos); • g2.draw(line); // draw fourth line • } }

More Related