在JPanel的中心绘制形状 [英] draw a shape in center of JPanel
本文介绍了在JPanel的中心绘制形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了这段代码:
public void paintComponent(Graphics g){
超级 .paintComponent(g);
int width = getWidth()/2;
int height = getHeight()/2;
g.drawOval(width,height, 100 , 100 );
}
并使用JFrame add方法将JPanel添加到JFrame,
但椭圆形不会出现在Jpanel的中心吗?
解决方案
在计算时,您需要考虑椭圆的宽度和高度.因此,您的最高点计算如下:
int top =(getHeight()-椭圆高度)/ 2 ; // 宽度相同
hi
i want to draw a oval in the center of JPanel
i wrote this code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width=getWidth()/2;
int height=getHeight()/2;
g.drawOval(width, height, 100,100);
}
and added the JPanel to JFrame with JFrame add method,
but the oval doesn''t appear in center of Jpanel?
how can i draw a shape in center of panel?
解决方案
In your calculations you need to take the width and height of the oval into consideration. Thus your top point is calculated as:
int top = (getHeight() - ovalHeight) / 2; // same for width
这篇关于在JPanel的中心绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文