원시 라인 그래픽 기능 Java가있는 애니메이션 라인 그래프

StackOverflow https://stackoverflow.com/questions/1603820

  •  05-07-2019
  •  | 
  •  

문제

애플릿과 Java에 대해 더 많이 배우기 위해 선 (드로우 라인)을 그리며 애니메이션 라인 그래프를 만들어 웨이브 애플릿 만들기를 실험하고 있습니다.

정적 그래프를 잘 만들 수 있습니다. 그러나 그래프의 애니메이션 측면으로 어려움을 겪고 있습니다. 그래프의 축은 왼쪽에서 오른쪽으로 이동하여 0보다 커지고 증가해야합니다.

내 문제는 내 요구를 해결책으로 번역하는 것입니다. 누구든지 내 문제에 대한 조언을 줄 수 있습니까?

특정 지점의 x와 y를 포함하는 점으로 인덱싱 된 다차원 배열이 있습니다. XS가 왼쪽으로 움직이는 것처럼 보이도록 렌더 기능을 수정하려고 시도했지만 제대로 작동하지 않습니다.

내가 어떤 접근을하고 싶습니까? 사용자 조치 나 추가 데이터로 인해 Y의 값이 변경 될 수 있다면 내 접근 방식이 얼마나 다를까요?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

/**
 * A graph that should in the future represent a wave according to my inputs
 * 
 * @authorImprofane
 * @version 1
 */
public class Graph extends JFrame
{
    private InnerGraph inner;

    private int width;
    private Random RNG;
    private int height;
    private int[][] series;

    private int xoffset;
    int prevx = 0;
    int prevy = 0;



    /**
     * Constructor for objects of class Graph
     */
    public Graph(int width, int height) throws InterruptedException
    {
         RNG = new Random();
         setTitle(" Graph");

         series = new int[width][2];

         this.width = width;
         this.height = height;
         inner = new InnerGraph(width, height);

         add(inner, BorderLayout.CENTER);

         setVisible(true);
         inner.preparePaint();
         pack();
         updateGraph();
    }

    public static void main(String[] args) throws InterruptedException
    {
        new Graph(300, 300);
    }

    public void updateGraph() throws InterruptedException
    {   

        // virtual x is how far along on the x axis we are, I ignore the 'real' X axis
        int vx = 0;
        int point = 0;
        int xdecay = 0;
        int inc = 5;

        // how many times we need to plot a point
        int points = (int) java.lang.Math.floor( width / inc );
            System.out.println(points);
            inner.preparePaint();
            // draw all points to graph

            // make some junk data, a saw like graph
            for (vx = 0 ; vx < points ; vx++) {
                series[vx] = new int[] { vx*inc, ( (vx*inc) % 120 ) };
            }

            Thread.sleep(150);
            int n = 5;

        while(n > 0) {
            System.out.println(xdecay);
            inner.preparePaint();
                for (vx = 0 ; vx < points ; vx++) {
                    inner.updateSeries(vx, xdecay);

                    inner.repaint();
                    Thread.sleep(50);
                }
                xdecay += inc;

                // shift the data points to the left
                int[][] nseries = new int[points][2];
                // System.arraycopy(series, 1, nseries, 0, points-1);
                n--;
            }

    }

    public class InnerGraph extends JPanel
    {
        private Graphics g;
        private Image img;

        private int gwidth;
        private int gheight;

        Dimension size;

        public InnerGraph(int width, int height)
        {
            gwidth = width;
            gheight = height;
            size = new Dimension(1, 1);


        }

        /**
         * Try make panel the requested size.
         */
        public Dimension getPreferredSize()
        {
            return new Dimension(gwidth, gheight);
        }

        /**
         * Create an image and graphics context
         * 
         */

        public void preparePaint()
        {          
            size = getSize();
            img = inner.createImage( (size.width | gwidth), (size.height | gheight) );
            g = img.getGraphics();


        }

        /**
         * Draw a point to the chart given the point to use and the decay. 
         * Yes this is bad coding style until I work out the mathematics needed
         * to do what I want.
         */

        public void updateSeries(int point, int decay)
        {
            g.setColor(Color.blue);
            int nx = series[point][0];

            series[point][0] -= decay;
            if ( point-1 >= 0 ) {
                series[point-1][0] -= decay;
            }

            int ny = series[point][1];
            prevx -= decay;

            g.drawLine(prevx-decay, prevy, nx-decay, ny );         
            prevx = nx-decay;
            prevy = ny;
        }


        public void paintComponent(Graphics g)
        {
                    g.drawImage(img, 0, 0, null);
}
    }

}
도움이 되었습니까?

해결책

첫째, 업데이트 서리 방법에서 부패를 너무 자주 빼는 것처럼 보입니다. 아래는 해당 방법의 새로운 버전입니다.

public void updateSeries(int point, int decay)
    {

        g.setColor(Color.blue);
        int nx = series[point][0];

        series[point][0] -= decay;
        if ( point-1 >= 0 ) {
            series[point-1][0] -= decay;
        }

        int ny = series[point][1];
    //    prevx -= decay;

    //    g.drawLine(prevx-decay, prevy, nx-decay, ny );         
    g.drawLine(prevx, prevy, nx-decay, ny );     
        prevx = nx-decay;
        prevy = ny;
    }

이러한 변화로 인해 선이 더 잘 끌리지 만 애니메이션을보기가 어렵습니다. 새로운 그래픽을 구축하고 모든 것을 교환하여 각 개별 라인 세그먼트가 그려지는 것을 볼 필요가 없도록해야합니다.

다른 팁

스타터의 경우 다음 예제 (및 이와 관련된 예제)를 확인할 수 있습니다.

스크롤 차트 @java2s.com

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top