You are on page 1of 9

GUI Programming

Frames.java

ShowFlowLayout.java

ShowGridLayout.java

ShowBorderLayout.java The Color Class You can create a color using the following constructor: public Color(int r, int g, int b);

in which r, g, and b specify a color by its red, green, and blue components. For example, Color color = new Color(128, 100, 100); The Font Class The constructor for Font is: public Font(String name, int style, int size);

You can choose a font name from SansSerif, Serif, Monospaced, Dialog, or DialogInput, choose a style from Font.PLAIN (0), Font.BOLD (1), Font.ITALIC (2), and Font.BOLD Font.ITALIC (3), and specify a font size of any positive integer. For example, the following statements create two fonts and set one font to a button: Font font1 = new Font("SansSerif", Font.BOLD, 16); Font font2 = new Font("Serif", Font.BOLD + Font.ITALIC, 12); JButton jbtOK = new JButton("OK"); jbtOK.setFont(font1);

Using Panels as Subcontainers JPanel p = new JPanel(); p.add(new JButton("OK"));

Panels can be placed inside a frame or inside another panel. The following statement places panel p into frame f: f.add(p);

TestPanels.java

TestSwingCommonFeatures.java

The Graphics Class

Graphics graphics = jlblBanner.getGraphics();

TestGetGraphics.java The paintComponent Method protected void paintComponent(Graphics g)

TestPaintComponent.java

Drawing Graphics on Panels TestPanelDrawing.java

Drawing Strings, Lines, Rectangles, and Ovals The drawString(String s, int x, int y) method draws a string starting at the point (x, y) The drawRect(int x, int y, int w, int h) method draws a plain rectangle, and the fillRect(int x, int y, int w, int h) method draws a filled rectangle.

The drawRoundRect(int x, int y, int w, int h, int aw, int ah) method draws a round-cornered rectangle, and the fillRoundRect(int x, int y, int w, int h, int aw, int ah) method draws a filled round-cornered rectangle.

The draw3DRect(int x, int y, int w, int h, boolean raised) method draws a 3D rectangle and the fill3DRect(int x, int y, int w, int h, boolean raised) method draws a filled 3D rectangle. TestFigurePanel.java

Drawing Arcs

drawArc(int x, int y, int w, int h, int startAngle, int arcAngle); fillArc(int x, int y, int w, int h, int startAngle, int arcAngle);

DrawArcs.java

The Polygon Class and Drawing Polygons and Polylines The two constructors given below are used to create a Polygon object.

public Polygon() Constructs an empty polygon.

public Polygon(int[] xpoints, int[] ypoints, int npoints)

Here is an example of creating a polygon and adding points into it: Polygon polygon = new Polygon(); polygon.addPoint(40, 20); polygon.addPoint(70, 40); polygon.addPoint(60, 80); polygon.addPoint(45, 45); polygon.addPoint(20, 60);

To draw or fill a polygon, use one of the following methods: drawPolygon(Polygon polygon); fillPolygon(Polygon polygon); drawPolygon(int[] xpoints, int[] ypoints, int npoints); fillPolygon(int[] xpoints, int[] ypoints, int npoints);

For example: int x[] = {40, 70, 60, 45, 20}; int y[] = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length);

Centering a Display Using the FontMetrics Class

FontMetrics is an abstract class. To get a FontMetrics object for a specific font, use the following getFontMetrics methods defined in the Graphics class:

public FontMetrics getFontMetrics(Font font) Returns the font metrics of the specified font. public FontMetrics getFontMetrics() Returns the font metrics of the current font.

TestCenterMessage.java

The MessagePanel Class

TestMessagePanel.java

The StillClock Class

DisplayClock.java Displaying Images

DisplayImage.java

The ImageViewer Class

SixFlags.java

You might also like