Download on Drawing Figures

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

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

Document related concepts
no text concepts found
Transcript
Any object can be drawn …
Suppose we are creating a Dog class …………..
public class Dog {
private String name = “Fido”;
private String breed = “unknown”;
//dog’s name
// dog’s breed
public Dog (String nm, String brd){
name = nm;
breed = brd;
}
// .... We might have some other methods here
public String toString( ) {
return “ " + name;
}
}
And we wanted the capability of ‘drawing’ a dog……..
we will add the following method to the Dog class..
Remember, we have been using a Graphics2D object to draw
public void
draw(Graphics2D
g2){
shapes,
If one
of our Dog methods
had access to a Graphics2D
x = 10;
object,
then it could ‘draw a dog’ ! SO..
y = 10;
Ellipse2D.Double oval = new Ellipse2D.Double(x,y,100,50);
g2.draw(oval);
//eyes
oval = new Ellipse2D.Double(x+25,y+12,5,5);
g2.draw(oval);
oval = new Ellipse2D.Double(x+12,y+12,5,5);
g2.draw(oval);
//nose
oval = new Ellipse2D.Double(x+16,y+25,10,10);
g2.fill(oval);
// left ear
oval = new Ellipse2D.Double(x+62,y+13,50,75);
g2.fill(oval);
}
Any object can be drawn …
We may want to allow our Dog to be constructed with an initial
drawing position …
public class Dog {
String name = “Fido”;
String breed = “unknown”;
..position is now also specified as instance data
private int x = 10; //x,y drawing position, default 10,10
private int y = 10;
public Dog (String nm, String brd, int xpos, int ypos){
name = nm;
breed = brd;
x = xpos;
y = ypos;
}
Any object can be drawn …
Now any JComponent can easily add a Dog drawing …
import javax.swing.JComponent;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Color;
public class DrawPad extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// create two dog objects
Dog d1 = new Dog("George","Poodle", 10,10);
Dog d2 = new Dog( "Fred","mutt", 150,150);
// call Dog object’s Draw method
d1.draw(g2);
d2.draw(g2);
}
}
Drawing Complex Shapes
• Good practice: Make a class for each graphical shape
class Car
{
. . .
public void draw(Graphics2D g2)
{
// Drawing instructions
. . .
}
}
• Plan complex shapes by making sketches on graph paper
File CarComponent.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;:
// This component draws two car shapes.
public class CarComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Car car1 = new Car(0, 0); //car created at position 0,0
int x = getWidth() - Car.WIDTH; //another at lower right
int y = getHeight() - Car.HEIGHT;
Car car2 = new Car(x, y); //create car
car1.draw(g2);
car2.draw(g2);
}
}
//draw both cars
Drawing Cars
• Draw two cars: one in top-left corner of window, and another in
the bottom right
• Compute bottom right position, inside paintComponent
method:
int x = getWidth() - 60;
int y = getHeight() - 30;
Car car2 = new Car(x, y);
• getWidth and getHeight are applied to object that executes
paintComponent (the JComponent object)
• If window is resized paintComponent is called and car position
recomputed
Continued…
File CarViewer.java
import javax.swing.JFrame;
public class CarViewer{
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 300; //set up frame
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Two cars");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create component and add to frame
CarComponent component = new CarComponent();
frame.add(component);
frame.setVisible(true);
}
}
//make frame visible
Drawing Cars
The Car Component Draws 2 Shapes
Plan Complex Shapes on Graph
Paper
Using Graph Paper to
Find Shape Coordinates
draw method details..
public void draw(Graphics2D g2) {
Rectangle2D.Double body = new Rectangle2D.Double(xLeft, yTop + 10, 60, 10);
Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
Ellipse2D.Double rearTire= new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
// top of front windshield
Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
// front of roof
Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
// rear of roof
// bottom of rear windshield
Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
Reading Text Input
• A graphical application can obtain input by displaying a
JOptionPane
• The showInputDialog method displays a prompt and waits for
user input
• The showInputDialog method returns the string that the user
typed
String input = JOptionPane.showInputDialog("Enter x");
double x = Double.parseDouble(input);
Continued…
Reading Text Input
An Input Dialog Box
ColoredSquareComponent.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
// A component that shows a colored square.
public class ColoredSquareComponent extends JComponent {
private Color fillColor;
public ColoredSquareComponent(Color aColor) {
// param aColor the fill color for the square
fillColor = aColor;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(fillColor);
// now draw a filled square
ColoredSquareComponent.java
// now draw a filled square
final int SQUARE_LENGTH = 100;
Rectangle square = new Rectangle(
(getWidth() - SQUARE_LENGTH) / 2,
(getHeight() - SQUARE_LENGTH) / 2,
SQUARE_LENGTH,
SQUARE_LENGTH);
g2.fill(square);
}
}
ColorViewer.java- allows user to specify color
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ColorViewer {
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Continued…
ColorViewer.java- allows user to specify color
//ask user for color, create and draw component
String input;
input = JOptionPane.showInputDialog("red:");
double red = Double.parseDouble(input);
input = JOptionPane.showInputDialog("green:");
double green = Double.parseDouble(input);
input = JOptionPane.showInputDialog("blue:");
double blue = Double.parseDouble(input);
//create color object using user RBG values)
Color fillColor = new Color(
(float) red, (float) green, (float) blue);
ColoredSquareComponent component
= new ColoredSquareComponent(fillColor);
frame.add(component);
frame.setVisible(true);
}
}
Related documents