如何在Java中在框架的右侧显示图形。 [英] How to display graphics on the right side of a frame in Java.

查看:93
本文介绍了如何在Java中在框架的右侧显示图形。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个java程序,它将以某种模式显示4个黑色方块,如果调整大小,它们会与帧成比例缩放。我绘制了前两个正方形,但是我无法在框架的右侧或底部绘制正方形,同时在调整框架大小时仍然使它们缩放。任何帮助将不胜感激:



I''m working on a java program that will display 4 black squares in a certain pattern, and have them scale proportionally with the frame if it is resized. I have drawn the first two squares, however I am unable to draw squares on the right side or bottom of the frame while still making them scale when the frame is resized. Any help would be appreciated:

	public void paint(Graphics g)
	{
		g.setColor(Color.white);
		int w = getWidth();
		int h = getHeight();
		g.fillRect(0,0,w,h);

		Graphics2D g2 = (Graphics2D)g;

		g2.setColor(Color.black);
		Rectangle r1 = new Rectangle(0, h/3, w/3, h/3);
		g2.fill(r1);

		g2.setColor(Color.black);
		Rectangle r2 = new Rectangle(w/3, h/500, w/3, h/3);
		g2.fill(r2);

		g2.setColor(Color.black);
		Rectangle r3 = new Rectangle(w/2, h/3, w/3, h/3);
		g2.fill(r3);
/*
		g2.setColor(Color.black);
		Rectangle r4 = new Rectangle(0, h/2, w/3, h/2);
		g2.fill(r4);*/
	}


	// copied from the W2MouseEvents for convenience
	// (so we can run the program from Test2Panel class too!)
	public static void main(String[] args)
	{
		Test2Frame w = new Test2Frame();
		w.setVisible(true);
	}
}

推荐答案

如果我明白了,你想在窗口是时更新框架调整大小。

你必须使用ComponentListener接口


componentResized(ComponentEvent e)方法,你必须更新你的 h w 变量,然后调用重绘方法。



最佳制裁

Filipe
Hi, if I understood, you want to update the frame when the window is resized.
You must use the ComponentListener Interface

and in componentResized(ComponentEvent e) method, you must update yours h and w variables and then call repaint method.

Best reguards
Filipe


这篇关于如何在Java中在框架的右侧显示图形。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆