关于Component如何触发绘制

是在EventDispatchThread类中:

public void run() {
	try {
	    pumpEvents(new Conditional() {
		public boolean evaluate() {
		    return true;
		}
	    });	    
	} finally {
	    /*
	     * This synchronized block is to secure that the event dispatch 
	     * thread won't die in the middle of posting a new event to the
	     * associated event queue. It is important because we notify
	     * that the event dispatch thread is busy after posting a new event
	     * to its queue, so the EventQueue.dispatchThread reference must
	     * be valid at that point.
	     */
	    synchronized (theQueue) {
                if (theQueue.getDispatchThread() == this) {
                    theQueue.detachDispatchThread();
                }
                /*
                 * Event dispatch thread dies in case of an uncaught exception. 
                 * A new event dispatch thread for this queue will be started
                 * only if a new event is posted to it. In case if no more
                 * events are posted after this thread died all events that 
                 * currently are in the queue will never be dispatched.
                 */
                /*
                 * Fix for 4648733. Check both the associated java event
                 * queue and the PostEventQueue.
                 */
                if (theQueue.peekEvent() != null || 
                    !SunToolkit.isPostEventQueueEmpty()) { 
                    theQueue.initDispatchThread();
                }
		AWTAutoShutdown.getInstance().notifyThreadFree(this);
	    }
	}
    }

 

假设给这样一个测试类:

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;


class HelloWorldPanel extends JPanel {
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawString("Not a hello,World program", X, Y);
	}

	public static final int X = 75;
	public static final int Y = 100;
}

class HelloWorldFrame extends JFrame {
	public HelloWorldFrame() {
		setTitle("HelloWorld");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		HelloWorldPanel panel = new HelloWorldPanel();
		getContentPane().add(panel);
	}

	public static final int DEFAULT_WIDTH = 300;
	public static final int DEFAULT_HEIGHT = 300;
}

public class TestHelloWorld {
	public static void main(String args[]) {
		HelloWorldFrame frame = new HelloWorldFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WinForm是微软提供的一种用于创建Windows应用程序的UI开发框架,可以用C#或VB.NET编写。要在WinForm中绘制网格图片,可以使用Graphics对象和Paint事件。 首先,在WinForm的设计视图中创建一个PictureBox控件,该控件将用于显示网格图片。然后,在代码视图中为PictureBox控件的Paint事件编写处理程序。在处理程序中,可以使用Graphics对象的绘图方法绘制网格。 首先,创建一个Graphics对象,可以使用PictureBox控件的CreateGraphics方法来获取Graphics对象。然后,使用Graphics对象的绘图方法绘制网格。例如,可以使用Graphics对象的DrawLine方法绘制水平和垂直线条,并指定线条的起始点和结束点。 绘制水平线条时,可以使用PictureBox控件的Height属性作为线条的y坐标,然后使用循环语句绘制多条水平线条,间距为网格的大小。 绘制垂直线条时,可以使用PictureBox控件的Width属性作为线条的x坐标,然后使用循环语句绘制多条垂直线条,间距为网格的大小。 绘制完所有线条后,可以使用PictureBox控件的Invalidate方法强制重绘,以显示绘制的网格。 最后,在WinForm的Load事件中调用PictureBox控件的Invalidate方法,以触发Paint事件,开始绘制网格。 绘制网格图片的整个过程就是这样。通过使用PictureBox控件的Paint事件和Graphics对象的绘图方法,可以在WinForm中绘制出网格图片。 ### 回答2: 在WinForms中绘制网格图片是相对简单的任务。首先,我们需要创建一个新的WinForms项目,并在窗体中添加一个PictureBox控件。 我们可以在窗体的Paint事件中获取PictureBox控件的Graphics对象,然后使用Graphics对象的DrawLine方法来绘制网格。以下是一个示例代码: ``` private void pictureBox1_Paint(object sender, PaintEventArgs e) { // 获取PictureBox的Graphics对象 Graphics g = e.Graphics; // 定义网格线的间隔距离 int interval = 40; // 绘制垂直线 for (int x = interval; x < pictureBox1.Width; x += interval) { g.DrawLine(Pens.Black, x, 0, x, pictureBox1.Height); } // 绘制水平线 for (int y = interval; y < pictureBox1.Height; y += interval) { g.DrawLine(Pens.Black, 0, y, pictureBox1.Width, y); } } ``` 在这个示例中,我们使用一个间隔值来确定网格线的间距,并使用循环来绘制垂直和水平线。我们使用了Pens.Black来指定绘制线条的颜色。 在窗体加载时,我们还需要将PictureBox控件的SizeMode属性设置为StretchImage,以使网格图片在控件中自适应,代码如下: ``` public Form1() { InitializeComponent(); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } ``` 最后,我们可以在运行程序时看到网格图片出现在PictureBox控件中。我们可以根据需要调整间距值和线条颜色,以创建我们想要的网格样式。 希望这个简单的示例对你有所帮助! ### 回答3: 在WinFrom中绘制网格图片有多种方法。一种常见的方法是使用PictureBox控件和绘图函数来实现。 首先,在WinFrom窗体中添加一个PictureBox控件,并将其大小设置为所需的网格大小。然后,我们可以在PictureBox的Paint事件中进行绘图操作。 在绘图操作过程中,我们可以使用GDI+绘图函数来绘制网格线。例如,可以使用Graphics对象的DrawLine方法来绘制水平和垂直的网格线。 具体实现时,可以在Paint事件中创建Graphics对象,并设置网格线的颜色和粗细。然后,使用循环语句在PictureBox上绘制水平和垂直的网格线。例如,可以使用两层嵌套的循环分别绘制水平和垂直的网格线。 在循环中,可以使用DrawLine方法绘制水平的网格线,起始点的X坐标为0,Y坐标为当前循环变量的值,而结束点的X坐标为PictureBox的宽度,Y坐标同样为当前循环变量的值。 类似地,可以使用DrawLine方法绘制垂直的网格线,起始点的X坐标为当前循环变量的值,Y坐标为0,而结束点的X坐标同样为当前循环变量的值,Y坐标为PictureBox的高度。 最后,在主窗体的Load事件中调用PictureBox的Invalidate方法,以触发Paint事件,即可在PictureBox中绘制出网格图片。 总之,以上是一种在WinFrom中绘制网格图片的方法。当然,还有其他的实现方式,具体可以根据需求和个人喜好进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值