Java之图形程序设计

AWT、Swing、JavaFX

Java起初包含一个用于基本GUI程序设计的类库AWT(Abstract Window Toolkit)

后来基于AWT架构上创建了Swing类库,仅仅提供能力更加强大的用户界面控件。

比如,Button,Frame等类属于AWT组件,Swing组件由AWT组件扩展而来,以J开头

在不久的将来,JavaFX可能会取代Swing。

框架

创建框架

 1 package simpleFrame;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * @version 1.32 2007-06-12
 8  * @author Cay Horstmann
 9  */
10 public class SimpleFrameTest
11 {
12    public static void main(String[] args)
13    {
14       EventQueue.invokeLater(new Runnable()
15          {
16             public void run()
17             {
18                SimpleFrame frame = new SimpleFrame();
19                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20                frame.setVisible(true);
21             }
22          });
23    }
24 }
25 
26 class SimpleFrame extends JFrame
27 {
28    private static final int DEFAULT_WIDTH = 300;
29    private static final int DEFAULT_HEIGHT = 200;
30 
31    public SimpleFrame()
32    {
33       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
34    }
35 }
View Code

 框架定位

 1 package sizedFrame;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * @version 1.32 2007-04-14
 8  * @author Cay Horstmann
 9  */
10 public class SizedFrameTest
11 {
12    public static void main(String[] args)
13    {
14       EventQueue.invokeLater(new Runnable()
15          {
16             public void run()
17             {
18                JFrame frame = new SizedFrame();
19                frame.setTitle("SizedFrame");
20                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21                frame.setVisible(true);
22             }
23          });
24    }
25 }
26 
27 class SizedFrame extends JFrame
28 {
29    public SizedFrame()
30    {
31       // get screen dimensions
32 
33       Toolkit kit = Toolkit.getDefaultToolkit();
34       Dimension screenSize = kit.getScreenSize();
35       int screenHeight = screenSize.height;
36       int screenWidth = screenSize.width;
37 
38       // set frame width, height and let platform pick screen location
39 
40       setSize(screenWidth / 2, screenHeight / 2);
41       setLocationByPlatform(true);
42 
43       // set frame icon
44 
45       Image img = new ImageIcon("icon.gif").getImage();
46       setIconImage(img);      
47    }
48 }
View Code

在组件中显示信息

 1 package notHelloWorld;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 /**
 7  * @version 1.32 2007-06-12
 8  * @author Cay Horstmann
 9  */
10 public class NotHelloWorld
11 {
12    public static void main(String[] args)
13    {
14       EventQueue.invokeLater(new Runnable()
15          {
16             public void run()
17             {
18                JFrame frame = new NotHelloWorldFrame();
19                frame.setTitle("NotHelloWorld");
20                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21                frame.setVisible(true);
22             }
23          });
24    }
25 }
26 
27 /**
28  * A frame that contains a message panel
29  */
30 class NotHelloWorldFrame extends JFrame
31 {
32    public NotHelloWorldFrame()
33    {
34       add(new NotHelloWorldComponent());
35       pack();
36    }
37 }
38 
39 /**
40  * A component that displays a message.
41  */
42 class NotHelloWorldComponent extends JComponent
43 {
44    public static final int MESSAGE_X = 75;
45    public static final int MESSAGE_Y = 100;
46 
47    private static final int DEFAULT_WIDTH = 300;
48    private static final int DEFAULT_HEIGHT = 200;
49 
50    public void paintComponent(Graphics g)
51    {
52       g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);
53    }
54    
55    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
56 }
View Code

2D图形

 1 package draw;
 2 
 3 import java.awt.*;
 4 import java.awt.geom.*;
 5 import javax.swing.*;
 6 
 7 /**
 8  * @version 1.32 2007-04-14
 9  * @author Cay Horstmann
10  */
11 public class DrawTest
12 {
13    public static void main(String[] args)
14    {
15       EventQueue.invokeLater(new Runnable()
16          {
17             public void run()
18             {
19                JFrame frame = new DrawFrame();
20                frame.setTitle("DrawTest");
21                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22                frame.setVisible(true);
23             }
24          });
25    }
26 }
27 
28 /**
29  * A frame that contains a panel with drawings
30  */
31 class DrawFrame extends JFrame
32 {
33    public DrawFrame()
34    {      
35       add(new DrawComponent());
36       pack();
37    }
38 }
39 
40 /**
41  * A component that displays rectangles and ellipses.
42  */
43 class DrawComponent extends JComponent
44 {
45    private static final int DEFAULT_WIDTH = 400;
46    private static final int DEFAULT_HEIGHT = 400;
47 
48    public void paintComponent(Graphics g)
49    {
50       Graphics2D g2 = (Graphics2D) g;
51 
52       // draw a rectangle
53 
54       double leftX = 100;
55       double topY = 100;
56       double width = 200;
57       double height = 150;
58 
59       Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
60       g2.draw(rect);
61 
62       // draw the enclosed ellipse
63 
64       Ellipse2D ellipse = new Ellipse2D.Double();
65       ellipse.setFrame(rect);
66       g2.draw(ellipse);
67 
68       // draw a diagonal line
69 
70       g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));
71 
72       // draw a circle with the same center
73 
74       double centerX = rect.getCenterX();
75       double centerY = rect.getCenterY();
76       double radius = 150;
77 
78       Ellipse2D circle = new Ellipse2D.Double();
79       circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
80       g2.draw(circle);
81    }
82    
83    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
84 }
View Code

使用颜色

 1 package fill;
 2 
 3 import java.awt.*;
 4 import java.awt.geom.*;
 5 import javax.swing.*;
 6 
 7 /**
 8  * @version 1.33 2007-04-14
 9  * @author Cay Horstmann
10  */
11 public class FillTest
12 {
13    public static void main(String[] args)
14    {
15       EventQueue.invokeLater(new Runnable()
16          {
17             public void run()
18             {
19                JFrame frame = new FillFrame();
20                frame.setTitle("FillTest");
21                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22                frame.setVisible(true);
23             }
24          });
25    }
26 }
27 
28 /**
29  * A frame that contains a component with drawings
30  */
31 class FillFrame extends JFrame
32 {
33    public FillFrame()
34    {
35       add(new FillComponent());
36       pack();
37    }
38 }
39 
40 /**
41  * A component that displays filled rectangles and ellipses
42  */
43 class FillComponent extends JComponent
44 {
45    private static final int DEFAULT_WIDTH = 400;
46    private static final int DEFAULT_HEIGHT = 400;
47 
48    public void paintComponent(Graphics g)
49    {
50       Graphics2D g2 = (Graphics2D) g;
51 
52       // draw a rectangle
53 
54       double leftX = 100;
55       double topY = 100;
56       double width = 200;
57       double height = 150;
58 
59       Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
60       g2.setPaint(Color.BLACK);
61       g2.draw(rect);
62       g2.setPaint(Color.RED);
63       g2.fill(rect); // Note that the right and bottom edge are not painted over
64       
65       // draw the enclosed ellipse
66 
67       Ellipse2D ellipse = new Ellipse2D.Double();
68       ellipse.setFrame(rect);
69       g2.setPaint(new Color(0, 128, 128)); // a dull blue-green
70       g2.fill(ellipse);
71    }
72    
73    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
74 }
View Code

文本使用特殊字体

 1 package font;
 2 
 3 import java.awt.*;
 4 import java.awt.font.*;
 5 import java.awt.geom.*;
 6 import javax.swing.*;
 7 
 8 /**
 9  * @version 1.33 2007-04-14
10  * @author Cay Horstmann
11  */
12 public class FontTest
13 {
14    public static void main(String[] args)
15    {
16       EventQueue.invokeLater(new Runnable()
17          {
18             public void run()
19             {
20                JFrame frame = new FontFrame();
21                frame.setTitle("FontTest");
22                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23                frame.setVisible(true);
24             }
25          });
26    }
27 }
28 
29 /**
30  * A frame with a text message component
31  */
32 class FontFrame extends JFrame
33 {
34    public FontFrame()
35    {      
36       add(new FontComponent());
37       pack();
38    }
39 }
40 
41 /**
42  * A component that shows a centered message in a box.
43  */
44 class FontComponent extends JComponent
45 {
46    private static final int DEFAULT_WIDTH = 300;
47    private static final int DEFAULT_HEIGHT = 200;
48 
49    public void paintComponent(Graphics g)
50    {
51       Graphics2D g2 = (Graphics2D) g;
52 
53       String message = "Hello, World!";
54 
55       Font f = new Font("Serif", Font.BOLD, 36);
56       g2.setFont(f);
57 
58       // measure the size of the message
59 
60       FontRenderContext context = g2.getFontRenderContext();
61       Rectangle2D bounds = f.getStringBounds(message, context);
62 
63       // set (x,y) = top left corner of text
64 
65       double x = (getWidth() - bounds.getWidth()) / 2;
66       double y = (getHeight() - bounds.getHeight()) / 2;
67 
68       // add ascent to y to reach the baseline
69 
70       double ascent = -bounds.getY();
71       double baseY = y + ascent;
72 
73       // draw the message
74 
75       g2.drawString(message, (int) x, (int) baseY);
76 
77       g2.setPaint(Color.LIGHT_GRAY);
78 
79       // draw the baseline
80 
81       g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));
82 
83       // draw the enclosing rectangle
84 
85       Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
86       g2.draw(rect);
87    }
88    
89    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
90 }
View Code

字体列表

 1 package listFonts;
 2 
 3 import java.awt.*;
 4 
 5 /**
 6  * @version 1.11 2004-06-05
 7  * @author Cay Horstmann
 8  */
 9 public class ListFonts
10 {
11    public static void main(String[] args)
12    {
13       String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
14             .getAvailableFontFamilyNames();
15 
16       for (String fontName : fontNames)
17          System.out.println(fontName);
18    }
19 }
View Code

显示图像

 1 package image;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * @version 1.33 2007-04-14
 8  * @author Cay Horstmann
 9  */
10 public class ImageTest
11 {
12    public static void main(String[] args)
13    {
14       EventQueue.invokeLater(new Runnable()
15          {
16             public void run()
17             {
18                JFrame frame = new ImageFrame();
19                frame.setTitle("ImageTest");
20                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21                frame.setVisible(true);
22             }
23          });
24    }
25 }
26 
27 /**
28  * A frame with an image component
29  */
30 class ImageFrame extends JFrame
31 {
32    public ImageFrame()
33    {
34       add(new ImageComponent());
35       pack();
36    }
37 }
38 
39 /**
40  * A component that displays a tiled image
41  */
42 class ImageComponent extends JComponent
43 {
44    private static final int DEFAULT_WIDTH = 300;
45    private static final int DEFAULT_HEIGHT = 200;
46 
47    private Image image;
48 
49    public ImageComponent()
50    {
51       image = new ImageIcon("blue-ball.gif").getImage();
52    }
53 
54    public void paintComponent(Graphics g)
55    {
56       if (image == null) return;
57 
58       int imageWidth = image.getWidth(this);
59       int imageHeight = image.getHeight(this);
60 
61       // draw the image in the upper-left corner
62 
63       g.drawImage(image, 0, 0, null);
64       // tile the image across the component
65 
66       for (int i = 0; i * imageWidth <= getWidth(); i++)
67          for (int j = 0; j * imageHeight <= getHeight(); j++)
68             if (i + j > 0) 
69                g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);
70    }
71    
72    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
73 }
View Code

 

posted @ 2018-04-11 16:15  睿阳  阅读(399)  评论(0编辑  收藏  举报