Moving Smiling Face C# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Below I've created a program using C# that creates a smiley face. It also moves across the screen. I cannot figure out how to get the smiley face to bounce off the edges and around the screen. Please Help. Thank you.
*/using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HappyFace
{
public partial class HappyFace : Form
{
int xpos = 0;
int ypos = 0;
int width = 0;
int length = 0;
int startAngle = 45;
int sweepAngle = 90;
public HappyFace()
{
InitializeComponent();
}
private void HappyFace_Load(object sender, EventArgs e)
{
}
private void HappyFace_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(Brushes.Red, 7);
Pen myPen2 = new Pen(Brushes.Green, 7);
//g.DrawLine(myPen, 0, 0, 500, 500);
//g.DrawLine(myPen, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
//g.DrawLine(myPen2, 0, this.ClientRectangle.Height, this.ClientRectangle.Width, 0);
//g.DrawLine(myPen2, this.ClientRectangle.Left, this.ClientRectangle.Bottom, this.ClientRectangle.Right, ClientRectangle.Top);
int endX = this.ClientRectangle.Width;
int endY = this.ClientRectangle.Height;
//string msg = String.Format("endX = {0} endY = {1}", endX, endY);
//MessageBox.Show(msg);
int xCenter = this.ClientRectangle.Left + (this.ClientRectangle.Width / 2);
int yCenter = this.ClientRectangle.Top + (this.ClientRectangle.Height / 2);
Pen circlePen = new Pen(Brushes.Black, 9);
//g.DrawEllipse(circlePen, xCenter - 50, yCenter - 50, 100, 100);
// g.FillEllipse(Brushes.Orange, xCenter -50, yCenter - 50, 100, 100);
Font myFont = new Font("Monotype Corsiva", 43, FontStyle.Bold);
g.DrawString("Happy Face", myFont, Brushes.Aqua, 300, 25);
//g.DrawArc(circlePen, xpos, width, length, startAngle, sweepAngle);
g.DrawEllipse(circlePen, xpos, ypos + 130, 250, 250);
g.FillEllipse(Brushes.PeachPuff, xpos, ypos + 130, 250, 250);
g.DrawEllipse(circlePen, xpos + 65, ypos + 200, 20, 35);
g.FillEllipse(Brushes.Black, xpos + 65, ypos + 200, 20, 35);
g.DrawEllipse(circlePen, xpos + 160, ypos + 200, 20, 35);
g.FillEllipse(Brushes.Black, xpos + 160, ypos + 200, 20, 35);
g.DrawArc(circlePen, xpos + 60, ypos + 215, 130, 120, 35, 115);
}
private void timer1_Tick(object sender, EventArgs e)
{
xpos = xpos + 3;
if(xpos >= this.ClientRectangle.Right - 250)
{
xpos = 0;
}
this.Invalidate();
}
}
}*/

Well, I was a bit bored. I'll assume that the object is going to move in a 45 degrees trajectory,and that when it collides with the bounds it would change by 90ยบ.
What I would do (this is a very simple solution) is, first of all, define the direction in both axes in which i want the "smiley" to move,the step in each timer tick, the position of the center and the size of the object, something like:
int xpos = 0;
int ypos = 130;
int step = 10;
int width = 250;
int height = 250;
int directionX = +1;
int directionY = -1;
The timer would just increase the x and y positions:
private void timer1_Tick(object sender, EventArgs e)
{
xpos += 10*directionX;
ypos += 10*directionY;
checkBounds(); //This would check if the object collides with the bounds
this.Invalidate();
}
The checkBounds method check if the object collides with the bounds:
private void checkBounds()
{
if (ypos < 0 + step || ypos + height+ step > ClientRectangle.Height)
{
directionY *= -1;
}
if (xpos < 0 + step || xpos + width + step > ClientRectangle.Width)
{
directionX *= -1;
}
}
Finally, the Paint method is similar to yours, just adjusting some values:
private void Form2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(Brushes.Red, 7);
Pen myPen2 = new Pen(Brushes.Green, 7);
int endX = this.ClientRectangle.Width;
int endY = this.ClientRectangle.Height;
int xCenter = this.ClientRectangle.Left + (this.ClientRectangle.Width / 2);
int yCenter = this.ClientRectangle.Top + (this.ClientRectangle.Height / 2);
Pen circlePen = new Pen(Brushes.Black, 9);
Font myFont = new Font("Monotype Corsiva", 43, FontStyle.Bold);
g.DrawString("Happy Face", myFont, Brushes.Aqua, 300, 25);
g.DrawEllipse(circlePen, xpos, ypos, 250, 250);
g.FillEllipse(Brushes.PeachPuff, xpos, ypos, 250, 250);
g.DrawEllipse(circlePen, xpos + 65, ypos -130 + 200, 20, 35);
g.FillEllipse(Brushes.Black, xpos + 65, ypos-130 + 200, 20, 35);
g.DrawEllipse(circlePen, xpos + 160, ypos-130 + 200, 20, 35);
g.FillEllipse(Brushes.Black, xpos + 160, ypos-130 + 200, 20, 35);
g.DrawArc(circlePen, xpos + 60, ypos-130 + 215, 130, 120, 35, 115);
}
This code could be highly improved, but this may help you think how it should be done. Hope it helps.

Related

Print Preview making Issue in C#

when I click on print preview button. it shows my listed items. but when I close print preview screen and add some more items and click on print preview. it shows only that items which I enter later. I clear my old added Items.
here is my code which I'm trying
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Date: " + DateTime.Now.ToShortDateString(), new Font("Calibri", 11, FontStyle.Regular), Brushes.Black, new Point(300, 150));
e.Graphics.DrawString("Customer Name: " + customername.Text.Trim(), new Font("Calibri", 11, FontStyle.Regular), Brushes.Black, new Point(20, 150));
int yPos = 200;
int xPos = 20;
int brandyPos = 180;
for (int i = numberOfItemsPrintedSoFar; i < beltlist.Count; i++)
{
numberOfItemsPerPage++;
if (numberOfItemsPerPage <= 15 && count <= 3)
{
numberOfItemsPrintedSoFar++;
if (numberOfItemsPrintedSoFar <= beltlist.Count)
{
e.Graphics.DrawString(beltlist[i].BrandName, new Font("Calibri", 11, FontStyle.Regular), Brushes.Black, new Point(xPos, brandyPos));
brandyPos = yPos + 20;
e.Graphics.DrawString(beltlist[i].BeltSize.ToString() + "--" + beltlist[i].QTY.ToString(), new Font("Calibri", 11, FontStyle.Regular), Brushes.Black, new Point(20, yPos));
yPos += 20;
}
else
{
e.HasMorePages = false;
}
}
else
{
count++;
yPos = 180;
xPos += 150;
numberOfItemsPerPage = 0;
if (count > 3)
{
e.HasMorePages = true;
xPos = 20;
count = 1;
return;
}
}
}
}

add multiple pages in C#

I'm writing a program in C# which takes information of items from Listview and print it based on the quantity of these items. My problem is that I would like to print a document in multiple pages in c#.
Here is my code:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
//frame
e.Graphics.DrawRectangle(Pens.Black, 30, 30, 757, 1080);
//rows
e.Graphics.DrawLine(Pens.Black, 30, 184, 787, 184);
e.Graphics.DrawLine(Pens.Black, 30, 338, 787, 338);
e.Graphics.DrawLine(Pens.Black, 30, 492, 787, 492);
e.Graphics.DrawLine(Pens.Black, 30, 646, 787, 646);
e.Graphics.DrawLine(Pens.Black, 30, 800, 787, 800);
e.Graphics.DrawLine(Pens.Black, 30, 955, 787, 955);
//columns
e.Graphics.DrawLine(Pens.Black, 282, 30, 282, 1110);
e.Graphics.DrawLine(Pens.Black, 534, 30, 534, 1110);
int rows_loc = 35;
int cols_loc = 70;
int lable_index = 0;
foreach (ListViewItem item in listView1.Items)
{
for (int quantity = 1; quantity < (Convert.ToInt32(item.SubItems[1].Text) + 1); quantity++)
{
Font font = new System.Drawing.Font("Arial", 16);
SolidBrush black = new SolidBrush(Color.Black);
Zen.Barcode.Code128BarcodeDraw barcode1 = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
Image barcode_image = barcode1.Draw(item.SubItems[2].Text, 20);
e.Graphics.DrawImage(barcode_image, 40f + cols_loc, 29f + rows_loc);
e.Graphics.DrawString(item.Text, font, black, 60f + cols_loc, 0f + rows_loc);
lable_index++;
row_index = (int)(lable_index / 3);
col_index = lable_index % 3;
rows_loc = 35 + ((row_index) * 155);
cols_loc = 70 + (250 * col_index);
if (rows_loc >= e.PageSettings.PrintableArea.Height)
{
e.HasMorePages = true;
rows_loc = 35;
return;
}
else
{
e.HasMorePages = false;
}
}
}
}
But got the page looks like the figure below with infinite number of pages! I would be very appreciated if someone can help.
Thanks

Using ReadLine input for a different method

I'm trying to write a program that, given my choice of variables, depicts a smiley on screen.
I've written the following.
Now my program asks three questions, which I will answer with numbers between 0 and 11. I would like to use these answers in the method 'tekenscherm'. How can I call these variables in that method?
class HalloForm : Form
{
public string a,b,c = ""; //Declare them here.
public HalloForm()
{
this.Text = "Hallo";
this.BackColor = Color.Yellow;
this.Size = new Size(800, 600);
this.Paint += this.tekenScherm;
}
public static void Main()
{
System.Console.WriteLine("Smiley size, on a scale of 1 tot 10?");
string a = System.Console.ReadLine();
Int32.Parse(a);
System.Console.WriteLine("X coordinate of the smiley, on a scale of 1 to 10");
string b = System.Console.ReadLine();
Int32.Parse(b);
System.Console.WriteLine("Y coordinate of the smiley, on a scale of 1 to 10");
string c = System.Console.ReadLine();
Int32.Parse(c);
HalloForm scherm;
scherm = new HalloForm();
Application.Run(scherm);
}
void tekenScherm(object obj, PaintEventArgs pea)
{
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Pen blackBrush = new Pen(Color.Black, 5);
int x = 360;
int y = x + 75;
pea.Graphics.FillEllipse(blueBrush, 300, 200, 200, 200);
pea.Graphics.DrawEllipse(blackBrush, 300, 200, 200, 200);
pea.Graphics.DrawArc(blackBrush, 350, 250, 100, 100, 45, 90);
pea.Graphics.DrawEllipse(blackBrush, a, 250, 5, 5); //I've used it here
pea.Graphics.DrawEllipse(blackBrush, y, 250, 5, 5);
}
}
Declare those variables outside of the method scope as integers.
Int32.Parse returns an integer value, which you are not assigning to those variables, so it's basically doing nothing the way you are using it.
Here is the working code:
class HalloForm : Form
{
public int a, b, c = 0; //Declare them here.
public HalloForm()
{
this.Text = "Hallo";
this.BackColor = Color.Yellow;
this.Size = new Size(800, 600);
this.Paint += this.tekenScherm;
}
public static void Main()
{
System.Console.WriteLine("Smiley size, on a scale of 1 tot 10?");
a = Int32.Parse(System.Console.ReadLine());
System.Console.WriteLine("X coordinate of the smiley, on a scale of 1 to 10");
b = Int32.Parse(System.Console.ReadLine());
System.Console.WriteLine("Y coordinate of the smiley, on a scale of 1 to 10");
c = Int32.Parse(System.Console.ReadLine());
HalloForm scherm;
scherm = new HalloForm();
Application.Run(scherm);
}
void tekenScherm(object obj, PaintEventArgs pea)
{
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Pen blackBrush = new Pen(Color.Black, 5);
int x = 360;
int y = x + 75;
pea.Graphics.FillEllipse(blueBrush, 300, 200, 200, 200);
pea.Graphics.DrawEllipse(blackBrush, 300, 200, 200, 200);
pea.Graphics.DrawArc(blackBrush, 350, 250, 100, 100, 45, 90);
pea.Graphics.DrawEllipse(blackBrush, x, 250, 5, 5);
pea.Graphics.DrawEllipse(blackBrush, y, 250, 5, 5);
}
}

need to draw an Google marker image via C# Graphics

I need to draw an Google marker image via C# Graphics
for that i;ll need the exact coordinates of Google marker
how can i do that Please help .
My Current Code is .
private void Shape8(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int startMarker = 0;
int MarkerDiameter = 30;
int EllipseDiameter = 15;
const int StartX = 150;
const int StartY = 350;
int X = StartX;
int Y = StartY;
GraphicsPath path = new GraphicsPath();
Rectangle ellipse = new Rectangle(X, Y, EllipseDiameter, EllipseDiameter);
path.AddEllipse(ellipse);
path.CloseFigure();
X = X + (EllipseDiameter / 2) - (MarkerDiameter / 2);
Y = Y - (EllipseDiameter * 2) - (MarkerDiameter / 2);
startMarker = StartY - 10 - (Convert.ToInt32(MarkerDiameter * 1.5));//Space Between marker and circle.
AddMarker8(ref path, X, startMarker, StartX, StartY, EllipseDiameter, MarkerDiameter, "Concept1");
e.Graphics.FillPath(Brushes.Pink, path);
e.Graphics.DrawPath(Pens.Black, path);
e.Graphics.FillEllipse(Brushes.White, ellipse);
path.CloseAllFigures();
}
private void AddMarker8(ref GraphicsPath path, int X, int Y, int StartX, int StartY, int EllipseDiameter, int MarkerDiameter, string conceptName)
{
float startAngle = 180.0F;
float sweepAngle = 180.0F;
int fontStyle = (int)FontStyle.Regular;
path.AddString(conceptName, new FontFamily("Arial"), fontStyle, 15, new Point(X + (MarkerDiameter / 2), Y - 15), lblFormat);
Rectangle rect = new Rectangle(X, Y, MarkerDiameter, MarkerDiameter);
path.AddArc(rect, startAngle, sweepAngle);
Point lastpoint = new Point(X + (MarkerDiameter / 2), (Y + (MarkerDiameter * 2)) - (MarkerDiameter / 2));
path.AddCurve(new Point[] {
new Point(X+(MarkerDiameter), Y + (MarkerDiameter / 2)), //a
new Point((X +MarkerDiameter)-(MarkerDiameter/4) ,(Y +(MarkerDiameter ))) ,//A.5
lastpoint ,//c
new Point(X +(MarkerDiameter / 4) ,(Y +(MarkerDiameter ))) ,//B.5
new Point(X, Y + (MarkerDiameter / 2))//b
}, 0.5f);
path.CloseFigure();
}
Try the following code
private void DrawMarker(int X, int Y, double Scale, Color BorderColor, Color FillColor, PaintEventArgs e, char Letter = '\0')
{
Graphics G1 = e.Graphics;
Pen Pen1 = new Pen(BorderColor, 8);
SolidBrush Brush1 = new SolidBrush(BorderColor);
SolidBrush Brush2 = new SolidBrush(FillColor);
G1.SmoothingMode = SmoothingMode.AntiAlias;
G1.ResetTransform();
G1.ScaleTransform(Convert.ToSingle(0.4 * Scale), Convert.ToSingle(0.4 * Scale));
GraphicsPath GraphicsPath1 = new GraphicsPath();
GraphicsPath1.AddBeziers(new Point(X + 51, Y + 169), new Point(X + 47, Y + 137), new Point(X + 42, Y + 107), new Point(X + 14, Y + 77));
GraphicsPath1.AddBeziers(new Point(X + 87, Y + 77), new Point(X + 62, Y + 107), new Point(X + 55, Y + 137), new Point(X + 51, Y + 169));
GraphicsPath GraphicsPath2 = new GraphicsPath();
GraphicsPath2.AddEllipse(X + 5, Y + 5, 92, 92);
G1.FillPath(Brush2, GraphicsPath2);
G1.DrawPath(Pen1, GraphicsPath2);
G1.SetClip(new Rectangle(X, Y + 85, 103, 84));
G1.FillPath(Brush2, GraphicsPath1);
G1.DrawPath(Pen1, GraphicsPath1);
G1.ResetClip();
if (Letter != '\0')
{
Font Font1 = new Font("Arial", 52, FontStyle.Bold);
StringFormat StringFormat1 = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
G1.DrawString(Convert.ToString(Letter), Font1, Brush1, new Rectangle(X, Y, 103, 103), StringFormat1);
Font1.Dispose();
StringFormat1.Dispose();
}
else
{
G1.FillEllipse(Brush1, new Rectangle(X + 32, Y + 32, 37, 37));
}
Pen1.Dispose();
Brush1.Dispose();
Brush2.Dispose();
GraphicsPath1.Dispose();
GraphicsPath2.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawMarker(0, 0, 0.5, Color.Black, Color.Tomato, e, Convert.ToChar("A"));
DrawMarker(80, 0, 0.8, Color.Black, Color.Red, e, Convert.ToChar("B"));
DrawMarker(160, 0, 1, Color.Black, Color.Linen, e, Convert.ToChar("C"));
DrawMarker(210, 0, 1.3, Color.Black, Color.Orange, e, Convert.ToChar("D"));
DrawMarker(260, 0, 1.6, Color.Black, Color.Green, e);
}
after running it, the result should be like this

Using a matrix to rotate rectangles individually

Having a bit of a drawing complication you would call it. My math is a bit rusty when it comes to Matrices and drawing rotations on shapes. Here is a bit of code:
private void Form1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
DoRotation(e);
g.DrawRectangle(new Pen(Color.Black), r1);
g.DrawRectangle(new Pen(Color.Black), r2);
// draw a line (PEN, CenterOfObject(X, Y), endpoint(X,Y) )
g.DrawLine(new Pen(Color.Black), new Point((r1.X + 50), (r1.Y + 75)), new Point((/*r1.X + */50), (/*r1.Y - */25)));
this.lblPoint.Text = "X-pos: " + r1.X + " Y-pos: " + r1.Y;
//this.Invalidate();
}
public void DoRotation(PaintEventArgs e)
{
// move the rotation point to the center of object
e.Graphics.TranslateTransform((r1.X + 50), (r1.Y + 75));
//rotate
e.Graphics.RotateTransform((float)rotAngle);
//move back to the top left corner of the object
e.Graphics.TranslateTransform(-(r1.X + 50), -(r1.Y + 75));
}
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
case Keys.T:
rotAngle += 1.0f;
}
when I rotate (what I think should be r1) both r1 and r2 rotate. I need to be able to rotate each shape individually as I add more shapes.
I would use a function similar to this:
public void RotateRectangle(Graphics g, Rectangle r, float angle) {
using (Matrix m = new Matrix()) {
m.RotateAt(angle, new PointF(r.Left + (r.Width / 2),
r.Top + (r.Height / 2)));
g.Transform = m;
g.DrawRectangle(Pens.Black, r);
g.ResetTransform();
}
}
It uses a matrix to perform the rotation at a certain point, which should be the middle of each rectangle.
Then in your paint method, use it to draw your rectangles:
g.SmoothingMode = SmoothingMode.HighQuality;
//g.DrawRectangle(new Pen(Color.Black), r1);
//DoRotation(e);
//g.DrawRectangle(new Pen(Color.Black), r2);
RotateRectangle(g, r1, 45);
RotateRectangle(g, r2, 65);
Also, here is the line to connect the two rectangles:
g.DrawLine(Pens.Black, new Point(r1.Left + r1.Width / 2, r1.Top + r1.Height / 2),
new Point(r2.Left + r2.Width / 2, r2.Top + r2.Height / 2));
Using these settings:
private Rectangle r1 = new Rectangle(100, 60, 32, 32);
private Rectangle r2 = new Rectangle(160, 100, 32, 32);
resulted in:

Categories

Resources