1 / 17

Any object can be drawn …

Any object can be drawn …. Suppose we are creating a Dog class ………….. public class Dog { private String name = “Fido”; //dog’s name private String breed = “unknown”; // dog’s breed public Dog (String nm, String brd){ name = nm;

Download Presentation

Any object can be drawn …

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. Any object can be drawn … Suppose we are creating a Dog class ………….. public class Dog { private String name = “Fido”; //dog’s name private String breed = “unknown”; // 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; } }

  2. And we wanted the capability of ‘drawing’ a dog…….. Remember, we have been using a Graphics2D object to draw shapes, If one of our Dog methods had access to a Graphics2D object, then it could ‘draw a dog’ ! SO.. we will add the following method to the Dog class.. public void draw(Graphics2D g2){ x = 10; 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); }

  3. 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; }

  4. 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); } }

  5. Drawing Complex Shapes • Good practice: Make a class for each graphical shape • Plan complex shapes by making sketches on graph paper class Car{ . . . public void draw(Graphics2D g2) { // Drawing instructions . . . }}

  6. 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); //draw both cars car2.draw(g2); } }

  7. 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: • getWidth and getHeight are applied to object that executes paintComponent (the JComponent object) • If window is resized paintComponent is called and car position recomputed int x = getWidth() - 60;int y = getHeight() - 30;Car car2 = new Car(x, y); Continued…

  8. File CarViewer.java import javax.swing.JFrame; public class CarViewer{ public static void main(String[] args) { JFrame frame = new JFrame(); finalint FRAME_WIDTH = 300; //set up frame finalint 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 } }

  9. Drawing Cars The Car Component Draws 2 Shapes

  10. Plan Complex Shapes on Graph Paper Using Graph Paper to Find Shape Coordinates

  11. 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);

  12. 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…

  13. Reading Text Input An Input Dialog Box

  14. 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

  15. 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); } }

  16. 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…

  17. 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); } }

More Related