62,621
社区成员
发帖
与我相关
我的任务
分享
package Test_Rectangle2D;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
public class cla_Rectangle2D
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
DrawFrame frame = new DrawFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class DrawFrame extends JFrame
{
private static class Coord_And_Size
{
/**
* 矩形坐标:左上角坐标
*/
int r_pos_x ;
int r_pos_y ;
/**
* 矩形大小:长、宽
*/
int r_size_width ;
int r_size_height ;
};
private static Coord_And_Size aim = new Coord_And_Size();
/**
* 设置默认图形坐标和长宽比例
*/
private void DefaultCoordAndSize()
{
aim.r_pos_x = 100 ;
aim.r_pos_y = 100 ;
aim.r_size_height = 250 ;
aim.r_size_width = 250 ;
}
/**
* 内容窗格容器对象
*/
Container con = this.getContentPane();
public DrawFrame()
{
DefaultCoordAndSize();
// 以下这句没用,是我出问题后抱着试试的态度写的,这句可以删去
// con.paintComponents(getGraphics());
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D)g ;
Rectangle2D rect = new Rectangle2D.Double(
aim.r_pos_x, aim.r_pos_y, aim.r_size_width, aim.r_size_height);
g2.draw(rect);
}
}





