JAVA-窗口中绘制图形
2011-04-17 20:41
232 查看
import java.awt.*; import java.awt.geom.*; import javax.swing.*; /* * 窗口中绘图(长方形,椭圆,对角线,圆) * class structure * Point2D-Point * -Ellipse2D * Shape-Rectangular Shape | * | -Rectangel2D-Rectangle * ---Line2D * * -Rectangle2D.Float * Rectangele2D-| * -Rectangle2d.Double */ public class DrawTest { public static void main(String[] args){ EventQueue.invokeLater(new Runnable() { public void run() { DrawFrame frame = new DrawFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } /** * A frame that contains apanel with drawings */ class DrawFrame extends JFrame { public DrawFrame() { setTitle("DrawFrame"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //add a panel to frame DrawComponent component = new DrawComponent(); add(component); } public static final int DEFAULT_WIDTH = 400; public static final int DEFAULT_HEIGHT = 400; } /** * A component that displays rectangles and ellipses */ class DrawComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; //draw a rectangle double leftX = 100; double topY = 100; double width = 200; double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX,topY,width,height); g2.draw(rect); //draw a ellipse //*Ellipse2D ellipse = new Ellipse2D.Double(leftX,topY,width,height); Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); //draw a diagonal line g2.draw(new Line2D.Double(leftX,topY,leftX+width,topY+height)); //draw a circle with the same center double centerX = leftX + width/2; double centerY = topY + height/2; double radius = 150; Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(centerX, centerY,centerX+radius,centerY+radius); //*Ellipse2D circle = new Ellipse2D.Double(leftX,topY,radius*2,radius*2); g2.draw(circle); } }
相关文章推荐
- 我的java思想005:窗口中绘制图形
- java开发系统内核:实现窗口图形绘制API
- JAVA学习笔记-加载窗口_画图形_编程中坐标基本知识
- JFreeChart java 图形绘制
- 史上最扯Java图形绘制(J2SE)之一JAVA动画效果
- java.awt(包含用于创建用户界面和绘制图形图像的所有类)
- Java入门:绘制简单图形
- JAVA程序设计(12.2)---- 多态、监听器初级应用:在窗口中绘制图形,可以选择圆形 正方形 线条
- Qt自定义界面类并提升(提升的窗口部件),把OpenGL绘制的图形显示在QT的ui界面上
- Java基础之在窗口中绘图——绘制星星(StarApplet 1)
- JAVA绘制图形方法
- 【JAVA语言程序设计基础篇】--图形--练习绘制时钟
- 学习Java过程中关于金字塔和镂空金字塔形状图形绘制的代码
- 我的java思想003:如何在窗口中画出图形
- 用Java图形窗口显示网页内容
- java图形程序设计之面板图形绘制
- java 2D图形绘制
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
- Qt自定义界面类并提升(提升的窗口部件),把OpenGL绘制的图形显示在QT的ui界面上
- 【JAVA语言程序设计基础篇】--图形--练习绘制弧形