简体   繁体   中英

How to draw using trackbar?

well how do you draw in C# using variables?

ive managed to draw some shapes but only when i hardcode in the lengths. i need to draw shapes using a trackbar to get the lengths.

public abstract class Shape
{
    //private String shape;
    private int length;
}

public virtual void setLength(int newLength)
{
    this.length = newLength;
}

public virtual int getLength()
{
    return length;
}

//public String getShape()
//{
//    return shape;
//}

//abstract public double getLength(float length);

abstract public float getPerimeter(int length);

abstract public float getArea(int length);

only showing square class but this project also includes triangle and square.

using System;
using System.Drawing;

public class Square : Shape
{
    private float perimeter, area;

    public override float getPerimeter(int length)
    {
        perimeter = length*4;
        return perimeter;
    }

    public override float getArea(int length)
    {
        area = length*length;
        return area;
    }
}

this is the class with all my event handlers

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 shapes
{
    //private System.Windows.Forms.TrackBar trackBar1;

    public partial class Form1 : Form
    {        
        private Shape shape;
        private int length = 0;
        private int shapeL = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label3.Text = "Length Slider: " + trackBar1.Value;

            textBox1.Text = shape.getPerimeter(shape.getLength()).ToString("0.00");
            textBox2.Text = shape.getArea(shape.getLength()).ToString("0.00");

            textBox1.Refresh();
            textBox2.Refresh();

            length = trackBar1.Value;
            shape.setLength(length);
        }

        private void onCircleClick(object sender, EventArgs e)
        {
            shape = new Circle();
            //length = trackBar1.Value;
            length = shape.getLength();
            this.Refresh();

            using (Graphics g = this.panel1.CreateGraphics())
            {
                Pen pen = new Pen(Color.Black, 2);
                Graphics formGraphics;
                formGraphics = this.panel1.CreateGraphics();
                formGraphics.DrawEllipse(pen, 50, 50, length, length);
                //g.DrawEllipse(pen, 100, 100, length, length);

            }
        }

        private void onSquareClick(object sender, EventArgs e)
        {
            shape = new Square();
            length = trackBar1.Value;


            using (Graphics g = this.panel1.CreateGraphics())
            {
                Pen pen = new Pen(Color.Black, 2);

                g.DrawRectangle(pen, 50, 50, length, length);

                System.Windows.Forms.MessageBox.Show("lenght is: " + length);
            }
        }

        private void onTriangleClick(object sender, EventArgs e)
        {
            shape = new Triangle();
            length = trackBar1.Value;

            using (Graphics g = this.panel1.CreateGraphics())
            {
                SolidBrush blueBrush = new SolidBrush(Color.Blue);

                // Create points that define polygon.
                Point point1 = new Point(50, 50);
                Point point2 = new Point(50, 100);
                Point point3 = new Point(100, 50);
                Point[] curvePoints = { point1, point2, point3};

                // Draw polygon to screen.
                g.FillPolygon(blueBrush, curvePoints);
            }
        }

        private void shapeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            Pen pen = new Pen(Color.Black, 2);

            Graphics g = pe.Graphics;
            g = this.CreateGraphics();
            g.DrawRectangle(pen, 50, 50, length, length);
        }

        private void OnPaint(object sender, PaintEventArgs e)
        {

        }
    }
}

yes its very messy, as you can see ive tried various things.

whats the difference between the panel1_paint and onPaint? as you can see im not too sure how to use eventhandlers, the onCircleClick is basically a menu item button but how do i activate a different eveenthandler(panel1_Paint) from another eventhandler(onCircleClick)?

do graphics need to drawn in a *_paint/OnPaint method? ive gotten mine to draw in just the normal panel.

next is whats the best course of action to get the trackbar value to the shape object and back again to the method? yes the data is being saved (i think) when i use displayMessage(shape.getLength) it displays the length and is usually one off.

whats the equilent to repaint() in java for c#? ive tried this.Refresh(); but it doesnt work itll draw the shape then makes it disappear.

am i writing my setters/getters properly? or should i use

public int X
{
    get {return x;}
    set {x = value;}
}

in java, graphics will draw on any panel, in c# does it need to be in a specific container?

this is very simple, lets say that you want to draw on panel2 . all you have to do is to write this inside your private void panel2_Paint(object sender, PaintEventArgs e) body.

    {
        e.Graphics.Clear(panel1.BackgroundColor);
        int length = trackBar1.Value;
        Pen pen = new Pen(Color.Black, 2);
        e.Graphics.DrawRectangle(pen, 50, 50, length, length);
    }

and whenever you want to refresh the drawing, you can call either panel2.Refresh() or panel2.Invalidate() . both would do the job.

  • note that if you did this, panel2 won't get cleared after drawing the shape as it happened with you before.

  • note also that the panel would flicker as you change the trackbar value. i know how to handle this, but i don't want to complicate the solution for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM