如何更新jframe?

时间:2015-03-24 00:05:09

标签: java swing graph jframe frame

我正在尝试创建这个程序来显示二次函数(带X²的函数),同时能够更改函数的变量。 功能:ax²+ k。 x是for循环中的整数; 我改变了“a”和“k”。 当我尝试更新框架的内容时,它会创建一个新的内容。 稍后,我将创建一个关键的监听器,这样我就可以将“a”和“k”上下改变1。

我的问题是:如何在不创建新框架的情况下更新框架?

这是我的代码(完整但没有keylisteners)

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import java.util.Scanner;

@SuppressWarnings("serial")
public class Function1 extends JPanel {
    // Settings of the frame and graph
    private static final int MAX_SCORE = 20;
    private static final int PREF_W = 600;
    private static final int PREF_H = 500;
    private static final int BORDER_GAP = 30;
    private static final Color GRAPH_COLOR = Color.GREEN;
    private static final Stroke GRAPH_STROKE = new BasicStroke(1);
    private static final int GRAPH_POINT_WIDTH = 10;
    private static final int Y_HATCH_CNT = 44;

// values im trying to change
private static int vK;
private static int vA;

// my function
private List<Integer> scores;

public Function1(List<Integer> scores) {
    this.scores = scores;
}
// Rendering graph
@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 * BORDER_GAP)
            / (scores.size() - 1);
    double yScale = ((double) getHeight() - 2 * BORDER_GAP)
            / (MAX_SCORE - 1);

    List<Point> graphPoints = new ArrayList<Point>();
    for (int i = 0; i < scores.size(); i++) {
        int x1 = (int) (i * xScale + BORDER_GAP);
        int y1 = (int) ((MAX_SCORE - scores.get(i)) * yScale + BORDER_GAP);
        graphPoints.add(new Point(x1, y1));
    }

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

    // create x and y axes
    g2.drawLine(getWidth() / 2, getHeight() - BORDER_GAP, getWidth() / 2,
            BORDER_GAP); // y axe
    g2.drawLine(BORDER_GAP, getHeight() / 2, getWidth() - BORDER_GAP,
            getHeight() / 2); // x axe

    // create hatch marks for y axis.
    for (int i = 0; i <= Y_HATCH_CNT; i++) {
        int x0 = getWidth() / 2 - GRAPH_POINT_WIDTH / 2;
        int x1 = getWidth() / 2 + GRAPH_POINT_WIDTH / 2;
        int y0 = BORDER_GAP + (i * 10);
        int y1 = y0;
        g2.drawLine(x0, y0, x1, y1);
    }

    // now for x axis.
    for (int i = 0; i <= Y_HATCH_CNT; i++) {
        int x0 = getWidth()
                - ((i * (getWidth() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
        ;
        int x1 = x0;
        int y0 = getHeight() / 2 - GRAPH_POINT_WIDTH / 2;
        int y1 = y0 + GRAPH_POINT_WIDTH;
        g2.drawLine(x0, y0, x1, y1);
    }

    // drawing the lines connecting the dots
    g2.setColor(GRAPH_COLOR);
    g2.setStroke(GRAPH_STROKE);
    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);
    }

}

// the getter for my Quadratic Function ax² + k where x is valuex
public static int getValues(int valuex) {
    int valuexf = (int) Math.pow(valuex, 2);
    int result = vA * valuexf + vK;
    return result;
}

// making the frame more flexible
@Override
public Dimension getPreferredSize() {
    return new Dimension(PREF_W, PREF_H);
}

// creating the object im putting on the frame (get values() is the value's
// getter)
private static void createAndShowGui() {
    List<Integer> scores = new ArrayList<Integer>();
    int maxDataPoints = 16;
    for (int i = -20; i < maxDataPoints; i++) {
        scores.add(getValues(i));
    }
    Function1 mainPanel = new Function1(scores);

    // Final seetings and creting the frame of the frame
    JFrame frame = new JFrame("Software by JPorto Enterprises");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

@SuppressWarnings("resource")
public static void main(String[] args) {
    while (true) {

        // create Scanner for values
        Scanner insert = new Scanner(System.in);

        // value for a
        System.out.println("a =");
        int insertedA = insert.nextInt();
        setvA(insertedA);

        // value for k
        System.out.println("k =");
        int insertedK = insert.nextInt();
        setvK(insertedK);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

// setters vor values a and k

public static void setvA(int vA) {
    Function1.vA = vA;
}

public static void setvK(int vK) {
    Function1.vK = vK;
}

}

这是插入a和k的值后的结果: https://www.dropbox.com/s/9nn7ha5g8yiuv8l/Captura%20de%20Ecr%C3%A3%20%282%29.png?dl=0 你能帮忙吗? 我15岁,这样做是一个爱好者。如果您在代码中发现任何错误,请轻松上手;)

0 个答案:

没有答案