Download Powerpoint lecture on Frame Applications

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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);
Component Coordinate System
0,0
0,largest_y
largest_x, 0
largest_x,
largest_y
x increases 
y increases i
File LineViewer.java
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
import javax.swing.*;
public class LineViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("An Empty Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
A Frame Window
This ia what LineViewer looks
like when it runs.
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.
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 …
}
}
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 …
}
}
Drawing Lines
• To draw a line, you must create a line type object
Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2);
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.
g2.draw(segment);
//will draw the line
// 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);
}
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
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);
}
}
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?
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);
An Ellipse
Figure 6:
An Ellipse and Its Bounding Box
Drawing Strings
g2.drawString("Message", 50, 100);
Figure 7:
Basepoint and Baseline
//specify basepoint
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 = new Color(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
What are the RGB color values of
Color.BLUE?
0.0F, 0.0F, and 0.1F
How do you draw a yellow square on a red
background?
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));
•
•
•
•
•
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……………
•
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);
•
line.setLine(xpos + 100, ypos + 100 ,xpos, ypos + 100);
•
g2.draw(line); // draw third line
//draw second line
•
•
line.setLine(xpos, ypos + 100, xpos, ypos);
•
g2.draw(line);
•
}
}
// draw fourth line