Swing图形绘制(长方形、椭圆、同心圆及直方图)
2016-06-21 15:23
357 查看
感觉自己的图形分析有待增强啊,还达不到想什么就能画什么的程度。
代码如下:
运行截屏:
代码如下:
ackage example; import java.awt.Graphics; //Fig. 5.27: ShapesTest.java, modified by pandenghuang@163.com //Test application that displays class Shapes. import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.Graphics; //handle the display public class ShapesTest { public static class Shapes extends JPanel { private int choice; // user's choice of which shape to draw // constructor sets the user's choice public Shapes(int userChoice) { choice = userChoice; } // draws a cascade of shapes starting from the top-left corner public void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth(); // total width int height = getHeight(); // total height int barHorizontalDistance=width/11; int barVerticalDistance=height/11; for (int i = 0; i < 10; i++) { // pick the shape based on the user's choice switch (choice) { case 1: // draw rectangles g.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); break; case 2: // draw ovals g.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); break; case 3: // draw concentric circles g.drawOval(width/2-(i+1)*10, height/2-(i+1)*10, 10+20*i, 10+20*i); break; case 4: // draw bar chart g.drawRect(i * barHorizontalDistance, height - i * barVerticalDistance, 20, height + i * height/11); break; } } } } public static void main(String[] args) { // obtain user's choice String input = JOptionPane.showInputDialog( "输入1画长方形\n" + "输入2画圆形\n"+ "输入3画同心圆\n"+ "输入4画条形图"); int choice = Integer.parseInt(input); // convert input to int // create the panel with the user's input Shapes panel = new Shapes(choice); JFrame application = new JFrame(); // creates a new JFrame application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.setSize(300, 300); application.setVisible(true); } } // end class ShapesTest
运行截屏:
相关文章推荐
- Sigar介绍与使用
- android中对线程池的理解与使用
- WebRTC代码走读(八):代码目录结构
- Linux route命令 显示getnameinfo failed [UNKNOWN]
- Javascript打印局部页面实例
- Maven - 继承和聚合
- 夏至日的计算公式
- elasticsearch异常信息汇总
- linux常用命令集锦
- jquery中选择器查找元素兼容IE的小问题
- eclipse全面提速
- 数组的逆序输出
- python学习笔记之函数总结--高阶函数以及装饰器
- Grunt、gulp、webpack、不要听着高大上你就上,试试Codekit?
- margin:0 auto;不居中
- springmvc 中controller与jsp传值
- weblogic 不停机部署更新
- broadleaf电商文档翻译 Merge Configuration 合并配置
- 循环有序数组最小值
- java实现像数据库表一样操作redis