【问题标题】:Java JFrame Plotting Multiple LinesJava JFrame 绘制多行
【发布时间】:2014-10-21 18:01:16
【问题描述】:

所以我的问题的症结是在 Java 中将多个组件绘制成一个 JFrame。我试图使用相同的组件两次来绘制两条不同的线,但只出现一条。我在不同的文件中处理三个不同的类,这可能会让我更难。我尝试了可能的解决方案,但无济于事herehereherehere 和其他地方。我怀疑我做错了很多事情,因为我仍在努力完全理解JFrameJPanelLayoutManagers。谁能指出我哪里出错了?

我的测试类如下:

import javax.swing.JFrame;

public class TransportSlabTester
{
    public static void main(String[] args)
    {
        System.out.println("Estimation at 100 sections: ");
        TransportSlab slab1 = new TransportSlab(10000,1,5,100);
        System.out.println();

        JFrame frame = new JFrame("Attenuated Profile");
        frame.setSize(600,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TransportSlabGraph component = new TransportSlabGraph();
        //analytical is a method from a 3rd class that returns double[]
        component.attProfileArray(slab1.analytical(),slab1.getThickness());
        frame.add(component);
        component = new TransportSlabGraph();
        //euler is a method from a 3rd class that returns double[]
        component.attProfileArray(slab1.euler(),slab1.getThickness());
        frame.add(component);
        frame.setVisible(true);
    }
}

现在,扩展 JPanel 的类:

import java.awt.*;
import java.awt.geom.Line2D;
import java.math.*;
import javax.swing.JPanel;

public class TransportSlabGraph extends JPanel
{
    double[] N, xAxes, yAxes;
    final int edge = 100; //Distance from edge of frame
    String[] xlabel = new String[11];
    String[] ylabel = new String[11];

    /**
     *
     * @param inputN Data array of type {@code double[]}
     * @param thickness Thickness set by the original constructor
     */
    public void attProfileArray(double[] inputN, double thickness)
    {
        N = new double[inputN.length];
        //Create labels for the tick marks of the x and y axis from rounded #'s
        BigDecimal bd1, bd2;
        for (int i = 0; i <= 10; i++)
        {
            bd1 = new BigDecimal((thickness/10)*i);
            MathContext mc = new MathContext(2); //Round to one decimal place
            bd2 = bd1.round(mc);
            xlabel[i] = String.valueOf(bd2.doubleValue());
            ylabel[i] = String.valueOf((inputN[0]*i)/(inputN.length-1));
        }
        //Set up data array and the axes
        for (int i = 0; i < N.length; i++)
        {
            N[i]=inputN[i];
            xAxes = new double[N.length];
            yAxes = new double[N.length];
        }
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        //Get frame dimensions to scale drawn components
        int w = getWidth();
        int h = getHeight();
        double xInc = (double)(w-2*edge)/(N.length-1);
        double scale = (double)(h-2*edge)/N[0];
        g2.draw(new Line2D.Double(edge, h-edge, w-edge, h-edge)); //draw x axis
        g2.draw(new Line2D.Double(edge, edge, edge, h-edge)); // draw y axis
        //Create evenly spaced tick marks for both axes and label them
        for (int i = 0; i <= 10; i++)
        {
            g2.draw(new Line2D.Double(edge+((w-edge-edge)/10.0)*i, h-edge-10, edge+((w-edge-edge)/10.0)*i, h-edge+10)); //x ticks
            g2.draw(new Line2D.Double(edge-10, h-edge-((h-edge-edge)/10.0)*i, edge+10, h-edge-((h-edge-edge)/10.0)*i)); //y ticks
            g2.drawString(xlabel[i],(int)(edge+((w-edge-edge)/10.0)*i),h-edge+20);
            g2.drawString(ylabel[i],edge-30,(int)(h-edge-((h-edge-edge)/10.0)*i));
        }
        //Scale data and convert to pixel coordinates
        for (int i = 0; i < N.length; i++)
        {
            xAxes[i] = edge+i*xInc;
            yAxes[i] = h-edge-scale*N[i];
        }
        //Only set the data line's color
        g2.setPaint(Color.BLUE);
        //Draw the data as a series of line segments
        for (int i = 1; i < N.length; i++)
        {
            g2.draw(new Line2D.Double(xAxes[i-1],yAxes[i-1],xAxes[i],yAxes[i]));
        }
    }
}

【问题讨论】:

  • 一个组件只能添加到容器中一次
  • Symbol not found: TransportSlab 要尽快获得更好的帮助,请发布MCVE(最小、完整、可验证的示例)。
  • TransportSlab 是我为节省空间而省略的第三类。

标签: java swing plot jframe paintcomponent


【解决方案1】:

问题 #1

Component 的实例只能驻留在单个 Container 中(一次)。

您需要为要添加的每个Component 创建一个新实例。我会推荐一个工厂模式...

问题 #2

JFrame,但默认使用 BorderLayout,这将只允许单个组件驻留在其 5 个可用布局位置中的每一个位置。

您还会遇到问题,因为您的 TransportSlabGraph 类没有覆盖它的 getPreferredSize 方法,这意味着默认情况下,许多组件的实例将提供默认大小为 0x0布局管理器。

考虑将布局管理器更改为 GridLayout 之类的内容。

查看Laying Out Components Within a Container了解更多详情

【讨论】:

  • 喜欢以下? TransportSlabGraph component2 = new TransportSlabGraph();component2.attProfileArray(slab1.euler(),slab1.getThickness());frame.add(component2);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2018-05-07
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多