Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 224553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:19:39+00:00 2026-05-11T19:19:39+00:00

** Why is the class CanvasPane not included in the Java API?. import javax.swing.*;

  • 0

** Why is the class CanvasPane not included in the Java API?.

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

    /**
     * Class Canvas - a class to allow for simple graphical 
     * drawing on a canvas.
     * 
     * @author Michael Kolling (mik)
     * @author Bruce Quig
     *
     * @version 2008.03.30
     */

    public class Canvas
    {
        private JFrame frame;
        private CanvasPane canvas;
        private Graphics2D graphic;
        private Color backgroundColor;
        private Image canvasImage;

        /**
         * Create a Canvas with default height, width and background color 
         * (300, 300, white)
         * @param title  title to appear in Canvas Frame     
         */
        public Canvas(String title)
        {
            this(title, 300, 300, Color.white);        
        }

        /**
         * Create a Canvas with default background color (white).
         * @param title  title to appear in Canvas Frame
         * @param width  the desired width for the canvas
         * @param height  the desired height for the canvas
         */
        public Canvas(String title, int width, int height)
        {
            this(title, width, height, Color.white);
        }

        /**
         * Create a Canvas.
         * @param title  title to appear in Canvas Frame
         * @param width  the desired width for the canvas
         * @param height  the desired height for the canvas
         * @param bgClour  the desired background color of the canvas
         */
        public Canvas(String title, int width, int height, Color bgColor)
        {
            frame = new JFrame();
            canvas = new CanvasPane();
            frame.setContentPane(canvas);
            frame.setTitle(title);
            canvas.setPreferredSize(new Dimension(width, height));
            backgroundColor = bgColor;
            frame.pack();
        }

        /**
         * Set the canvas visibility and brings canvas to the front of screen
         * when made visible. This method can also be used to bring an already
         * visible canvas to the front of other windows.
         * @param visible  boolean value representing the desired visibility of
         * the canvas (true or false) 
         */
        public void setVisible(boolean visible)
        {
            if(graphic == null) {
                // first time: instantiate the offscreen image and fill it with
                // the background color
                Dimension size = canvas.getSize();
                canvasImage = canvas.createImage(size.width, size.height);
                graphic = (Graphics2D)canvasImage.getGraphics();
                graphic.setColor(backgroundColor);
                graphic.fillRect(0, 0, size.width, size.height);
                graphic.setColor(Color.black);
            }
            frame.setVisible(true);
        }

        /**
         * Provide information on visibility of the Canvas.
         * @return  true if canvas is visible, false otherwise
         */
        public boolean isVisible()
        {
            return frame.isVisible();
        }

        /**
         * Draw the outline of a given shape onto the canvas.
         * @param  shape  the shape object to be drawn on the canvas
         */
        public void draw(Shape shape)
        {
            graphic.draw(shape);
            canvas.repaint();
        }

        /**
         * Fill the internal dimensions of a given shape with the current 
         * foreground color of the canvas.
         * @param  shape  the shape object to be filled 
         */
        public void fill(Shape shape)
        {
            graphic.fill(shape);
            canvas.repaint();
        }

        /**
         * Fill the internal dimensions of the given circle with the current 
         * foreground color of the canvas.
         */
        public void fillCircle(int xPos, int yPos, int diameter)
        {
            Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
            fill(circle);
        }

        /**
         * Fill the internal dimensions of the given rectangle with the current 
         * foreground color of the canvas. This is a convenience method. A similar 
         * effect can be achieved with the "fill" method.
         */
        public void fillRectangle(int xPos, int yPos, int width, int height)
        {
            fill(new Rectangle(xPos, yPos, width, height));
        }

        /**
         * Erase the whole canvas.
         */
        public void erase()
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            Dimension size = canvas.getSize();
            graphic.fill(new Rectangle(0, 0, size.width, size.height));
            graphic.setColor(original);
            canvas.repaint();
        }

        /**
         * Erase the internal dimensions of the given circle. This is a 
         * convenience method. A similar effect can be achieved with
         * the "erase" method.
         */
        public void eraseCircle(int xPos, int yPos, int diameter)
        {
            Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
            erase(circle);
        }

        /**
         * Erase the internal dimensions of the given rectangle. This is a 
         * convenience method. A similar effect can be achieved with
         * the "erase" method.
         */
        public void eraseRectangle(int xPos, int yPos, int width, int height)
        {
            erase(new Rectangle(xPos, yPos, width, height));
        }

        /**
         * Erase a given shape's interior on the screen.
         * @param  shape  the shape object to be erased 
         */
        public void erase(Shape shape)
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            graphic.fill(shape);              // erase by filling background color
            graphic.setColor(original);
            canvas.repaint();
        }

        /**
         * Erases a given shape's outline on the screen.
         * @param  shape  the shape object to be erased 
         */
        public void eraseOutline(Shape shape)
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            graphic.draw(shape);  // erase by drawing background color
            graphic.setColor(original);
            canvas.repaint();
        }

        /**
         * Draws an image onto the canvas.
         * @param  image   the Image object to be displayed 
         * @param  x       x co-ordinate for Image placement 
         * @param  y       y co-ordinate for Image placement 
         * @return  returns boolean value representing whether the image was 
         *          completely loaded 
         */
        public boolean drawImage(Image image, int x, int y)
        {
            boolean result = graphic.drawImage(image, x, y, null);
            canvas.repaint();
            return result;
        }

        /**
         * Draws a String on the Canvas.
         * @param  text   the String to be displayed 
         * @param  x      x co-ordinate for text placement 
         * @param  y      y co-ordinate for text placement
         */
        public void drawString(String text, int x, int y)
        {
            graphic.drawString(text, x, y);   
            canvas.repaint();
        }

        /**
         * Erases a String on the Canvas.
         * @param  text     the String to be displayed 
         * @param  x        x co-ordinate for text placement 
         * @param  y        y co-ordinate for text placement
         */
        public void eraseString(String text, int x, int y)
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            graphic.drawString(text, x, y);   
            graphic.setColor(original);
            canvas.repaint();
        }

        /**
         * Draws a line on the Canvas.
         * @param  x1   x co-ordinate of start of line 
         * @param  y1   y co-ordinate of start of line 
         * @param  x2   x co-ordinate of end of line 
         * @param  y2   y co-ordinate of end of line 
         */
        public void drawLine(int x1, int y1, int x2, int y2)
        {
            graphic.drawLine(x1, y1, x2, y2);   
            canvas.repaint();
        }

        /**
         * Sets the foreground color of the Canvas.
         * @param  newColor   the new color for the foreground of the Canvas 
         */
        public void setForegroundColor(Color newColor)
        {
            graphic.setColor(newColor);
        }

        /**
         * Returns the current color of the foreground.
         * @return   the color of the foreground of the Canvas 
         */
        public Color getForegroundColor()
        {
            return graphic.getColor();
        }

        /**
         * Sets the background color of the Canvas.
         * @param  newColor   the new color for the background of the Canvas 
         */
        public void setBackgroundColor(Color newColor)
        {
            backgroundColor = newColor;   
            graphic.setBackground(newColor);
        }

        /**
         * Returns the current color of the background
         * @return   the color of the background of the Canvas 
         */
        public Color getBackgroundColor()
        {
            return backgroundColor;
        }

        /**
         * changes the current Font used on the Canvas
         * @param  newFont   new font to be used for String output
         */
        public void setFont(Font newFont)
        {
            graphic.setFont(newFont);
        }

        /**
         * Returns the current font of the canvas.
         * @return     the font currently in use
         **/
        public Font getFont()
        {
            return graphic.getFont();
        }

        /**
         * Sets the size of the canvas.
         * @param  width    new width 
         * @param  height   new height 
         */
        public void setSize(int width, int height)
        {
            canvas.setPreferredSize(new Dimension(width, height));
            Image oldImage = canvasImage;
            canvasImage = canvas.createImage(width, height);
            graphic = (Graphics2D)canvasImage.getGraphics();
            graphic.drawImage(oldImage, 0, 0, null);
            frame.pack();
        }

        /**
         * Returns the size of the canvas.
         * @return     The current dimension of the canvas
         */
        public Dimension getSize()
        {
            return canvas.getSize();
        }

        /**
         * Waits for a specified number of milliseconds before finishing.
         * This provides an easy way to specify a small delay which can be
         * used when producing animations.
         * @param  milliseconds  the number 
         */
        public void wait(int milliseconds)
        {
            try
            {
                Thread.sleep(milliseconds);
            } 
            catch (InterruptedException e)
            {
                // ignoring exception at the moment
            }
        }

        /************************************************************************
         * Inner class CanvasPane - the actual canvas component contained in the
         * Canvas frame. This is essentially a JPanel with added capability to
         * refresh the image drawn on it.
         */
        private class CanvasPane extends JPanel
        {
            public void paint(Graphics g)
            {
                g.drawImage(canvasImage, 0, 0, null);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:19:39+00:00Added an answer on May 11, 2026 at 7:19 pm

    Why it should be? I mean its a BlueJ extension class, AFAIK. Similar questions may come to the mind regarding StringUtils, or NumberUtils. IMHO, these two really qualifies to be there in the original Java API. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Typicaly you don't want the Data tier to have any… May 12, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer I think that the Stopwatch class is what you are… May 12, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer NHibernates requires all collections to be mapped as ISomething to… May 12, 2026 at 6:18 pm

Related Questions

Here is the page I have a problem with on each image (little square)
If I have a List < Person > where person is defined by the
I have my class X which inherits from Qt's class Base . I declared
I have a c++ header file containing a class. I want to use this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.