Graphics
2014-05-10 09:04
260 查看
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TestFigurePanel extends JFrame {
public TestFigurePanel () {
setLayout(new GridLayout(2, 3, 5, 5));
add(new FigurePanel(FigurePanel.LINE));
add(new FigurePanel(FigurePanel.RECTANGLE));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
add(new FigurePanel(FigurePanel.OVAL));
add(new FigurePanel(FigurePanel.RECTANGLE, true));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));
}
public static void main(String[] args) {
TestFigurePanel frame = new TestFigurePanel();
frame.setTitle("TestFigurePanel");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null); //Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FigurePanel extends JPanel {
// Define the 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 = false;
/** 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 */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
switch(type) {
case LINE: // Display two cross lines
setBorder(new LineBorder(Color.BLACK));
g.setColor(Color.BLACK);
g.drawLine((int)(0.1 * width), (int)(0.1 * height), (int)(0.9 * width), (int)(0.9 * height));
g.drawLine((int)(0.1 * width), (int)(0.9 * height), (int)(0.9 * width), (int)(0.1 * height));
break;
case RECTANGLE: // Display a rectangle
setBorder(new LineBorder(Color.BLUE));
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
setBorder(new LineBorder(Color.RED));
g.setColor(Color.RED);
if(filled)
g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height) , (int)(0.8 * width), (int)(0.8 *height),
(int)(0.2 * width), (int)(0.2 * height));
else
g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height) , (int)(0.8 * width), (int)(0.8 *height),
(int)(0.2 * width), (int)(0.2 * height));
break;
case OVAL: // Display an oval
setBorder(new LineBorder(Color.BLACK));
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));
}
}
/** Return figure type */
public int getType() {
return type;
}
/** Set a new figure type */
public void setType(int type) {
this.type = type;
repaint();
}
/** Check if the figure is filled */
public boolean isFilled() {
return filled;
}
/** Set a new filled property */
public void setFilled(boolean filled) {
this.filled = filled;
repaint();
}
/** Specify preferred size */
public Dimension getPreferredSize() {
return new Dimension(80, 80);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TestFigurePanel extends JFrame {
public TestFigurePanel () {
setLayout(new GridLayout(2, 3, 5, 5));
add(new FigurePanel(FigurePanel.LINE));
add(new FigurePanel(FigurePanel.RECTANGLE));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
add(new FigurePanel(FigurePanel.OVAL));
add(new FigurePanel(FigurePanel.RECTANGLE, true));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));
}
public static void main(String[] args) {
TestFigurePanel frame = new TestFigurePanel();
frame.setTitle("TestFigurePanel");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null); //Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FigurePanel extends JPanel {
// Define the 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 = false;
/** 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 */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
switch(type) {
case LINE: // Display two cross lines
setBorder(new LineBorder(Color.BLACK));
g.setColor(Color.BLACK);
g.drawLine((int)(0.1 * width), (int)(0.1 * height), (int)(0.9 * width), (int)(0.9 * height));
g.drawLine((int)(0.1 * width), (int)(0.9 * height), (int)(0.9 * width), (int)(0.1 * height));
break;
case RECTANGLE: // Display a rectangle
setBorder(new LineBorder(Color.BLUE));
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
setBorder(new LineBorder(Color.RED));
g.setColor(Color.RED);
if(filled)
g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height) , (int)(0.8 * width), (int)(0.8 *height),
(int)(0.2 * width), (int)(0.2 * height));
else
g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height) , (int)(0.8 * width), (int)(0.8 *height),
(int)(0.2 * width), (int)(0.2 * height));
break;
case OVAL: // Display an oval
setBorder(new LineBorder(Color.BLACK));
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));
}
}
/** Return figure type */
public int getType() {
return type;
}
/** Set a new figure type */
public void setType(int type) {
this.type = type;
repaint();
}
/** Check if the figure is filled */
public boolean isFilled() {
return filled;
}
/** Set a new filled property */
public void setFilled(boolean filled) {
this.filled = filled;
repaint();
}
/** Specify preferred size */
public Dimension getPreferredSize() {
return new Dimension(80, 80);
}
}
相关文章推荐
- Graphics File Formats FAQ
- 闲谈Graphics单元
- High Dynamic Range Compression on Programmable Graphics Hardware
- 我的Ti显卡与NoSuitableGraphicsDeviceException
- CSIT-540 Spring 2007 - Project 4 - Benchmarking Graphics Hardware
- 如何使用Graphics绘制图像
- About the W32/Codbot-O —— DirectX Graphics (dxdmain)
- System.Drawing.Graphics读取带有索引的图片及思考
- WEB xml 画图 (javascript包装)用到 prototype.js wz_jsgraphics.js
- Threading and Integrated Graphics at GDC 2008 (英文)
- TC&Graphics
- From After Effects to Flash: Poetry in Motion Graphics
- Qt Graphics View Framework
- Windows 7中的远程桌面增强(Graphics Remoting, Remote Desktop)
- The Morgan Kaufmann Series Ebooks in Computer Graphics
- Mathematics for Computer Graphics
- 2D and 3D Graphics
- 零打碎敲学Android(一)—用什么来替代Graphics
- 游戏玩家的图形显示设置指南(11)(The Gamer's Graphics & Display Settings Guide)
- Exploring 2D Graphics(Chapter 4 of Hello, Android)