有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swing绘图有时很有效

我正在尝试使用JavaSwing和AWT绘制函数。问题并不总是所有的300个点都画出来了。当我在调试模式下循环图的第一个点时,会有更多的变化,图会完全绘制出来。我使用以下代码创建一个JFrame,并将图形对象设置为类成员g

jFrame = new JFrame();
jFrame.setSize(WIDTH, HEIGHT);
jFrame.setVisible(true);
g = jFrame.getContentPane().getGraphics();

然后我为我想要绘制的每个函数调用这个方法

private void drawGraph(IGraph graph, Bounds bounds, Ratios ratios) {
    //contains visual information about the graph
    GraphVisuals visuals = graph.getVisuals();
    g.setColor(visuals.color);
    //the previous point is remembered, to be able to draw a line from one point to the next
    int previousXi = 0;
    int previousYi = 0;
    //a loop over every point of the graph. The graph object contains two arrays: the x values and the y values
    for (int i = 0; i < graph.getSize(); ++i) {
        //calculate the x value using the ratio between the graph's size on the x-axis and the window size and the starting point on the x-axis
        int xi = (int) (ratios.xRatio * (graph.getX(i) - bounds.xMin) + 0.5);
        //analogous for the y axis
        int yi = HEIGHT - (int) (ratios.yRatio * (graph.getY(i) - bounds.yMin) + 0.5);
        //draw
        if (visuals.hasBullets) {
           g.fillOval(xi, yi, visuals.bulletSize, visuals.bulletSize);
        }
        if (visuals.hasLine) {
            if (i != 0) {
                g.drawLine(previousXi, previousYi, xi, yi);
            }
        }
        previousXi = xi;
        previousYi = yi;
    }
}

共 (0) 个答案