DrawingPanel
|
A simplified C# drawing window class based on DrawingPanel.java class that accompanies the Building Java Programs textbook. More...
Public Member Functions | |
DrawingPanel () | |
Constructs a new panel of a default width and height. More... | |
DrawingPanel (int width, int height) | |
Constructs a new panel of the given width and height. More... | |
DrawingPanel (int x, int y, int width, int height) | |
Constructs a new panel of the given width and height, with its top/left coordinate at the given (x, y) pixel. More... | |
DrawingPanel (string filename) | |
Constructs a new panel to display an image from the given file. The panel window will be sized to tightly fit that image. More... | |
void | Clear () |
Fills the panel's canvas area with its background color, wiping out any shapes that have been previously drawn. More... | |
void | Close () |
Closes the panel window, causing it to disappear from the screen. More... | |
Graphics | GetGraphics () |
Returns the Graphics object for this panel's canvas. Note that certain DrawingPanel methods invalidate the Graphics object and switch to a new one, such as when the panel is resized; for that reason, it is not recommended to store the result of GetGraphics for long periods in your code. Equivalent to the Graphics property; kept in for compatibility with some Java-isms. More... | |
Color | GetPixelColor (int x, int y) |
Returns the color of the given pixel in the panel's canvas. More... | |
Color [,] | GetPixelArray () |
Returns the colors of all pixels in the panel's canvas as a 2D array in [x, y] order. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixels. More... | |
Color [,] | GetPixels () |
Returns the colors of all pixels in the panel's canvas as a 2D array. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelArray. More... | |
int [,] | GetPixelsArgb () |
Returns the colors of all pixels in the panel's canvas as a 2D array of RGB integers. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelsRgb. More... | |
int [,] | GetPixelsRgb () |
Returns the colors of all pixels in the panel's canvas as a 2D array of RGB integers. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelsArgb. More... | |
void | Hide () |
Hides the window from the screen so that it will not be shown. You can cause the window to reappear by calling Show(). More... | |
bool | InBounds (int x, int y) |
Returns whether the given 0-based (x, y) pixel offset is within the bounds of this panel's drawing canvas area; in other words, if it is between (0, 0) and (width-1, height-1) inclusive. More... | |
void | Load (string filename) |
Reads an image from a file and draws it onto this panel's canvas at (0, 0). Also resizes the canvas to be the size of the given image. Equivalent to LoadImage. More... | |
void | LoadImage (string filename) |
Reads an image from a file and draws it onto this panel's canvas at (0, 0). Also resizes the canvas to be the size of the given image. Equivalent to Load. More... | |
void | MainLoop () |
Shows the panel window on the screen and loops waiting until it closes. More... | |
void | Pause () |
Causes the program to pause until the Enter key is pressed. see also: MainLoop. More... | |
void | RefreshDisplay () |
Causes the panel to redraw itself on the screen. You normally do not need to call this method, unless you have set the RepaintImmediately flag to false. Equivalent to Repaint. More... | |
void | Repaint () |
Causes the panel to redraw itself on the screen. You normally do not need to call this method, unless you have set the RepaintImmediately flag to false. Equivalent to RefreshDisplay. More... | |
void | Resize (int width, int height) |
Resizes this panel's canvas to be the given size. The surrounding window will resize to tightly fit the canvas at its new size. More... | |
void | Save () |
Saves the pixels of this panel's canvas to the default save file. More... | |
void | Save (string filename) |
Saves the pixels of this panel's canvas to the given file. More... | |
void | SetPixelArray (Color[,] pixels) |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixels. More... | |
void | SetPixelColor (int x, int y, Color color) |
Sets the color of the pixel at the given x/y coordinate to be the given color. More... | |
void | SetPixelColor (int x, int y, int r, int g, int b) |
Sets the color of the pixel at the given x/y coordinate to the given red, green, and blue values. More... | |
void | SetPixelColor (int x, int y, int a, int r, int g, int b) |
Sets the color of the pixel at the given x/y coordinate to the given alpha, red, green, and blue values. More... | |
void | SetPixelColor (int x, int y, int rgb) |
Sets the color of the pixel at the given x/y coordinate to the color represented by the given RGB integer. More... | |
void | SetPixelColorArgb (int x, int y, int argb) |
Sets the color of the pixel at the given x/y coordinate to the color represented by the given ARGB integer. More... | |
void | SetPixels (Color[,] pixels) |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixelArray. More... | |
void | SetPixelArray (int[,] pixels) |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels as RGB integers. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixels. More... | |
void | SetPixels (int[,] pixels) |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels as RGB integers. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixelArray. More... | |
void | Show () |
Causes the panel's window to display itself on the screen. More... | |
void | Sleep () |
Causes the program to pause for 1/60 of a second. Useful for producing animations. More... | |
void | Sleep (int ms) |
Causes the program to pause for the given number of milliseconds. Useful for producing animations. More... | |
void | ToBack () |
Moves the panel's window to the back of the Z-ordering of windows on the screen. More... | |
void | ToFront () |
Moves the panel's window to the front of the Z-ordering of windows on the screen. More... | |
void | Zoom (int zoomFactor) |
Zooms the drawing panel in/out to the given factor. A zoom factor of 1, the default, indicates normal size. A zoom factor of 2 would indicate 200% size, and so on. The factor value passed should be at least 1; if not, 1 will be used. More... | |
Static Public Member Functions | |
static int | GetAlpha (int argb) |
Returns the alpha component of the given ARGB integer, which is stored in bits 24-31. More... | |
static int | GetBlue (int rgb) |
Returns the blue component of the given RGB integer, which is stored in bits 0-7. More... | |
static int | GetGreen (int rgb) |
Returns the green component of the given RGB integer, which is stored in bits 8-15. More... | |
static int | GetRed (int rgb) |
Returns the red component of the given RGB integer, which is stored in bits 16-23. More... | |
static void | SaveAll () |
Saves all active drawing panels into image files. Not meant to be called by students. More... | |
static void | SetSaveFileName (string filename) |
Sets the file to be used when saving graphical output for all DrawingPanels. Not meant to be called by students. More... | |
static int | ToRgbInteger (int r, int g, int b) |
Returns an RGB integer made from the given red, green, and blue components from 0-255. The returned integer is suitable for use with various RGB integer methods in this class such as SetPixel. More... | |
static int | ToRgbInteger (int a, int r, int g, int b) |
Returns an RGB integer made from the given alpha, red, green, and blue components from 0-255. The returned integer is suitable for use with various RGB integer methods in this class such as SetPixel. More... | |
Properties | |
bool | Antialiased [get, set] |
Whether the panel performs smoothing on its pixels; default false. More... | |
Brush | Background [get, set] |
The background color painted onto the panel's drawing canvas; default white. When this is set, it clears the panel and fills its canvas area with the new background color. More... | |
int | Bottom [get, set] |
The y-coordinate of the bottom of the panel window, which is equal to its top y coordinate plus the window's height. More... | |
Form | Form [get] |
The Windows Form representing this panel's window. In general you should not need to directly access the window, and can mostly just call methods on the DrawingPanel itself or its Graphics object; but this is provided as a convenience. Note that calling some methods and properties on this form directly (e.g. Top, Width, Size) could cause the form state to go out of sync with the DrawingPanel state; this is not recommended. More... | |
Graphics | Graphics [get] |
The Graphics object that can be used to draw on the panel's canvas. Note that certain DrawingPanel methods invalidate the Graphics object and switch to a new one, such as when the panel is resized; for that reason, it is not recommended to store the returned Graphics property result for long periods in your code. More... | |
int | Height [get, set] |
The height of the panel's canvas area in pixels. The panel's window will size itself to tightly fit a canvas of this height. More... | |
int | Left [get, set] |
The x-coordinate of the left side of the panel window. Equivalent to X property. More... | |
Point | Location [get, set] |
The x/y location of the top-left corner of the panel window. More... | |
bool | RepaintImmediately [get, set] |
Whether the panel should automatically redraw itself after each drawing operation; default true. It can be useful to set this to false if you are about to perform a bunch of small drawing operations, such as setting the color of many individual pixels, and do not want the slowdown of repainting after each individual pixel changes color. More... | |
int | Right [get, set] |
The x-coordinate of the right side of the panel window, which is equal to its left x coordinate plus the window's width. More... | |
Size | Size [get, set] |
The size of the panel's drawing canvas in pixels. More... | |
string | Title [get, set] |
The title text that will appear in the window's top title bar; default "DrawingPanel". More... | |
int | Top [get, set] |
The y-coordinate of the top of the panel window. Equivalent to Y property. More... | |
bool | Visible [get, set] |
Whether the panel's window should be shown on the screen; default true. More... | |
int | Width [get, set] |
The height of the panel's canvas area in pixels. The panel's window will size itself to tightly fit a canvas of this width. More... | |
int | X [get, set] |
The x-coordinate of the left side of the panel window. Equivalent to Left property. More... | |
int | Y [get, set] |
The y-coordinate of the top of the panel window. Equivalent to Top property. More... | |
A simplified C# drawing window class based on DrawingPanel.java class that accompanies the Building Java Programs textbook.
author: Marty Stepp, Stanford University. version: 1.031, 2018/11/13 Some code written by or based on code by Alec McTavish, Bothell High School (thanks!).
The DrawingPanel class provides a simple interface for drawing persistent images using a Graphics object. An internal Bitmap object is used to keep track of what has been drawn. A client of the class simply constructs a DrawingPanel of a particular size and then draws on it with its Graphics object, setting the background color if they so choose.
The following is a short example program that uses DrawingPanel:
using System.Drawing;
using System.Drawing.Drawing2D;
public class SmileyFace
{
public static void Main()
{
DrawingPanel panel = new DrawingPanel(400, 300);
panel.Background = Brushes.Yellow;
Graphics g = panel.GetGraphics();
g.DrawEllipse(Pens.Red, 10, 20, 100, 100);
g.FillRectangle(Brushes.Blue, 50, 50, 70, 20);
panel.MainLoop();
}
}
|
inline |
Constructs a new panel of a default width and height.
|
inline |
Constructs a new panel of the given width and height.
width | panel's width in pixels |
height | panel's height in pixels |
|
inline |
Constructs a new panel of the given width and height, with its top/left coordinate at the given (x, y) pixel.
x | panel's left x coordinate |
y | panel's top y coordinate |
width | panel's width in pixels |
height | panel's height in pixels |
|
inline |
Constructs a new panel to display an image from the given file. The panel window will be sized to tightly fit that image.
filename | file path/name containing image file to display |
|
inline |
Fills the panel's canvas area with its background color, wiping out any shapes that have been previously drawn.
|
inline |
Closes the panel window, causing it to disappear from the screen.
|
inlinestatic |
Returns the alpha component of the given ARGB integer, which is stored in bits 24-31.
argb | the ARGB integer from which to extract alpha component |
|
inlinestatic |
Returns the blue component of the given RGB integer, which is stored in bits 0-7.
rgb | the RGB integer from which to extract blue component |
|
inline |
Returns the Graphics object for this panel's canvas. Note that certain DrawingPanel methods invalidate the Graphics object and switch to a new one, such as when the panel is resized; for that reason, it is not recommended to store the result of GetGraphics for long periods in your code. Equivalent to the Graphics property; kept in for compatibility with some Java-isms.
|
inlinestatic |
Returns the green component of the given RGB integer, which is stored in bits 8-15.
rgb | the RGB integer from which to extract green component |
|
inline |
Returns the colors of all pixels in the panel's canvas as a 2D array in [x, y] order. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixels.
|
inline |
Returns the color of the given pixel in the panel's canvas.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
|
inline |
Returns the colors of all pixels in the panel's canvas as a 2D array. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelArray.
|
inline |
Returns the colors of all pixels in the panel's canvas as a 2D array of RGB integers. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelsRgb.
|
inline |
Returns the colors of all pixels in the panel's canvas as a 2D array of RGB integers. The first index of the array is the x-coordinate, and the second index is the y-coordinate. So, for example, index [a, b] represents the RGB pixel data for the pixel at position (x=a, y=b). Equivalent to GetPixelsArgb.
|
inlinestatic |
Returns the red component of the given RGB integer, which is stored in bits 16-23.
rgb | the RGB integer from which to extract red component |
|
inline |
Hides the window from the screen so that it will not be shown. You can cause the window to reappear by calling Show().
|
inline |
Returns whether the given 0-based (x, y) pixel offset is within the bounds of this panel's drawing canvas area; in other words, if it is between (0, 0) and (width-1, height-1) inclusive.
x | x-coordinate of pixel to check |
y | y-coordinate of pixel to check |
|
inline |
Reads an image from a file and draws it onto this panel's canvas at (0, 0). Also resizes the canvas to be the size of the given image. Equivalent to LoadImage.
filename | file name/path of image to load |
|
inline |
Reads an image from a file and draws it onto this panel's canvas at (0, 0). Also resizes the canvas to be the size of the given image. Equivalent to Load.
filename | file name/path of image to load |
|
inline |
Shows the panel window on the screen and loops waiting until it closes.
|
inline |
Causes the program to pause until the Enter key is pressed. see also: MainLoop.
|
inline |
Causes the panel to redraw itself on the screen. You normally do not need to call this method, unless you have set the RepaintImmediately flag to false. Equivalent to Repaint.
|
inline |
Causes the panel to redraw itself on the screen. You normally do not need to call this method, unless you have set the RepaintImmediately flag to false. Equivalent to RefreshDisplay.
|
inline |
Resizes this panel's canvas to be the given size. The surrounding window will resize to tightly fit the canvas at its new size.
|
inline |
Saves the pixels of this panel's canvas to the default save file.
|
inline |
Saves the pixels of this panel's canvas to the given file.
filename | file name/path in which to save file |
|
inlinestatic |
Saves all active drawing panels into image files. Not meant to be called by students.
|
inline |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixels.
pixels | 2D array of pixels to display, in [x, y] dimension order |
|
inline |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels as RGB integers. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixels.
pixels | 2D array of pixel RGB integers to display, in [x, y] dimension order |
|
inline |
Sets the color of the pixel at the given x/y coordinate to be the given color.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
color | color to use for pixel |
|
inline |
Sets the color of the pixel at the given x/y coordinate to the given red, green, and blue values.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
r | red component to use for pixel from 0-255 |
g | green component to use for pixel from 0-255 |
b | blue component to use for pixel from 0-255 |
|
inline |
Sets the color of the pixel at the given x/y coordinate to the given alpha, red, green, and blue values.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
a | alpha component to use for pixel from 0-255 |
r | red component to use for pixel from 0-255 |
g | green component to use for pixel from 0-255 |
b | blue component to use for pixel from 0-255 |
|
inline |
Sets the color of the pixel at the given x/y coordinate to the color represented by the given RGB integer.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
rgb | RGB integer representing color to use for pixel |
|
inline |
Sets the color of the pixel at the given x/y coordinate to the color represented by the given ARGB integer.
x | x-coordinate of pixel |
y | y-coordinate of pixel |
argb | ARGB integer representing color to use for pixel |
|
inline |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixelArray.
pixels | 2D array of pixels to display, in [x, y] dimension order |
|
inline |
Sets the color of every pixel in the panel's canvas to the given array of [x, y] pixels as RGB integers. If the given 2D array's size is not the same as the size of this panel's canvas, this panel is resized to match the size of the array. Equivalent to SetPixelArray.
pixels | 2D array of pixel RGB integers to display, in [x, y] dimension order |
|
inlinestatic |
Sets the file to be used when saving graphical output for all DrawingPanels. Not meant to be called by students.
|
inline |
Causes the panel's window to display itself on the screen.
|
inline |
Causes the program to pause for 1/60 of a second. Useful for producing animations.
|
inline |
Causes the program to pause for the given number of milliseconds. Useful for producing animations.
|
inline |
Moves the panel's window to the back of the Z-ordering of windows on the screen.
|
inline |
Moves the panel's window to the front of the Z-ordering of windows on the screen.
|
inlinestatic |
Returns an RGB integer made from the given red, green, and blue components from 0-255. The returned integer is suitable for use with various RGB integer methods in this class such as SetPixel.
r | red component of pixel from 0-255 |
g | green component of pixel from 0-255 |
b | blue component of pixel from 0-255 |
|
inlinestatic |
Returns an RGB integer made from the given alpha, red, green, and blue components from 0-255. The returned integer is suitable for use with various RGB integer methods in this class such as SetPixel.
a | alpha component of pixel from 0-255 |
r | red component of pixel from 0-255 |
g | green component of pixel from 0-255 |
b | blue component of pixel from 0-255 |
|
inline |
Zooms the drawing panel in/out to the given factor. A zoom factor of 1, the default, indicates normal size. A zoom factor of 2 would indicate 200% size, and so on. The factor value passed should be at least 1; if not, 1 will be used.
NOTE: This method is not yet implemented.
zoomFactor | the zoom factor to use (1 or greater) |
|
getset |
Whether the panel performs smoothing on its pixels; default false.
|
getset |
The background color painted onto the panel's drawing canvas; default white. When this is set, it clears the panel and fills its canvas area with the new background color.
|
getset |
The y-coordinate of the bottom of the panel window, which is equal to its top y coordinate plus the window's height.
|
get |
The Windows Form representing this panel's window. In general you should not need to directly access the window, and can mostly just call methods on the DrawingPanel itself or its Graphics object; but this is provided as a convenience. Note that calling some methods and properties on this form directly (e.g. Top, Width, Size) could cause the form state to go out of sync with the DrawingPanel state; this is not recommended.
|
get |
The Graphics object that can be used to draw on the panel's canvas. Note that certain DrawingPanel methods invalidate the Graphics object and switch to a new one, such as when the panel is resized; for that reason, it is not recommended to store the returned Graphics property result for long periods in your code.
|
getset |
The height of the panel's canvas area in pixels. The panel's window will size itself to tightly fit a canvas of this height.
|
getset |
The x-coordinate of the left side of the panel window. Equivalent to X property.
|
getset |
The x/y location of the top-left corner of the panel window.
|
getset |
Whether the panel should automatically redraw itself after each drawing operation; default true. It can be useful to set this to false if you are about to perform a bunch of small drawing operations, such as setting the color of many individual pixels, and do not want the slowdown of repainting after each individual pixel changes color.
|
getset |
The x-coordinate of the right side of the panel window, which is equal to its left x coordinate plus the window's width.
|
getset |
The size of the panel's drawing canvas in pixels.
|
getset |
The title text that will appear in the window's top title bar; default "DrawingPanel".
|
getset |
The y-coordinate of the top of the panel window. Equivalent to Y property.
|
getset |
Whether the panel's window should be shown on the screen; default true.
|
getset |
The height of the panel's canvas area in pixels. The panel's window will size itself to tightly fit a canvas of this width.
|
getset |
The x-coordinate of the left side of the panel window. Equivalent to Left property.
|
getset |
The y-coordinate of the top of the panel window. Equivalent to Top property.