Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The FigurePanel class in Listing 13.3 can display lines, rectangles, round-cornered rectangles and ovals. Add appropriate new code in the class to display arcs
- * and polygons. Write a test program to display the shapes as shown in Figure 13.28a using the new FigurePanel class.
- */
- import java.awt.*;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class ImproveFigurePanel extends JFrame
- {
- public ImproveFigurePanel()
- {
- setLayout(new GridLayout(2, 5, 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));
- add(new ArcsPanel());
- add(new ArcsPanel(true));
- add(new PolygonPanel());
- add(new PolygonPanel(true));
- }
- public static void main(String[] args)
- {
- ImproveFigurePanel frame = new ImproveFigurePanel();
- frame.setSize(400, 200);
- frame.setTitle("TestFigurePanel");
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- @SuppressWarnings("serial")
- // ArcsPanel class
- class ArcsPanel extends JPanel
- {
- private boolean filled;
- public ArcsPanel()
- {
- }
- public ArcsPanel(boolean filled)
- {
- this.filled = filled;
- }
- @Override // Draw four blades of a fan
- protected void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- int xCenter = getWidth() / 2;
- int yCenter = getHeight() / 2;
- int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
- int x = xCenter - radius;
- int y = yCenter - radius;
- g.setColor(Color.green);
- if(filled)
- {
- g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
- g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
- g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
- g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
- }
- else
- {
- g.drawArc(x, y, 2 * radius, 2 * radius, 0, 30);
- g.drawArc(x, y, 2 * radius, 2 * radius, 90, 30);
- g.drawArc(x, y, 2 * radius, 2 * radius, 180, 30);
- g.drawArc(x, y, 2 * radius, 2 * radius, 270, 30);
- }
- }
- }
- @SuppressWarnings("serial")
- // PolygonPanel class
- class PolygonPanel extends JPanel
- {
- private boolean filled;
- public PolygonPanel()
- {
- }
- public PolygonPanel(boolean filled)
- {
- this.filled = filled;
- }
- protected void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- int xCenter = getWidth() / 2;
- int yCenter = getHeight() / 2;
- int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
- g.setColor(Color.yellow);
- // Create new polygon object
- Polygon polygon = new Polygon();
- // Add points to the polygon in this order
- polygon.addPoint(xCenter + radius, yCenter);
- polygon.addPoint((int)(xCenter + radius *
- Math.cos(2 * Math.PI / 6)), (int)(yCenter - radius *
- Math.sin(2 * Math.PI / 6)));
- polygon.addPoint((int)(xCenter + radius *
- Math.cos(2 * 2 * Math.PI / 6)), (int)(yCenter - radius *
- Math.sin(2 * 2 * Math.PI / 6)));
- polygon.addPoint((int)(xCenter + radius *
- Math.cos(3 * 2 * Math.PI / 6)), (int)(yCenter - radius *
- Math.sin(3 * 2 * Math.PI / 6)));
- polygon.addPoint((int)(xCenter + radius *
- Math.cos(4 * 2 * Math.PI / 6)), (int)(yCenter - radius *
- Math.sin(4 * 2 * Math.PI / 6)));
- polygon.addPoint((int)(xCenter + radius *
- Math.cos(5 * 2 * Math.PI / 6)), (int)(yCenter - radius *
- Math.sin(5 * 2 * Math.PI / 6)));
- // Draw the polygon
- if(filled)
- g.fillPolygon(polygon);
- else
- g.drawPolygon(polygon);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment