⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise11_10.java

📁 java程序设计导论(daniel liang著) 所有偶数课后习题答案
💻 JAVA
字号:
// Exercise11_10.java: Display a pie chart
import java.awt.*;
import javax.swing.*;

public class Exercise11_10 extends JFrame {
  public static void main(String[] args) {
    JFrame frame = new Exercise11_10();
    frame.setSize(300, 300);
    frame.setTitle("Exercise11_10");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public Exercise11_10() {
    Container container = getContentPane();
    container.setLayout(new GridLayout(2, 2));
    container.add(new RectPanel());
    container.add(new OvalsPanel());
    container.add(new ArcsPanel());
    container.add(new PolygonsPanel());
  }
}

class RectPanel extends JPanel {
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    // Set new color
    g.setColor(Color.red);

    // Draw a rectangle
    g.drawRect(5, 5, getWidth()/2-10, getHeight()/2-10);

    // Draw a rounded rectangle
    g.drawRoundRect(getWidth()/2+5, 5,
      getWidth()/2-10, getHeight()/2-10, 60, 30);

    // Change the color to cyan
    g.setColor(Color.cyan);

    // Draw a 3D rectangle
    g.fill3DRect(5, getHeight()/2+5, getWidth()/2-10,
      getHeight()/2-10, true);

    // Draw a raised 3D rectangle
    g.fill3DRect(getWidth()/2+5, getHeight()/2+5, getWidth()/2-10,
      getHeight()/2-10, false);
  }
}

class OvalsPanel extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawOval(5, 5, getWidth()/2-10, getHeight()/2-10);
    g.setColor(Color.red);
    g.drawOval(getWidth()/2+5, 5, getWidth()/2-10, getHeight()/2-10);
    g.setColor(Color.yellow);
    g.fillOval(5, getHeight()/2+5, getWidth()/2-10,
      getHeight()/2-10);
    g.setColor(Color.orange);
    g.fillOval(getWidth()/2+5, getHeight()/2+5, getWidth()/2-10,
      getHeight()/2-10);
  }
}

class ArcsPanel extends JPanel {
  // Draw four blazes of a fan
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    int xCenter = getSize().width/2;
    int yCenter = getSize().height/2;
    int radius =
      (int)(Math.min(getSize().width, getSize().height)*0.4);

    int x = xCenter - radius;
    int y = yCenter - radius;

    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);
  }
}

class PolygonsPanel extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    int xCenter = getSize().width/2;
    int yCenter = getSize().height/2;
    int radius =
      (int)(Math.min(getSize().width, getSize().height)*0.4);

    // Create a Polygon object
    Polygon polygon = new Polygon();

    // Add points to the polygon
    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
    g.drawPolygon(polygon);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -