滚动浏览JPanel会清除其图形

时间:2012-06-11 08:35:13

标签: java swing awt jscrollpane java-2d

我是Java编程的初学者,我遇到了问题。我在JPanel中包含的JScrollPane中使用了JFrame

我正在使用paintComponent()方法绘制某条曲线并且已正确完成。问题是,当我滚动浏览我的面板时,我看到图像正在被清除。我已经搜索并了解了闪烁,但我不确定它的含义以及这是否是我遇到的问题。我还注意到,当调用重绘时,图像被清除。

这是我的代码,如果我做错了什么,任何人都可以告诉我吗?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.QuadCurve2D;
import javax.swing.JFrame;
import java.lang.Math;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class DrawCurve extends JPanel {

    class MyAdjustmentListener implements AdjustmentListener {

        public MyAdjustmentListener() {
        }

        public void adjustmentValueChanged(AdjustmentEvent evt) {
            setFocusable(true);
            jScroll.setFocusable(false);
            //repaint();
            //revalidate();
        }
    }

    Graphics gr;
    Stroke drawingStroke = new BasicStroke(0.5f);
    double x;
    int y = 0;
    static String seq = "AAGTCGACCTGTAGCTAGATCGGATCATAGCTCGATCCAGAGATT";
    QuadCurve2D curve;
    char s;
    int a = 0;
    int c = 0;
    int g = 0;
    int t = 0;
    int af = 0;
    int cf = 0;
    int gf = 0;
    int tf = 0;
    int h = 0;
    int flag = 0;
    final JScrollPane jScroll = new JScrollPane();
    final JFrame parFrame;
    Vector<Double> xrand = new Vector<Double>();

    public DrawCurve() {
        super();
        parFrame = new JFrame();
        parFrame.pack();
        jScroll.setFocusable(false);
        setFocusable(true);
        parFrame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        this.setBackground(Color.WHITE);
        jScroll.setViewportView(this);
        jScroll.getHorizontalScrollBar().addAdjustmentListener(new MyAdjustmentListener());
        jScroll.getVerticalScrollBar().addAdjustmentListener(new MyAdjustmentListener());
        if (checkSequence(seq) == 0) {
            jScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            jScroll.setViewportBorder(new LineBorder(Color.WHITE));
            setFocusable(true);
            setPreferredSize(new Dimension(1000, 300));
            setBackground(Color.magenta);
            parFrame.add(jScroll, BorderLayout.CENTER);
            parFrame.setSize(600, 320);
            parFrame.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "invalid input", "Warning!", JOptionPane.PLAIN_MESSAGE);
        }
    }

    public int checkSequence(String sequ) {
        int f = 0;
        for (int i = 0; i < sequ.length(); i++) {
            if (sequ.charAt(i) != "A".charAt(0) && sequ.charAt(i) != "C".charAt(0) && sequ.charAt(i) != "G".charAt(0) && sequ.charAt(i) != "T".charAt(0)) {
                f = 1;
                break;
            }
            xrand.add(Math.random() * 300 - 200);
        }
        return f;
    }

    public void paintComponent(Graphics gr) {
        super.paintComponent(gr);
        Graphics2D ga = (Graphics2D) gr;
        System.out.println("in");
        ga.setStroke(drawingStroke);
        for (int i = 0; i < seq.length(); i++) {
            s = seq.charAt(i);
            if (s == "A".charAt(0)) {
                ga.setColor(Color.RED);
                a = 1;
                af = 1;
                cf = 0;
                gf = 0;
                tf = 0;
                h = -1;
            } else if (s == "C".charAt(0)) {
                ga.setColor(Color.YELLOW);
                c = 1;
                af = 0;
                cf = 1;
                gf = 0;
                tf = 0;
                h = -3;
            } else if (s == "G".charAt(0)) {
                ga.setColor(Color.GREEN);
                g = 1;
                af = 0;
                cf = 0;
                gf = 1;
                tf = 0;
                h = 1;
            } else if (s == "T".charAt(0)) {
                ga.setColor(Color.BLUE);
                t = 1;
                af = 0;
                cf = 0;
                gf = 0;
                tf = 1;
                h = 3;
            } else {
                af = 0;
                cf = 0;
                gf = 0;
                tf = 0;
                h = 0;
            }
            x = Math.random() * 300 - 200;
            curve = new QuadCurve2D.Double(y, 250 + h, y + 10, xrand.elementAt(i), y + 20, 250 + h);
            ga.draw(curve);
            if (a == 1 && af == 0) {
                ga.setColor(Color.RED);
                h = -1;
                ga.drawLine(y, 250 + h, y + 20, 250 + h);
            }
            if (c == 1 && cf == 0) {
                ga.setColor(Color.YELLOW);
                h = -3;
                ga.drawLine(y, 250 + h, y + 20, 250 + h);
            }
            if (g == 1 && gf == 0) {
                ga.setColor(Color.GREEN);
                h = 1;
                ga.drawLine(y, 250 + h, y + 20, 250 + h);
            }
            if (t == 1 && tf == 0) {
                ga.setColor(Color.BLUE);
                h = 3;
                ga.drawLine(y, 250 + h, y + 20, 250 + h);
            }
            y += 20;

        }
    }

    public static void main(String[] args) {
        final DrawCurve panel = new DrawCurve();        
    }
}

1 个答案:

答案 0 :(得分:0)

我弄清楚该代码有什么问题,结果发现在我的循环中我忘记初始化每次调用paintComponent时曲线应该从哪里开始。加y = 0;在paintComponent的开头会解决它