第四章图形编程内容摘要:

MESSAGE_Y)。 } public static final int MESSAGE_X = 75。 public static final int MESSAGE_Y = 100。 } 常用 API Container getContentPane() 返回 JFrame的内容窗格 void repain() 重新绘制组件 void repaint(int x, int y,intwidth,int height) 重新绘制组件的一部分 void paintComponent(Graphics g) 需要重写该方法来说明你的组件应如何绘制 一 . 2D图形 随着 , J2SE包含了一个 Java 2D库,该库实现了一个非常强大的图形操作集 如何获得 Graphics 2D类对象 该类是 Graphics类的子类,通常可用Graphics对象转换而来 .例: public void paintComponent(Graphics g) { graphics2D g2=(graphics 2D)g。 … .. } JDK java 2D形体使用浮点数坐标系,这样可为坐标指定单位。 存在的问题 :Retangle2D r=……… .。 float f=()。 /*Error*/ getWidth()返回 double值,应进行转换: float f=(float)()。 例: 因此, 2D库的形体类有两种版本: class DrawPanel extends JPanel { public void paintComponent(Graphics g) { (g)。 Graphics2D g2 = (Graphics2D)g。 // draw a rectangle double leftX = 100。 double topY = 100。 double width = 200。 double height = 150。 Rectangle2D rect = new (leftX, topY, width, height)。 (rect)。 // draw the enclosed ellipse Ellipse2D ellipse = new ()。 (rect)。 (ellipse)。 // draw a diagonal line (new (leftX, topY, leftX + width, topY + height))。 // draw a circle with the same center double centerX = ()。 double centerY = ()。 double radius = 150。 Ellipse2D circle = new ()。 (centerX, centerY, centerX + radius, centerY + radius)。 (circle)。 } } API (float x,float y,float width, float height) (double x,double y, double width,double height) (double x,double y, double width,double height) (Point2D start,Point2D end) (double startx,double starty, double endx,double endy) (double x,double y) fill(Graphics2D rec) 填充颜色 drawGraphics2D rec) 绘制图形 rec 二 .颜色 Graphics2D类的 setPaint方法 (Graphics类为 setColor方法 )用来设置颜色。 例: ()。 (“Set Color”,100,100) black、 blue、 cyan、 darkgray、 gray、green、 lightgray、 magenta、 oran。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。