楊小白 2016-06-15 11:14 采纳率: 0%
浏览 1317

java将代码转变成图形界面。求具体的代码。

public abstract class Shape {
public static final double PI=3.14;

public abstract double getArea();

public abstract double getPerimeter();

}public class Circle extends Shape{

double r;
public Circle(double r){
    this.r=r;
}

@Override   
public double getArea(){
    return Shape.PI*r*r;
}

@Override
public double getPerimeter(){
    return Shape.PI*r*2;
}

}public class Rectangle extends Shape {

double m, n;

public Rectangle(double m, double n) {

    this.m = m;
    this.n = n;

}

@Override
public double getArea() {
    return m * n;
}

@Override
public double getPerimeter() {
    return 2 * (m + n);
}

}public class Triangle extends Shape {

double a, b, c;

public Triangle(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

@Override
public double getArea() {
    double p = (a + b + c) / 2;

    if ((a + b) > c && (a + c) > b && (b + c) > c) {

        return Math.sqrt(p * (p - a) * (p - b) * (p - c));

    } else {

        System.out.println("wrong values");
        return -1;

    }
}

@Override
public double getPerimeter() {
    if ((a + b) > c && (a + c) > b && (b + c) > c) {

        return a + b + c;

    } else {

        System.out.println("wrong values");
        return -1;

    }

}

}import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("choose the shape which is the one you want calculate it's area and Perimeter:");
System.out.println("1.Circle\t2.Triangle\t3.Rectangle");
try {
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("input r:");
try {
double r = sc.nextDouble();
Shape s = new Circle(r);
System.out.println("the area of the circle is :" + s.getArea()
+ "\nthe perimeter of the circle is :" + s.getPerimeter());
} catch (Exception e) {
System.out.println("wrong");
}
break;
case 2:
System.out.println("input a,b,c:");
double a, b, c;
try {
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
Shape s = new Triangle(a, b, c);
System.out.println("the area of the circle is :" + s.getArea()
+ "\nthe perimeter of the circle is :" + s.getPerimeter());

            } catch (Exception e) {
                System.out.println("wrong");
            }
            break;
        case 3:
            System.out.println("input m,n:");
            double m, n;
            try {
                m = sc.nextDouble();
                n = sc.nextDouble();
                Shape s = new Rectangle(m, n);
                System.out.println("the area of the circle is :" + s.getArea()
                        + "\nthe perimeter of the circle is :" + s.getPerimeter());

            } catch (Exception e) {
                System.out.println("wrong");
            }
            break;
        }
    } catch (Exception e) {
        System.out.println("please choose a correct number");
    }
    sc.close();
}

}怎么变成java组件和事件处理的图形化界面。

  • 写回答

1条回答 默认 最新

  • caogang_90 2016-06-16 01:41
    关注

    package com.crazy.jfreechart;

    import java.awt.Font;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.CategoryAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    /**

    • 该类用于演示最简单的柱状图生成
    • @author Winter Lau
      */
      public class BarChartDemo {
      public static void main(String[] args) throws IOException{

      CategoryDataset dataset = getDataSet2(); 
      JFreeChart chart = ChartFactory.createBarChart3D( 
                         "水果产量图", // 图表标题
                         "水果", // 目录轴的显示标签
                         "产量", // 数值轴的显示标签
                          dataset, // 数据集
                          PlotOrientation.VERTICAL, // 图表方向:水平、垂直
                          true,  // 是否显示图例(对于简单的柱状图必须是 false)
                          false, // 是否生成工具
                          false  // 是否生成 URL 链接
                          ); 
      //中文乱码
      CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
      NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
      CategoryAxis domainAxis = categoryplot.getDomainAxis();  
      TextTitle textTitle = chart.getTitle();
      textTitle.setFont(new Font("黑体", Font.PLAIN, 20));      
      domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
      domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
      numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
      numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
      chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
      
      FileOutputStream fos_jpg = null; 
      try { 
          fos_jpg = new FileOutputStream("D:\\BarChart.jpg"); 
          ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f,chart,800,600,null); 
      } finally { 
          System.out.println("图片生成成功");
          try { 
              fos_jpg.close(); 
          } catch (Exception e) {
      
          } 
      } 
      

      }
      /**

      • 获取一个演示用的简单数据集对象
      • @return / private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "", "苹果"); dataset.addValue(200, "", "梨子"); dataset.addValue(300, "", "葡萄"); dataset.addValue(400, "", "香蕉"); dataset.addValue(500, "", "荔枝"); return dataset; } /*
      • 获取一个演示用的组合数据集对象
      • @return */ private static CategoryDataset getDataSet2() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); return dataset; } }
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮