开发者

Why it doesn't draw an animated curve?

This is my class which I found on the Internet. It was originally an applet but I don't want to use it as an applet so I changed some methods (such as init() to a constructor).

However, it doesn't work. Would you please help me?

SignInFrame Frame:

public class SignInFrame extends javax.swing.JFrame {

Panel panel;

/** Creates new form SignInFrame */
public SignInFrame() {
    initComponents();
}


public void init() {
    getContentPane().add(panel = new Panel());

}

public void start() {
    panel.start();
}

public void stop() {
    panel.stop();
}
 public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new SignInFrame().setVisible(true);
        }
    });
}}

Panel Dialog:

package ClientGUI;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;

/**
 *
 * @author ICC
 */

public class Panel extends javax.swing.JPanel implements Runnable{


private Thread thread;
    private BufferedImage bimg;
    private static final int NUMPTS = 6;

    //  solid line stoke
    protected BasicStroke solid = new BasicStroke(10.0f,
                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
    //  dashed line stroke
    protected BasicStroke dashed = new BasicStroke(10.0f,
       BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] {5}, 0);
    private float animpts[] = new float[NUMPTS * 2];
    private float deltas[] = new float[NUMPTS * 2];
    protected Paint fillPaint, drawPaint;

    // indicates whether or not to fill shape
    protected boolean doFill = true;

    // indicates whether or not to draw shape
    protected boolean doDraw = true;
    protected GradientPaint gradient;
    protected BasicStroke stroke;


    public Panel() {
        setBackground(Color.white);
        gradient = new GradientPaint(0,0,Color.red,200,200,Color.yellow);
        fillPaint = gradient;
        drawPaint = Color.blue;
        stroke = solid;

    }
           // generates new points for the path
    public void animate(float[] pts, float[] deltas, int i, int limit) {
        float newpt = pts[i] + deltas[i];
        if (newpt <= 0) {
            newpt = -newpt;
            deltas[i] = (float) (Math.random() * 4.0 + 2.0);
        } else if (newpt >= (float) limit) {
            newpt = 2.0f * limit - newpt;
            deltas[i] = - (float) (Math.random() * 4.0 + 2.0);
        }
        pts[i] = newpt;
    }


    /*
     * generates random points with the specified surface width
     * and height for the path
     */
    public void reset(int w, int h) {
        for (int i = 0; i < animpts.length; i += 2) {
            animpts[i + 0] = (float) (Math.random() * w);
            animpts[i + 1] = (float) (Math.random() * h);
            deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
            deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
            if (animpts[i + 0] > w / 2.0f) {
                deltas[i + 0] = -deltas[i + 0];
            }
            if (animpts[i + 1] > h / 2.0f) {
                deltas[i + 1] = -deltas[i + 1];
            }
        }
        gradient = new GradientPaint(0,0,Color.red,w*.7f,h*.7f,Color.yellow);
    }


    // calls animate for every point in animpts
    public void step(int w, int h) {
        for (int i = 0; i < animpts.length; i += 2) {
            animate(animpts, deltas, i + 0, w);
            animate(animpts, deltas, i + 1, h);
        }
    }


    // sets the points of the path and draws and fills the path
    public void drawDemo(int w, int h, Graphics2D g2) {
        float[] ctrlpts = animpts;
        int len = ctrlpts.length;
        float prevx = ctrlpts[len - 2];
        float prevy = ctrlpts[len - 1];
        float curx = ctrlpts[0];
        float cury = ctrlpts[1];
        float midx = (curx + prevx) / 2.0f;
        float midy = (cury + prevy) / 2.0f;
        GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
        gp.moveTo(midx, midy);
        for (int i = 2; i <= ctrlpts.length; i += 2) {
            float x1 = (midx + curx) / 2.0f;
            float y1 = (midy + cury) / 2.0f;
            prevx = curx;
            prevy = cury;
            if (i < ctrlpts.length) {
                curx = ctrlpts[i + 0];
                cury = ctrlpts[i + 1];
            } else {
                curx = ctrlpts[0];
                cury = ctrlpts[1];
            }
            midx = (curx + prevx) / 2.0f;
            midy = (cury + prevy) / 2.0f;
            float x2 = (prevx + midx) / 2.0f;
            float y2 = (prevy + midy) / 2.0f;
            gp.curveTo(x1, y1, x2, y2, midx, midy);
        }
        gp.closePath();
        if (doDraw) {
            g2.setPaint(drawPaint);
            g2.setStroke(stroke);
            g2.draw(gp);
        }
        if (doFill) {
        开发者_Python百科    if (fillPaint instanceof GradientPaint) {
                fillPaint = gradient;
            }
            g2.setPaint(fillPaint);
            g2.fill(gp);
        }
    }


    public Graphics2D createGraphics2D(int w, int h) {
        Graphics2D g2 = null;
        if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
            bimg = (BufferedImage) createImage(w, h);
            reset(w, h);
        }
        g2 = bimg.createGraphics();
        g2.setBackground(getBackground());
        g2.clearRect(0, 0, w, h);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        return g2;
    }


    public void paint(Graphics g) {
        Dimension d = getSize();
        step(d.width, d.height);
        Graphics2D g2 = createGraphics2D(d.width, d.height);
        drawDemo(d.width, d.height, g2);
        g2.dispose();
        if (bimg != null)  {
            g.drawImage(bimg, 0, 0, this);
        }
    }


    public void start() {
        thread = new Thread(this);
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
    }


    public synchronized void stop() {
        thread = null;
    }


    public void run() {
        Thread me = Thread.currentThread();
        while (thread == me) {
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) { break; }
        }
        thread = null;
    }
     public static void main(String argv[]) {
    SignInFrame n = new SignInFrame();

    n.start();
}}


In your SignInFrame constructor, you call initComponents(), but that does not exist. I think you mean to call init(). Also your JFrame does not have a size set, when I ran this under linux (Java 1.6), it worked but was tiny, you should add a setSize call.

Try it with these edits:

public class SignInFrame extends javax.swing.JFrame {

    Panel panel;

    /** Creates new form SignInFrame */
    public SignInFrame() {
        setSize (600,600);
        init();
    }


    public void init() {
        getContentPane().add(panel = new Panel());
        start();
    }

    public void start() {
        panel.start();
    }

    public void stop() {
        panel.stop();
    }

     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SignInFrame().setVisible(true);
            }
        });
     }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜