Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: DrawingBoard Class

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default DrawingBoard Class

    anyone can help me where could i find this class from the book im reading ...

    the book name is
    A Comprehensive Intro to OOP with Java
    by C. Thomas Wu,
    a Mc Graw - Hill International Edition and im in chapter 5 with a simple program named "Drawing Shapes" that
    simulates a screensaver.. what im having right now .. is the problem that i dont know where could i find
    the whole code of this DrawingBoard class , this class will handle the motion(the only problem i have) of the shape that will be drawn(but not only the motion). As the class's dictated signature, this DrawingBoard will be the drawing board for the shapes that will be drawn(this will be the resonsible for the shapes' behavior).. and theres another class that will handle all the shapes that will be generated for that DrawingBoard(i dont have problems with this class)..

    anyone know this book that im reading ? can anyone guide me about this thing!

    tnx in advance!


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: DrawingBoard Class

    i guess its not easy to understand this thread , anyway im trying to deal with this on my own...

    so now, im havig some problem


    ill post the code first

    the DrawingBoard Class

    import javax.swing.*;
    import java.awt.*;
     
    /**
     *
     * @author
     */
     
    public class DrawingBoard {
     
        private Container drawingBoard;
        private JFrame frame;
     
        public DrawingBoard() {
     
            frame = new JFrame("My Window!");
        }
     
        public void addShape() {
     
            /**
             * Adds shape to this DrawingBoard object.You can add an unlimited number
             * DrawableShape objects.
             */
        }
     
        public void setBackground(Color color) {
     
            /**
             * Sets the background of this drawingBoard object to the designated
             * color.The default background color is black
             */
            drawingBoard = frame.getContentPane();
            drawingBoard.setBackground(color);
        }
     
        public void setDelayTime() {
     
            /**
             * Sets the delay time between drawings to delay seconds.The smaller the
             * delay time, the faster the shapes move.If the movement type is other
             * than smooth, then setting the delay time has no visual effect.
             */
        }
     
        public void setMovement() {
     
            /**
             * Sets the movement type.Class constants for three types of motion are
             * Movement.STATIONARY - draw shapes at fixed positions.
             * Movement.RANDOM - draw shapes at random motion.
             * Movement.SMOOTH - draw shapes in a smooth motion.
             */
        }
     
        public void setVisible(boolean state) {
     
            /**
             * Make this DrawingBoard object appear on or disappear from the screem.
             * If "state" is "true" or "false" respectively. To simulate the screensaver,
             * setting it visible will cause a maximized window to appear on the screen.
             */
     
            frame.setVisible(state);
        }
     
        public void start () {
     
            /**
             * Starts the drawing.If the window is visible yet, it will be made visible
             * before the drawing begins.
             */
            frame.setLocation(300, 300);
            frame.setSize(400, 400);                   
        }
    }

    the main class

    import java.awt.Color;
     
    /**
     *
     * @author
     */
     
    public class Ch5DrawShape {
     
        private DrawingBoard canvas;
     
        public Ch5DrawShape() {
     
            canvas = new DrawingBoard();
        }
     
        public void start() {
     
            canvas.setVisible(true);
        }
     
        public static void main(String[] args) {
     
            DrawingBoard dboard = new DrawingBoard();
     
            Ch5DrawShape screensaver = new Ch5DrawShape();
     
            dboard.start();
            screensaver.start();  // but when i change this in this way -- > dboard.setVisible(true);
                                  //it runs perfectly..
            dboard.setBackground(Color.BLACK);
     
        }
    }

    im just checking if the the frame is displaying together with the content pane which must be BLACK..

    i hope you understand the flow... run it first... and read the comment inside the "main" program..



    problem:

    - why is it when the screensaver.start() method doesnt make the contentpane appear?

    but when i change it this way
    dboard.setVisible(true);
    the content pane is displaying?

    how come that it doesnt work perfectly in this kind of statement? >>
    screensaver.start()
    when the method start is already invoked?

    the field canvas is an object for the DrawingBoard class..
    and same as the dboard object.

    but why is it not working perfectly when im using the canvas object to set a boolean state in the setVisible method?

    i hope you understand what im trying to ask here..

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Tabriz-Iran
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: DrawingBoard Class

    /*
    Introduction to OOP with Java (5th Ed), McGraw-Hill

    Wu/Otani

    Chapter 5 The DrawingBoard class

    File: DrawingBoard.java

    */

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;

    /**
    * This window simulates a screensaver by drawing shapes. To use this class you must
    * define a class named Shape and provide the following methods:
    * <li>
    * public void setBackground(java.awt.Color)
    * public void setCenterPoint(java.util.Point)
    * public java.util.Point getCenterPoint()
    * public void draw(java.awt.Graphics)
    *
    * @author Dr. Caffeine
    */
    public class DrawingBoard extends JFrame implements Runnable {

    //----------------------------------
    // Data Members:
    //----------------------------------

    /**
    * Types of movements when drawing shapes
    */
    public static enum Movement {SMOOTH, RANDOM, STATIONARY}

    /**
    * Constant for delay time (in seconds) between drawing
    */
    private static final double DEFAULT_DELAY_TIME = 0.15;

    /**
    * Constant for default background color
    */
    private static final Color DEFAULT_BACKGROUND = Color.BLACK;

    /**
    * List to remember the shapes for drawing
    */
    private java.util.List<DrawableShape> shapeList;

    /**
    * List of (deltaX,deltaY) pairs represented as Point
    */
    private java.util.List<Point> offsetList;

    /**
    * Delay time (in seconds) between drawing
    */
    private double delayTime;

    /**
    * Type of drawing -- SMOOTH, RANDOM, or STATIONARY
    */
    private Movement drawingType;

    /**
    * Size of this window
    */
    private Dimension windowSize;

    /**
    * Border size of this frame
    */
    private Insets inset;

    /**
    * An Image used for double-buffering.
    */
    private Image offScreenImage;

    /**
    * A Graphics object associated to the offscreen Image used for double-buffering.
    */
    private Graphics offScreenGraphics;

    /**
    * Background color
    */
    private Color bkcolor;


    //-----------------------------------------
    //
    // Constructors:
    //
    //-----------------------------------------

    /**
    * Default Constructor.
    *
    */
    public DrawingBoard( ) {
    this(DEFAULT_DELAY_TIME, Movement.STATIONARY);
    }

    /**
    * Sets the delay time to the given parameter.
    *
    * @param delayTime delay time between drawings
    * @param type drawing type -- SMOOTH, RANDOM, STATIONARY
    */
    public DrawingBoard(double delayTime, Movement type) {

    this.delayTime = delayTime;
    this.drawingType = type;

    shapeList = new ArrayList<DrawableShape>();

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    windowSize = toolkit.getScreenSize();
    setBounds(0, 0, windowSize.width, windowSize.height);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);

    bkcolor = DEFAULT_BACKGROUND;
    }


    //-------------------------------------------------
    // Public Methods:
    //
    // public void addShape(DrawableShape)
    // public void draw(Graphics)
    //
    // public void setBackground(Color)
    // public void setDelayTime(double)
    // public void setMovement(int)
    // public void setVisible(boolean)
    //
    // public void paint(Graphics)
    // public void run( )
    // public void start( )
    //
    // public Point getCenterPoint()
    //
    //
    //------------------------------------------------


    /**
    * Adds the shape to be drawn on this drawing board
    *
    * @param shape the shape to be drawn
    */
    public void addShape(DrawableShape shape) {

    shapeList.add(shape);

    //assign a random point as the shape's center point
    //if one is not defined yet
    Point pt = shape.getCenterPoint();
    if (pt == null) {
    Dimension dim = shape.getDimension();

    int xMargin = (int) Math.round(dim.width / 2);
    int yMargin = (int) Math.round(dim.height / 2);

    pt = new Point(getRandom(xMargin, windowSize.width - xMargin),
    getRandom(yMargin, windowSize.height - yMargin));

    shape.setCenterPoint(pt);
    }
    }


    /**
    * Sets the background color.
    *
    * @param background background color
    */
    public void setBackground(Color background) {
    super.setBackground(background);
    bkcolor = background;
    }


    /**
    * Sets the delay time between the drawings.
    *
    * @param delayTime the delay time
    */
    public void setDelayTime(double delayTime) {

    if (delayTime < 0) delayTime = -delayTime;

    this.delayTime = delayTime;
    }

    /**
    * Sets the drawing type. Invalid parameter will
    * be ignored causing no changes.
    *
    * @param drawingType the drawing type
    */
    public void setMovement(Movement drawingType) {

    this.drawingType = drawingType;
    }

    /**
    * Overrides the inherited setVisible to make
    * this frame appear on the screen as maximized and
    * with the black background.
    *
    * @param state true for visible; false for invisible
    */
    public void setVisible(boolean state) {

    super.setVisible(state);
    inset = getInsets();

    offScreenImage = createImage(windowSize.width, windowSize.height);
    offScreenGraphics = offScreenImage.getGraphics();

    repaint();
    }


    /**
    * Paint the contents by asking shapes to draw themselves
    *
    * @param Graphics the graphics object to draw lines
    */
    public void paint( Graphics g ) {

    if (offScreenGraphics != null) {

    offScreenGraphics.setColor(bkcolor);
    offScreenGraphics.fillRect( 0, 0,
    windowSize.width,
    windowSize.height );

    switch (drawingType) {

    case SMOOTH: smoothDrawing(offScreenGraphics);
    break;

    case RANDOM: randomDrawing(offScreenGraphics);
    break;

    case STATIONARY: stationaryDrawing(offScreenGraphics);
    break;
    }

    g.drawImage ( offScreenImage, 0, 0, this );
    }
    }


    /**
    * Implements the Runnable interface.
    */
    public void run( ) {

    while (true) {

    try {
    Thread.sleep((long)(delayTime * 1000));

    } catch (Exception e) { }

    repaint( );
    }
    }


    /**
    * Starts the drawing
    */
    public void start( ) {

    if (!isVisible()) {
    this.setVisible(true);
    }

    Thread thread = new Thread(this);

    thread.start();
    }


    //-------------------------------------------------
    // Private Methods:
    //
    // public void createOffsetList( )
    // public int getRandom(int, int)
    //
    // public void randomDrawing(Graphics)
    // public void smoothDrawing(Graphics)
    // public void stationaryDrawing(Graphics)
    //
    //------------------------------------------------


    /**
    * Creates the offset list of (deltaX,deltaY).
    */
    private void createOffsetList( ) {

    if (offsetList == null) {

    offsetList = new ArrayList<Point>();

    for (int i = 0; i < shapeList.size(); i++) {

    Point pt = new Point(getRandom(10, 20),
    getRandom(10, 20));

    pt.x *= Math.round(Math.random()) == 0 ? 1 : -1;
    pt.y *= Math.round(Math.random()) == 0 ? 1 : -1;

    offsetList.add(pt);
    }
    }
    }

    /**
    * Returns a random number between
    * the passed parameters min and max.
    *
    * @param min the lower bound of the random number
    * @param max the upper bound of the random number
    */
    private int getRandom(int min, int max) {

    double temp = Math.random() * (max - min + 1);
    return (int) Math.floor(temp) + min;
    }


    /**
    * Draws the shapes at random locations
    *
    * @param Graphics the graphics context
    */
    private void randomDrawing(Graphics g) {

    Iterator itr = shapeList.iterator();

    while (itr.hasNext()) {

    DrawableShape s = (DrawableShape) itr.next();

    s.draw(g);

    Dimension dim = s.getDimension();
    int xMargin = dim.width / 2;
    int yMargin = dim.height / 2;

    //update the center point for next drawing
    s.setCenterPoint(
    new Point(getRandom(xMargin, windowSize.width - xMargin),
    getRandom(yMargin, windowSize.height- yMargin))
    );
    }

    }

    /**
    * Draws the shapes at continuous locations
    *
    * @param Graphics the graphics context
    */
    private void smoothDrawing(Graphics g) {

    int tempX, tempY;

    createOffsetList( );

    Iterator itr = shapeList.iterator();
    Iterator oItr = offsetList.iterator();

    while (itr.hasNext()) {

    DrawableShape s = (DrawableShape) itr.next();
    Point delta = (Point) oItr.next();

    Dimension dim = s.getDimension();
    int xMargin = (int) Math.round(dim.width / 2);
    int yMargin = (int) Math.round(dim.height / 2);

    s.draw(g);

    //update the center point for the next drawing
    Point pt = s.getCenterPoint( );

    pt.x += delta.x;
    pt.y += delta.y;

    //adjust for the boundary case
    if (pt.x > windowSize.width - xMargin - inset.right) {

    pt.x = windowSize.width - xMargin - inset.right;
    delta.x = -delta.x;

    } else if ( pt.x < inset.left + xMargin) {

    pt.x = inset.left + xMargin;
    delta.x = -delta.x;

    } else if (pt.y > windowSize.height - yMargin - inset.bottom) {

    pt.y = windowSize.height - yMargin - inset.bottom;
    delta.y = -delta.y;

    } else if (pt.y < inset.top + yMargin) {

    pt.y = inset.top + yMargin;
    delta.y = -delta.y;
    }
    }

    }

    /**
    * Draws the shapes at fixed, stationary locations
    *
    * @param Graphics the graphics context
    */
    private void stationaryDrawing(Graphics g) {

    Iterator itr = shapeList.iterator();

    while (itr.hasNext()) {

    DrawableShape s = (DrawableShape) itr.next();

    s.draw(g);
    }
    }
    }

  4. The Following User Says Thank You to saeedPort For This Useful Post:

    C++kingKnowledge (July 2nd, 2012)

Similar Threads

  1. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM