2016-12-19 10 views
0

애플릿을 사용하여 기본 플로터를 프로그래밍하려고합니다. 애플릿을 구체적으로 사용해야합니다.애플릿 AWT 캔버스가 그래픽을 업데이트하지 않음

폴트의 경우 별도의 캔버스를 만들었지 만 해결할 수없는 문제가 발생했습니다. 처음으로 그래프를 그릴 때 멋지게 그려집니다. 그러나 캔버스가 나중에 제대로 다시 그려지지 않습니다. 디버깅 화면에서 repaint() 메서드가 호출되고 paint()가 호출되지만 그래픽이 업데이트되지 않습니다. 사전에

public class MyCanvas extends Canvas{ 
int w,h; //width and height 
int samples; 
ArrayList<Double> eqValues = new ArrayList<>(); 
MyCanvas(int wi, int he) //constructor 
{ w=wi; h=he; 
    setSize(w,h); //determine size of canvas 
    samples=wi-20; 
} 
public void paint(Graphics g) 
{ 
    int y0=0, y1; //previous and new function value 
    g.setColor(Color.yellow); 
    g.fillRect(0,0,w,h); //clear canvas 
    g.setColor(Color.black); 
    if (eqValues.size()>0) { // draw new graph 
     for (int t = 1; t <= samples; t = t + 1) { 
      y1 = eqValues.get(t).intValue(); 
      g.drawLine(10 + t - 1, h - y0, 10 + t, h - y1); 
      y0 = y1; 
     } 
    } 
    System.out.println("Repainted"); 
    /*g.drawLine(10,10,10,h-10); //y-axis 
    g.drawLine(10,h/2,w-10,h/2); //x-axis 
    g.drawString("P",w-12,h/2+15); 
    g.drawString("P/2",w/2-13,h/2+15); 
    g.drawLine(w-10,h/2-2,w-10,h/2+2); //horizontal marks 
    g.drawLine(w/2, h/2-2,w/2, h/2+2);*/ 
} 

public void drawSine(double amp, double xCoef, double phase){ 
    for (int j=0;j<=samples;j++){ 
     eqValues.add(amp*Math.sin(xCoef*Math.PI*j/samples + Math.PI*phase/180)+0.5+h/2); 
    } 
    repaint(); 
    System.out.println("Got sine vals"); 
} 

public void drawFOeq(double sc, double fc){ 
    for (int j=0;j<=samples;j++){ 
     eqValues.add(sc*j+fc); 
    } 
    repaint(); 
    System.out.println("Got FO eq vals"); 
} 
} 

감사 : 여기

코드입니다!

답변

1

ArrayList에 값을 추가 할 때 문제가 발생합니다. 즉, ArrayList에 이미 값을 추가 한 후 (add (Double) 메서드 사용) 문제가 발생합니다.

public void drawSine(double amp, double xCoef, double phase) { 
    eqValues.clear(); //this clear the ArrayList 
    ...... 
    repaint(); 
    ...... 
} 

public void drawFOeq(double sc, double fc){ 
    eqValues.clear(); //this clear the ArrayList 
    ...... 
    repaint(); 
    ...... 
} 

당신이 만들 수있는 여러 기능을 플롯하려면 : 당신은 단지 줄거리를 지우고 새로 추가하기 전에 새로운 함수 값의 ArrayList에있는 분명() 방법을 사용 그리려는 경우 다른 ArrayList 또는 ArrayList의 모든 점 (예 : java.awt.Point)에 저장하는 것이 더 좋습니다.

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.util.ArrayList; 

public class MyCanvas extends Canvas { 

    int w, h; // width and height 
    int samples; 
    ArrayList<Point> eqValues = new ArrayList<>();    //Point constructor receives 2 int arguments: x and y; however, his methods getX() and getY() return double values 

    // constructor 
    MyCanvas(int wi, int he) { 
     w = wi; 
     h = he; 
     setSize(w, h);           // determine size of canvas 
     samples = wi - 20; 
    } 

    public void paint(Graphics g) { 
     int x1, y0, y1;           // previous and new function value 

     g.setColor(Color.yellow); 
     g.fillRect(0, 0, w, h);         // clear canvas 

     g.setColor(Color.black); 
     if (eqValues.size() > 0) {        // draw new graph 
      y0 = (int) Math.round(eqValues.get(0).getY());  // first line must start at the first point, not at 0,h 
      for (Point p : eqValues) {       // iterates over the ArrayList 
       x1 = (int) Math.round(p.getX()); 
       y1 = (int) Math.round(p.getY()); 
       g.drawLine(10 + x1 - 1, h - y0, 10 + x1, h - y1); 
       y0 = y1; 
      } 
     } 

     System.out.println("Repainted"); 

    } 

    public void drawSine(double amp, double xCoef, double phase) { 
     for (int j = 0; j <= samples; j++) { 
      eqValues.add(new Point(j, (int) Math 
        .round(amp * Math.sin(xCoef * Math.PI * j/samples + Math.PI * phase/180) + 0.5 + h/2))); 
     } 
     repaint(); 
     System.out.println("Got sine vals"); 
    } 

    public void drawFOeq(double sc, double fc) { 
     for (int j = 0; j <= samples; j++) { 
      eqValues.add(new Point(j, (int) Math.round(sc * j + fc))); 
     } 
     repaint(); 
     System.out.println("Got FO eq vals"); 
    } 
} 
+0

감사합니다. –