java jpanel生成图片_java – 在JPanel上绘制图像并将其添加到JFr...

本文介绍了如何在Java中使用JPanel进行图像绘制,特别是创建箱线图,并尝试将其添加到JFrame。作者遇到了在JPanel上绘制的内容无法在JFrame上显示的问题,代码中展示了BoxPlot类和Box类的实现,包括计算统计值和实际的绘图过程。然而,代码可能缺少关键步骤导致图像未显示。
摘要由CSDN通过智能技术生成

我是初中高中java学生,我必须做一个简单的盒子和胡须的事情.我无法将我在JPanel上绘制的内容显示在JFrame上.图纸没有显示出来.谁能帮助我?

public class BoxPlot extends JFrame {

public final int MIN;

public final double Q1;

public final double MEDIAN;

public final double Q3;

public final int MAX;

public BoxPlot(int[] data){

this.setLayout(new GridBagLayout());

this.MIN = Statistics.min(data);

this.Q1 = Statistics.lowerQuartile(data);

this.MEDIAN = Statistics.median(data);

this.Q3 = Statistics.upperQuartile(data);

this.MAX = Statistics.max(data);

this.addBox(data);

this.addSummary();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(800, 600);

this.setResizable(true);

this.setVisible(true);

}

// Adds a panel with the boxplot

private void addBox(int[] data) {

Box box = new Box(data);

GridBagConstraints c = new GridBagConstraints();

c.gridx = 0;

c.gridy = 0;

c.weighty = 1;

this.add(box, c);

}

// Adds a panel with the five number summaries as JLabels

// to the bottom of the JFrame

private void addSummary() {

JPanel summary = new JPanel();

summary.setLayout(new GridLayout());

JLabel l = new JLabel("Minimum: "+this.MIN, JLabel.CENTER);

summary.add(l);

l = new JLabel("First Quartile: "+this.Q1, JLabel.CENTER);

summary.add(l);

l = new JLabel("Median: "+this.MEDIAN, JLabel.CENTER);

summary.add(l);

l = new JLabel("Third Quartile: "+this.Q3, JLabel.CENTER);

summary.add(l);

l = new JLabel("Maximum: "+this.MAX, JLabel.CENTER);

summary.add(l);

GridBagConstraints c = new GridBagConstraints();

c.gridx = 0;

c.gridy = 1;

c.weightx = 1;

c.ipady = 10;

c.fill = GridBagConstraints.HORIZONTAL;

c.anchor = GridBagConstraints.PAGE_END;

this.add(summary, c);

}

public class Box extends JPanel{

private int min;

private double q1;

private double median;

private double q3;

private int max;

public Box(int[] data) {

this.min = Statistics.min(data);

this.q1 = Statistics.lowerQuartile(data);

this.median = Statistics.median(data);

this.q3 = Statistics.upperQuartile(data);

this.max = Statistics.max(data);

}

// paints the box onto the JPanel

public void paintComponent(Graphics g){

int d1 =10 * (int) (this.q1-this.min);

int d2 =10 * (int) (this.median-this.q1);

int d3 =10 * (int) (this.q3-this.median);

int d4 =10 * (int) (this.max-this.q3);

g.drawLine(100, 150, 100+d1, 150);

g.drawLine(100+d1+d2+d3, 150, 100+d1+d2+d3+d4, 150);

g.setColor(Color.RED);

g.fillRect(100+d1, 100, d2, 100);

g.setColor(Color.BLUE);

g.fillRect(100+d1+d2, 100, d3, 100);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值