简体   繁体   English

如何使用轨迹栏绘制?

[英]How to draw using trackbar?

well how do you draw in C# using variables?那么你如何使用变量在 C# 中绘制?

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? panel1_paint 和 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)?正如您所看到的,我不太确定如何使用事件处理程序,onCircleClick 基本上是一个菜单项按钮,但是我如何从另一个事件处理程序(onCircleClick)激活不同的事件处理程序(panel1_Paint)?

do graphics need to drawn in a *_paint/OnPaint method?图形需要在 *_paint/OnPaint 方法中绘制吗? 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.是的,当我使用 displayMessage(shape.getLength) 时,正在保存数据(我认为)它显示长度并且通常是一个。

whats the equilent to repaint() in java for c#? c# 中 java 中 repaint() 的等效性是什么? ive tried this.Refresh();我试过 this.Refresh(); but it doesnt work itll draw the shape then makes it disappear.但它不起作用它会绘制形状然后使其消失。

am i writing my setters/getters properly?我是否正确编写了我的 setter/getter? 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?在java中,图形可以在任何面板上绘制,在c#中是否需要在特定的容器中?

this is very simple, lets say that you want to draw on panel2 .这很简单,假设您想在panel2panel2 all you have to do is to write this inside your private void panel2_Paint(object sender, PaintEventArgs e) body.您所要做的就是将其写入您的private void panel2_Paint(object sender, PaintEventArgs e)主体中。

    {
        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() .每当您想刷新绘图时,您都可以调用panel2.Refresh()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.请注意,如果您这样做,则在绘制形状后将不会像之前那样清除panel2

  • 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.我知道如何处理这个问题,但我现在不想使解决方案复杂化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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