使用新输入重绘图形,Java图形

时间:2015-04-20 18:29:22

标签: java graphics graph redraw

我一直在努力寻找解决方案,但没有结果,所以我无处可去。我有一个程序从用户输入收集数据并使用它来绘制图形。每次用户更改文本字段中的数据并按下提交时,我想要绘制一个新图形并删除旧图形。

当我使用新输入按提交时,即使输入在两个类中都发生了变化,图表也没有任何反应。出于调试目的,我有一个

 System.out.println(graphPoints.size());

在我的DrawComponent类中。我注意到,每次按下提交时,graphPoints.size()被打印出来的次数会增加一次,所以我认为这没有用。我真的不知道从哪里开始。

这是我的actionlistener,我调用GraphPanel类:

query.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                textarea1.setText("");
                textarea1.append(run(tickerInput,tickerInput2,startInput,slutInput,valuta));
                Mainpanel.repaint();

                if(testx != null)
                       testx.removeAll();

                testx = new GraphPanel(first,second);
                addItem(Mainpanel, testx, 0,20, 20,20, GridBagConstraints.SOUTH);

                minframe.setVisible(true);
                minframe.repaint();
                minframe.pack();
                testx.repaint();

这是我的GraphPanel类:

public GraphPanel(ArrayList<Double> first, ArrayList<Double> second) {
    first1 = first;
    Collections.reverse(first1);
    second1 = second;
    Collections.reverse(second1);

}

@Override
protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        double xScale = ((double) getWidth() - (2 * padding) - labelPadding) / (first1.size() - 1);
        double yScale = ((double) getHeight() - 2 * padding - labelPadding) / (getMaxScore() - getMinScore());

        ArrayList<Point> graphPoints = new ArrayList<>();
        for (int i = 0; i < first1.size(); i++) {
            int x1 = (int) (i * xScale + padding + labelPadding);
            int y1 = (int) ((getMaxScore() - first1.get(i)) * yScale + padding);
            graphPoints.add(new Point(x1, y1));
        }

        ArrayList<Point> graphPoints2 = new ArrayList<>();
        graphPoints2.clear();
        for (int i = 0; i < second1.size(); i++) {
            int x1 = (int) (i * xScale + padding + labelPadding);
            int y1 = (int) ((getMaxScore() - second1.get(i)) * yScale + padding);
            graphPoints2.add(new Point(x1, y1));
        }


        g2.setColor(Color.WHITE);
        g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);
        g2.setColor(Color.BLACK);


        for (int i = 0; i < numberYDivisions + 1; i++) {
            int x0 = padding + labelPadding;
            int x1 = pointWidth + padding + labelPadding;
            int y0 = getHeight() - ((i * (getHeight() - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
            int y1 = y0;
            if (first1.size() > 0) {
                g2.setColor(gridColor);
                g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
                g2.setColor(Color.BLACK);
                String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + "";
                FontMetrics metrics = g2.getFontMetrics();
                int labelWidth = metrics.stringWidth(yLabel);
                g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
            }
            g2.drawLine(x0, y0, x1, y1);
        }


        g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
        g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);

        Graphics g3 = g2;




        Stroke oldStroke = g2.getStroke();
        g2.setColor(lineColor);
        g2.setStroke(GRAPH_STROKE);

        System.out.println(graphPoints.size());
        for (int i = 0; i < graphPoints.size() - 1; i++) {

            int x1 = graphPoints.get(i).x;
            int y1 = graphPoints.get(i).y;
            int x2 = graphPoints.get(i + 1).x;
            int y2 = graphPoints.get(i + 1).y;
            g2.drawLine(x1, y1, x2, y2);

        }
        g3.setColor(Color.PINK);
        for (int i = 0; i < graphPoints2.size() - 1; i++) {

            int x1 = graphPoints2.get(i).x;
            int y1 = graphPoints2.get(i).y;
            int x2 = graphPoints2.get(i + 1).x;
            int y2 = graphPoints2.get(i + 1).y;
            g3.drawLine(x1, y1, x2, y2);
        }

        g2.setStroke(oldStroke);
        g2.setColor(pointColor);
        for (int i = 0; i < graphPoints.size(); i++) {
            int x = graphPoints.get(i).x - pointWidth / 2;
            int y = graphPoints.get(i).y - pointWidth / 2;
            int ovalW = pointWidth;
            int ovalH = pointWidth;
            g2.fillOval(x, y, ovalW, ovalH);
        }
        for (int i = 0; i < graphPoints2.size(); i++) {
            int x = graphPoints2.get(i).x - pointWidth / 2;
            int y = graphPoints2.get(i).y - pointWidth / 2;
            int ovalW = pointWidth;
            int ovalH = pointWidth;
            g3.fillOval(x, y, ovalW, ovalH);
        }
    }

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, heigth);
}
private double getMinScore() {
    double minScore =0;
    if (Collections.min(first1)<=Collections.max(second1)){

        minScore= Collections.min(first1)-10;
    }
    else{
        minScore = Collections.min(second1)-10;
    }
    return minScore;
}

private double getMaxScore() {
    double maxScore =0;
    if (Collections.max(first1)>=Collections.max(second1)){

        maxScore= Collections.max(first1)+10;
    }
    else{
        maxScore = Collections.max(second1)+10;
    }
    return maxScore;
}

对于代码墙,我无法确定哪些是相关的,哪些不是。

1 个答案:

答案 0 :(得分:0)

这里的主要线索是重复的消息。 paintComponent被调用了很多次,或者你有一堆组件被绘制。从动作监听器中的这个块看起来像后者:

if(testx != null)
  testx.removeAll();

testx = new GraphPanel(first,second);
addItem(Mainpanel, testx, 0,20, 20,20, GridBagConstraints.SOUTH);

它会移除testx中的所有子项,然后创建一个新项目并将其添加到Mainpanel。它没有做的是从testx删除旧Mainpanel,以便他们重建。