Hi, I have one class form and draw shape class. in fact I need to make the ball move around when i click Button or mouse. I design it but when i run it with the timer give me error message that the Object reference not set to an instance of an object as the photo shows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DRAWINGSHAPES
{
public partial class ShapeDrawing : Form
{
//variables declaration
private DrawShapesClass shapeobject;
private int SizeofShape;
private int XColOfShape;
private int YRowOfShape;
private float xSpeed_;
private float ySpeed_;
private string txtHex;
private string shapeselected;
private Bitmap previewbitmap;
private Color colorselected;
private bool DrawButtonClicked;
public ShapeDrawing()
{
InitializeComponent();
//call the "MouseClickDrawShape" method to get the mouse position and draw the shape on it
DrawShapesPicturebox.MouseClick += new System.Windows.Forms.MouseEventHandler(MouseClickDrawShape);
//initialize the picture box for preview
previewbitmap = new Bitmap(PreviewPicturebox.Width, PreviewPicturebox.Height);
}
private void DrawShapes()
{
//get the selected shape and store the value in the variable "shapeselected"
if (CircleRadioButton.Checked == true)
{
shapeselected = "circle";
}
else if (SquareRadioButton.Checked == true)
{ shapeselected = "square"; }
else if (TriangleRadioButton.Checked == true)
{ shapeselected = "triangle"; }
//get the selected color and store the value in the variable "colorselected"
if (RedRadioButton.Checked == true)
{ colorselected = ColorTranslator.FromHtml("#FF0000"); } //hexadecimal value for Red color
else if (GreenRadioButton.Checked == true)
{ colorselected = ColorTranslator.FromHtml("#00FF00"); } //hexadecimal value for Green color
else if (BlueRadioButton.Checked == true)
{ colorselected = ColorTranslator.FromHtml("#0000FF"); } //hexadecimal value for Blue color
//check whether the drawbutton is clicked or not; if yes, then execute the method for getting the random position for the shape
if (DrawButtonClicked == true)
{
DrawButtonClicked = false;
Random randompos = new Random();
//the random no. to get must not be smaller than the width of the picturebox
XColOfShape = randompos.Next(DrawShapesPicturebox.Width);
//the random no. to get must not be smaller than the height of the picturebox
YRowOfShape = randompos.Next(DrawShapesPicturebox.Height);
xSpeed_ = randompos.Next(DrawShapesPicturebox.Width);
ySpeed_ = randompos.Next(DrawShapesPicturebox.Height);
}
// use CreateGraphics() function for drawing the shapes in to the "DrawShapesPicturebox"
Graphics shapegraphics = DrawShapesPicturebox.CreateGraphics();
if (CircleRadioButton.Checked == true)
{
shapeselected = "circle";
Random rand = new Random();
shapeobject = new DrawShapesClass(DrawShapesPicturebox.DisplayRectangle, rand, 30, Color.AntiqueWhite);
}
shapeobject = new DrawShapesClass(XColOfShape , YRowOfShape , xSpeed_, ySpeed_, SizeofShape, shapeselected, colorselected);
shapeobject.Draw(shapegraphics);
DrawShapesPicturebox.Image = previewbitmap;
//use FromImage()function for saving and retrieving the image of the "DrawShapesPicturebox"; this will save to the bitmap variable or memory
Graphics bitmapgraphics = Graphics.FromImage(previewbitmap);
shapeobject.Draw(bitmapgraphics);
PreviewPicturebox.Image = previewbitmap;
}
private void MouseClickDrawShape(object sender, MouseEventArgs e)
{
//if (CircleRadioButton.Checked == true)
//{
// //shapeselected = "circle";
// // XColOfShape = e.X ; //for "Column" mouse cursor position
// // YRowOfShape = e.Y ; //for "Row" mouse cursor position
//}
//else if (SquareRadioButton.Checked == true)
//{
// shapeselected = "square";
// XColOfShape = e.X ; //for "Column" mouse cursor position
// YRowOfShape = e.Y ; //for "Row" mouse cursor position
// DrawShapes();
//}
//else
//{
// XColOfShape = e.X ; //for "Column" mouse cursor position
// YRowOfShape = e.Y ; //for "Row" mouse cursor position
// DrawShapes();
//}
}
private void ColorpaletteButton_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
// Store the color as a hex number
string HexColor = string.Format("0x{0:X8}", colorDialog1.Color.ToArgb());
// Strip the unnecessary characters at the end of the hex number
txtHex = "#" + HexColor.Substring(HexColor.Length - 6, 6);
colorselected = ColorTranslator.FromHtml(txtHex);
//initialize the Color Radio buttons to false
RedRadioButton.Checked = false;
GreenRadioButton.Checked = false;
BlueRadioButton.Checked = false;
}
}
private void DrawButton_Click(object sender, EventArgs e)
{
DrawButtonClicked = true;
DrawShapes();
}
private void SizetrackBar_Scroll(object sender, EventArgs e)
{
SizeofShape = SizetrackBar.Value; //size of the shape
}
private void RefreshMenuOption_Click(object sender, EventArgs e)
{
//refresh the graphics or bitmap memory of the pictureboxes
DrawShapesPicturebox.Refresh();
PreviewPicturebox.Image = null;
PreviewPicturebox.Invalidate(); //refresh() function cannot clear the bitmap memory
previewbitmap = new Bitmap(PreviewPicturebox.Width, PreviewPicturebox.Height); //inialize the preview picturebox
}
private void DrawMenuOption_Click(object sender, EventArgs e)
{
DrawButtonClicked = true;
DrawShapes();
}
private void ExitMenuOption_Click(object sender, EventArgs e)
{
this.Close();
}
private void DrawShapesPicturebox_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
// shapeselected = "circle";
shapeobject.Move(DrawShapesPicturebox.DisplayRectangle);
DrawShapesPicturebox.Refresh();
}
}
}
using System;
using System.Drawing;
namespace DRAWINGSHAPES
{
class DrawShapesClass
{
private float xcol;
private float yrow;
private float dxcol;
private float dyrow;
private int shapediameter_;
private string shapetype;
//private Brush brushtype; <- this is used for Fill Method
private Brush brushtype; //<- this is used for drawing line
public DrawShapesClass(float colparam, float rowparam,float dx, float dy, int diameterparam, string shapeparam, Color colorparam)
{
xcol = colparam;
yrow = rowparam;
this.dxcol = dx;
this.dyrow = dy;
shapetype = shapeparam;
shapediameter_ = diameterparam;
//brushtype = new SolidBrush(colorparam); //for FILL method
brushtype = new SolidBrush (colorparam); //for drawing lines
}
public void Draw(Graphics graphics)
{
switch (shapetype)
{
case "circle":
//graphics.FillEllipse(brushtype, xcol, yrow, shapediameter, shapediameter); //for solidbrush function
graphics.FillEllipse(brushtype, xcol -30, yrow-15, shapediameter_, shapediameter_); //for drawing line
break;
case "square":
//graphics.FillRectangle(brushtype, xcol, yrow, shapediameter, shapediameter); //for solidbrush function
graphics.FillRectangle(brushtype, xcol -30, yrow-15, shapediameter_, shapediameter_); //for drawing line
break;
case "triangle":
{
//graphics.FillPolygon(brushtype, new Point[] //for solidbrush function and DrawPolygon function is for drawing lines
graphics.FillPolygon(brushtype, new Point[] { new Point((int)xcol, (int)yrow), new Point((int)xcol - shapediameter_, (int)yrow + shapediameter_), new Point((int)xcol + shapediameter_, (int)yrow + shapediameter_) });
}
break;
}
}
public DrawShapesClass(Rectangle bounds, Random rand, int shapeDiameter, Color color)
{
xcol = rand.Next(0, bounds.Width - shapediameter_);
yrow = rand.Next(0, bounds.Height - shapediameter_);
dxcol = Convert.ToSingle(0.5 + 1.5 * rand.NextDouble());
if (rand.Next(2) == 1)
{
{
dxcol = -dxcol;
}
}
dyrow = Convert.ToSingle(0.5 + 1.5 * rand.NextDouble());
if (rand.Next(2) == 1)
{
{
dyrow = -dyrow;
}
}
shapediameter_ = shapeDiameter;
brushtype = new SolidBrush(color);
}
public void Move(Rectangle bounds)
{
Move();
Bounce(bounds);
}
private void Move()
{
xcol += dxcol;
yrow += dyrow;
}
private void Bounce(Rectangle bounds)
{
if (xcol <= 0)
{
dxcol = Math.Abs(dxcol);
xcol = bounds.Left;
}
else if (xcol + shapediameter_ >= bounds.Width)
{
dxcol = -Math.Abs(dxcol);
xcol = bounds.Right - shapediameter_;
}
if (yrow <= 0)
{
dyrow = Math.Abs(dyrow);
yrow = bounds.Top;
}
else if (yrow + shapediameter_ >= bounds.Height)
{
dyrow = -Math.Abs(dyrow);
yrow = bounds.Bottom - shapediameter_;
}
}
public float X
{
get
{
return xcol;
}
set
{
xcol = value;
}
}
public float Y
{
get
{
return yrow;
}
set
{
yrow = value;
}
}
public Point Position // create the
{
get
{
// retrive and read
int x = Convert.ToInt32(xcol);
int y = Convert.ToInt32(yrow);
return new Point(x, y); // plan
}
set
{
xcol = value.X;
yrow = value.Y;
}
}
}
}