你如何使用变量绘制C#?
我设法绘制了一些形状,但只有当我硬编码的长度。我需要使用轨迹条绘制形状以获得长度。
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);
仅显示方形类,但此项目还包括三角形和方形。
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;
}
}
这是包含所有事件处理程序的类
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)
{
}
}
}
是的,它非常凌乱,你可以看到香港专业教育学院尝试了各种各样的东西。
panel1_paint和onPaint之间的区别是什么? 你可以看到我不太确定如何使用eventhandler,onCircleClick基本上是一个菜单项按钮,但我如何从另一个eventhandler(onCircleClick)激活一个不同的eveenthandler(panel1_Paint)?
是否需要在* _paint / OnPaint方法中绘制图形?我已经得到了我的正常面板。
接下来是将轨迹栏值赋予形状对象并再次返回方法的最佳操作方法?是的,数据被保存(我认为)当我使用displayMessage(shape.getLength)它显示长度,通常是一个。
在java中为c#重新编写equilent()吗?香港专业教育学院尝试过.Refresh();但它不起作用它会绘制形状然后使它消失。
我正在写我的二传手/吸气手吗?或者我应该使用public int X
{
get {return x;}
set {x = value;}
}
在java中,图形会在任何面板上绘制,在c#中它是否需要在特定的容器中?
答案 0 :(得分:0)
这非常简单,可以说您想在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);
}
正文中编写该代码。
panel2.Refresh()
,每当您要刷新图形时,都可以调用panel2.Invalidate()
或panel2
。两者都会做。
请注意,如果您这样做, <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
</head>
<body>
<button onClick="load()">load</button>
<button onClick="check()">check</button>
</body>
<script>
function load() {
var scriptElement = document.createElement("script");
scriptElement.onload = function () {
console.log("Successfully loaded script 2 using (onload).");
};
scriptElement.src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js";
document.head.appendChild(scriptElement);
}
function check() {
console.log(angular.element == jQuery) // false
}
</script>
</html>
在绘制形状之前不会像以前一样被清除。
还请注意,更改轨迹栏值时,面板将闪烁。我知道如何处理,但是我现在不想使解决方案复杂化。