Class CanvasPanel

  • All Implemented Interfaces:
    Colorizer, Initializable, ImageObserver, MenuContainer, Serializable, Accessible

    public class CanvasPanel
    extends JPanel
    implements Initializable, Colorizer

    A panel that allows drawing on it using a mouse. This class is basically a JPanel whos paint method has been altered to allow the ability to draw colors on it using mouse input. There is a lot of code used and edited with permission from Ray Toal (this class is essentially the code provided in the link, but edited to work with multiple line strokes and colors). When the canvas is drawn it looks like this:

    Since:
    JTalker 0.1.5
    Version:
    0.0.1
    Author:
    C. William Oswald
    See Also:
    JPanel, Serialized Form
    • Constructor Detail

      • CanvasPanel

        public CanvasPanel()
        Creates a CanvasPanel.
    • Method Detail

      • init

        public void init()
        Adds a CurveCreator to the CanvasPanel.
        Specified by:
        init in interface Initializable
      • setColor

        public void setColor​(Color currentColor)
        Sets the color to draw with.
        Specified by:
        setColor in interface Colorizer
        Parameters:
        currentColor -
      • paintComponent

        public void paintComponent​(Graphics g)
        Paints the CanvasPanel. This is where a lot of the graphical heavy lifting is done. Every time a new point is added to a curve, the canvas will redraw itself (calling this method). This will draw all of the points that have been added to the canvas, as well as draw them with the correct colors. It will draw them using lines between each point, and will use the stroke size defined in GUIConstants, 5. This code is inspired by this article.
        Overrides:
        paintComponent in class JComponent
        Parameters:
        g - The graphics context to use.