Intro to Java Programming, 6E - FigurePanel.java
来源:互联网 发布:手机八字排盘命理软件 编辑:程序博客网 时间:2024/05/22 14:11
package UI;import java.awt.*;import javax.swing.JPanel;public class FigurePanel extends JPanel { // Define constants public static final int LINE = 1; public static final int RECTANGLE = 2; public static final int ROUND_RECTANGLE = 3; public static final int OVAL = 4; private int type = 1; private boolean filled; /** * Construct a default FigurePanel */ public FigurePanel() { } /** * Construct a FigurePanel with the specified type */ public FigurePanel(int type) { this.type = type; } /** * Construct a FigurePanel with the specified type and filled */ public FigurePanel(int type, boolean filled) { this.type = type; this.filled = filled; } /** * Draw a figure on the panel */ public void paintComponent(Graphics g) { super.paintComponent(g); // Get the appropriate size for the figure int width = getSize().width; int height = getSize().height; switch (type) { case LINE: // Display two cross lines g.setColor(Color.BLACK); g.drawLine(10, 10, width - 10, height - 10); g.drawLine(width - 10, 10, 10, height - 10); break; case RECTANGLE: // Display a rectangle g.setColor(Color.BLUE); if (filled) { g.fillRect((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); } else { g.drawRect((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); } break; case ROUND_RECTANGLE: // Display a round-cornered rectangle g.setColor(Color.RED); if (filled) { g.fillRoundRect((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 20, 20); } else { g.drawRoundRect((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 20, 20); } break; case OVAL: // Display an oval g.setColor(Color.BLACK); if (filled) { g.fillOval((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); } else { g.drawOval((int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); } } } /** * Set a new figure type */ public void setType(int type) { this.type = type; repaint(); } /** * Return figure type */ public int getType() { return type; } /** * Set a new filled property */ public void setFilled(boolean filled) { this.filled = filled; repaint(); } /** * Check if the figure is filled */ public boolean isFilled() { return filled; } /** * Specify preferred size */ public Dimension getPreferredSize() { return new Dimension(80, 80); }}
原文地址:http://www.cse.ust.hk/~liao/comp201/slides/html/FigurePanel.html
应用例子见《java语言程序设计-基础篇(原书第8版)》406~409页
0 0
- Intro to Java Programming, 6E - FigurePanel.java
- TestFigurePanel.java + FigurePanel
- Java Intro
- Introduction to Java Programming
- Programming By Intention/Intro to JUnit
- Lesson 2: Udacity Intro to Parallel Programming
- A Brief intro to X11 Programming
- Introduce to Java Programming:NOTE
- 22-04-2015 Intro to Java Chapter 11 笔记
- 26-06-2015 Intro to Java Chapter 30 笔记
- Back To Basics: Intro to Object Oriented Programming
- [Java](NumberFormat) Intro
- [JAVA] Struts2 Intro 简述
- Introduction to Java Programming-Comprehensive Version (6th Edition)
- <Introduction to Java Programming 6th Ed> Note 01
- Java Thread Programming 1.1 - Introduction to Threads
- An Introduction to Network Programming with Java
- <Introduction to Java Programming> Note 01
- Oracle 查看用户信息
- C++ string
- fw_printenv 配置文件 nand flash
- hdu4268贪心
- jqgrid 去除 clear Search Value 按钮
- Intro to Java Programming, 6E - FigurePanel.java
- CString.Format的详细用法(转)
- java调用脚本
- PadDesigner中添加自己创建的热风焊盘Flash
- How to get current full screen dimention and orientation in run time
- 数据库自我快速学习笔记
- 动态链接库和静态链接库的区别
- Eclipse快捷键大全(转载)
- Android之Adapter用法总结