Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,


I want to draw X and Y axes on a panel. I want to use graphics class. Can anyone give an example for the same.


Thank You
Posted

Handle the Panel.Paint event and get the Graphics object from the PaintEventArgs parameter.
You can then use the Graphics.DrawLine method to draw your axes - it should be simple to create the two points you need for each line, using the Width and Height properties of the panel, offset by a small amount to leave a margin. You can if you need use the Graphics.DrawString method to label your axes.
 
Share this answer
 
Comments
Member 4354249 22-Oct-12 8:16am    
Can you give me the code related to it??
OriginalGriff 22-Oct-12 8:22am    
Oh come on!
It's two lines of code! (once you have created the event handler and hooked it up)
Hello,
here is simple Draw subroutine that do what you asked for, and can be used in any windows form app, it is necessary to have one Panel control on your form, and set background color of panel to White, and dock property to fill or dock to parent container.
If you analize code you shall learn some basics of drawing by using Graphics class.
Try reading MSDN about Graphics class to learn much more.

MSDN Graphics Class Link[^]

All the best,
Perić Željko

void Draw()
{
	//
	// Declaration of variables
	//
				
		Graphics table;
					
		Pen pencil;
					
		Brush brush;
						
		Font font;
					
		Color rubber;
					
		PointF start_line;
		PointF end_line;
					
		int panel_height;
		int panel_width;
					
	//
	//
	//
			
			
 //
 // Set new graphics that uses panel as drawing surface
 //
 table = panel1.CreateGraphics();
 //
 // Set new PointF(x,y),  start line point 
 // 
 start_line = new PointF();
 start_line.X = 0;
 start_line.Y = 0;
 //
 // Set new PointF(x,y) end line point
 // 
 end_line = new PointF();
 end_line.X = 0;
 end_line.Y = 0;
 //
 // Set new Pen, 
 // pencil that will be used for 
 // drawing with Red color and 2 pixels tick
 //
 pencil = new Pen(Color.Red,2);
 //
 // Set new Brush
 // brush that will be used for drawing letters
 //
 brush = new SolidBrush(Color.Black);
 //
 // Set new Font
 // font that shall be used for drawing strings
 //
 font = new Font("LucidaConsole",12,FontStyle.Bold);
 //
 // Set new Color, 
 // rubber that will be used for erasing table
 //
 rubber = new Color();
 rubber = Color.White;
 //
 // Get Panel height and width
 //
 panel_height = panel1.Height;
 panel_width = panel1.Width;
 //
 // Clear table surface with rubber, 
 // rubber contains value of collor 
 // and it must be the same as panel1 background color
 //
 table.Clear(rubber);
 //
 // Set start and end of X oordinate line
 //
 start_line.X = 0;
 start_line.Y = panel_height-20;
			
 end_line.X = panel_width;
 end_line.Y = start_line.Y;
 //
 // draw X oordinate
 //
 table.DrawLine(pencil,start_line,end_line);
 //
 // draw "x" letter at the end of oordinate
 //
 end_line.X-=20;// decrement x by 20
 table.DrawString("x",font,brush,end_line);
 //
 // Set start and end of Y oordinate line
 //
 start_line.X = 20;
 start_line.Y = 0;
			
 end_line.X = start_line.X;
 end_line.Y = panel_width;
 //
 // draw Y oordinate
 //
 table.DrawLine(pencil,start_line,end_line);
 //
 // draw "y" letter at the end of oordinate
 //
 table.DrawString("y",font,brush,start_line);
 //
 // arrows are up to you to draw it
 //
}
 
Share this answer
 
v3
Hello try this one,

C#
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;

namespace WindowsFormsApplication3
{


    public class ImagePanel : Panel
    {
        private Image image;
        private Panel panel1;

        public Image Image
        {
            get { return image; }
            set
            {

                image = value;
                Refresh();
            }
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            this.ResumeLayout(false);

        }
    }


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ImagePanel obj = new ImagePanel();

        private void Form1_Load(object sender, EventArgs e)
        {
            obj.Image = Image.FromFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\nike.jpg");
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (obj.Image != null)
            {
                e.Graphics.DrawImage(obj.Image, new Point(100,100));
            }
            base.OnPaint(e);
        }
    }
}


Otherwise use like this:

It sounds like you haven't hooked up the paint event of the panel:

C#
panelArea.Paint += new PaintEventHandler(panelArea_Paint);


If panelArea is the name of your form, then just change it to your panel:

C#
panel1.Paint += new PaintEventHandler(panel1_Paint);


and then move your painting logic to that method:

C#
private void panel1_Paint(object sender, PaintEventArgs e) {
  // the rest of your drawing
}






regards
sarva
 
Share this answer
 
v2
Comments
Member 4354249 22-Oct-12 8:15am    
Hello,

I want to draw axis. I have few points and i have to plot on the screen and plot the axis too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900