当前位置: 代码迷 >> Java Web开发 >> 关于实时动态界面无法刷新,出现白屏的问题
  详细解决方案

关于实时动态界面无法刷新,出现白屏的问题

热度:711   发布时间:2011-03-01 08:22:38.0
关于实时动态界面无法刷新,出现白屏的问题
实质是在做堆排序的但不动态演示。用多线程做过,单线程延时做过,但就是刷新不出来!最小化最大化后Panel里面就会白屏。附项目,动态数据处理主要是在MyPanel类实现!动态效果是从一个坐标先延X在延Y到达另一个坐标进行对排序的动态调整!!望解决 急!
搜索更多相关主题的帖子: 多线程  

----------------解决方案--------------------------------------------------------
程序代码:
package test;
/*
*无休眠 ,单步执行deleteMax操作;
*
*/
public class HeapSort {
    public static void main(String[] args) {
        MyFrame f = new MyFrame();
        MyPanel p = new MyPanel();
        InputPanel i = new InputPanel();
        ShowPanel sen = new ShowPanel();

        f.add(sen);
        f.add(i);
        f.add(p);

    }
}
//*****************************************

package test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;

public class InputPanel extends Panel {

    private static final long serialVersionUID = 1L;

    static int iLen;
    static int[] tempp = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    static String[] temp = { "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "" };
    ArrayList<Integer> a = new ArrayList<Integer>();// 修改为一个数组 写的 ;

    TextField inputText = new TextField("请输入演示数据:");
    TextArea showText = new TextArea();
    TextArea showAns = new TextArea();
    Button s = new Button("构造最大堆");

    public InputPanel() {
        this.setBounds(800, 30, 200, 600);
        this.setLayout(null);
        this.setBackground(new Color(228, 228, 228));
        inputText.setBounds(0, 0, 200, 20);
        showText.setBounds(0, 30, 200, 40);
        showAns.setBounds(0, 100, 200, 40);
        this.add(inputText);
        this.add(showText);
        this.add(showAns);

        inputText.setSize(200, 20);
        inputText.addActionListener(new inputActionListener());
        inputText.addMouseListener(new inputActionListener());
    }

    class inputActionListener extends MouseAdapter implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                inputText = (TextField) e.getSource();
            } catch (NumberFormatException E) {
                E.printStackTrace();
            }

            String s = inputText.getText();
            inputText.removeActionListener(null);
            if (s.endsWith(",")) {
                s = s.substring(0, s.length() - 1);
            }
            String ss = "0," + inputText.getText();
            temp = ss.split(",");
            iLen = temp.length;
            int[] aa = new int[iLen];// 显示最终排序结果
            tempp = new int[iLen];// 直接进行数组 简单 !
            repaint();
            if (iLen > 16) {
                showText.setText("多于16个数 请重新输入:");
                return;
            }
            for (int i = 0; i < iLen; i++) {
                a.add((Integer.parseInt(temp[i])));
                aa[i] = (Integer.parseInt(temp[i]));
            }
            for (int i = 1; i < iLen; i++) {
                tempp[i] = (Integer.parseInt(temp[i]));
            }
            // System.out.println(iLen);
            showText.setText("要排序的数组为:" + s);
            showAns.setText(Sort(aa));
        }

        public void mousePressed(MouseEvent e) {
            inputText.setText("");
        }

        public String Sort(int aa[]) {
            String s = "";
            int len = aa.length;
            for (int i = 0; i < len; i++) {
                for (int k = 0; k < len; k++) {
                    if (aa[i] <= aa[k]) {
                        int tem = aa[i];
                        aa[i] = aa[k];
                        aa[k] = tem;
                    }
                }

            }
            for (int i = 1; i < len; i++) {
                s = s + aa[i] + " ";
            }
            return "最终排序结果是:" + s;
        }

    }

}
//**********************************

package test;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame{
    /**
     *
     
*/
    private static final long serialVersionUID = -5908534022988507382L;

    public MyFrame(){
        //this.setBackground(Color.blue);
        this.setBounds(100, 100, 1009, 639);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                System.exit(0);
            }
        });
        this.setResizable(false);
        this.setLayout(null);
        this.setVisible(true);
        
    }
}
//***********************************


package test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class MyPanel extends Panel {
    /**
     *
     
*/
    private static final long serialVersionUID = -5618379063057043498L;
    static int r1 = 40;
    static int r = 20;
    static int[] o_x = { 78, 391, 260, 521, 195, 325, 456, 586, 156, 227, 297,
            357, 416, 487, 552, 617 };
    static int[] o_y = { 56, 56, 112, 112, 233, 233, 233, 233, 373, 373, 373,
            373, 373, 373, 373, 373 };
    static int[] li_x = { 391, 300, 431, 521, 266, 215, 286, 345, 526, 475,
            546, 605, 215, 176, 215, 247, 351, 312, 351, 377, 475, 436, 475,
            507, 611, 572, 611, 637 };
    static int[] li_y = { 76, 132, 76, 125, 152, 237, 152, 237, 152, 237, 152,
            237, 273, 373, 273, 373, 273, 373, 273, 373, 273, 373, 273, 373,
            273, 373, 273, 373 };
    static int heap[];
    static int CurrentSize, f = 0, x1, y1, x2, y2, x, y, i = 16;

    public MyPanel() {
        Button start = new Button("开始");
        Button makeMH = new Button("构造最大堆");
        Button heapSort = new Button("Sort");
        start.setBounds(700, 40, 50, 20);
        makeMH.setBounds(700, 70, 70, 20);
        heapSort.setBounds(700, 100, 50, 20);
        makeMH.addActionListener(new makeMHAction());
        start.addActionListener(new startAction());
        heapSort.addActionListener(new heapSortAction());
        this.setLayout(null);
        this.add(start);
        this.add(makeMH);
        this.add(heapSort);
        this.setBounds(9, 70, 782, 560);
        this.setBackground(Color.WHITE);
        this.setForeground(Color.RED);

    }

    public void paint(Graphics g) {
        int len = (2 * (InputPanel.temp.length - 1));
        g.setColor(Color.blue);
        int w = this.getWidth();
        int h = this.getHeight();
        g.setColor(Color.BLUE);
        g.drawString("temp", w / 10, h / 10);

        for (int i = 0; i < InputPanel.temp.length; i++) {
            g.setColor(Color.black);
            g.drawOval(o_x[i], o_y[i], r1, r1);
        }

        for (int i = 0; i < len - 2; i += 2) {
            g.drawLine(li_x[i], li_y[i], li_x[i + 1], li_y[i + 1]);
        }

        for (int i = 1; i < InputPanel.temp.length; i++) {
            g.setColor(Color.BLUE);
            g.drawString(InputPanel.tempp[i] + "", o_x[i] + r, o_y[i] + r);
        }
        // 移动所需要的动态图
        g.setColor(Color.RED);
        if (i != 16) {
            g.fillOval(o_x[i], o_y[i], r1, r1);
            g.drawString(InputPanel.tempp[i] + "", o_x[i], o_y[i]);
        }
        g.setColor(Color.BLACK);
        g.drawString(InputPanel.tempp[x1 + 1] + "", o_x[x1] + r, o_y[x1] + r);
        // g.drawString(InputPanel.tempp[x1+1] + "", o_x[x1+1] + r + x,
        
// o_y[y1+1] + r
        
// + y);
        
// g.drawOval(o_x[x1], o_y[x1], r1, r1);// /原结点,可以注释掉 上面已经画了
        
// g.drawOval(o_x[x1] + x, o_y[x1] + y, r1, r1);// x1,y1
        
// g.drawOval(o_x[x2], o_y[x2], r1, r1);// x2,y2// 目标结点 可以注释掉
    }

    class startAction implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            repaint();
        }
    }

    // class CThread extends Thread {
   
//
   
// public void run() {
   
// // while (f == 0) {
   
// while (true) {
   
// repaint();
   
// try {
   
// Thread.sleep(1000);
   
// } catch (InterruptedException e) {
   
// e.printStackTrace();
   
// }
   
// }
   
// // }
   
// }
   
// }

    class makeMHAction implements ActionListener {
        public void actionPerformed(ActionEvent ar) {

            Initialize(InputPanel.tempp, InputPanel.tempp.length - 1);
            repaint();
            System.out.println("CurrentSize:" + CurrentSize + " i:" + i);
        }
    }

    class heapSortAction implements ActionListener {
        public void actionPerformed(ActionEvent arg) {
            if (CurrentSize == 1) {
                JOptionPane.showMessageDialog(null, "排序已经结束", "排序结束",
                        JOptionPane.PLAIN_MESSAGE);
            } else {
                // int len = InputPanel.tempp.length;
                i = CurrentSize;
                // for (int i = 1; i < len; i++) {

                System.out.println("CurrentSize:" + CurrentSize + " i:" + i);
                InputPanel.tempp[CurrentSize] = DeleteMax();

                // try {
               
// Thread.sleep(2000);
               
// System.out.println("wwwww");
               
// } catch (InterruptedException e) {
               
// // TODO Auto-generated catch block
               
// e.printStackTrace();
               
// }
               
// InputPanel.tempp[len-i] = t;
            }
            // int size = InputPanel.tempp.length - 1;
            
// int Max = DeleteMax();
            
// InputPanel.tempp[InputPanel.tempp[--size]] = Max;
        }
    }

    // }

    public void preRun(int x1, int y1) {// 传进要移动的位置参数; x1--->x2
        
// System.out.println("kaishi prerun()" + o_x[x1] + " " + o_y[x1]
        
// + " " + o_x[x2] + " " + o_y[x2] + " " + x1 + " " + x2);
        MyPanel.x1 = x1;
        MyPanel.x2 = y1;

        runn();
    }

    public void runn() {// 变换x,y位置动态移动
        MyPanel.x = 0;
        MyPanel.y = 0;
         System.out.println("kaishi run ()");
        boolean flag = true;
        while (flag) {
             System.out.println("run ()");
            repaint();
            System.out.println("repaint"+" x:"+x+" ----y"+y);
            if (x != (o_x[x2] - o_x[x1])) {
                if (o_x[x2] > o_x[x1]) {
                    x += 1;
                } else
                    x -= 1;
            } else if (o_y[x2] - o_y[x1] > 0) {
                y += 1;
            } else {
                if (y == (o_y[x2] - o_y[x1]))
                    flag = false;
                else {

                    if (y > (o_y[x2] - o_y[x1]))
                        y--;
                    else
                        y++;
                }

            }
            if (y == (o_y[x2] - o_y[x1]))
                return;

        
         try {
         Thread.sleep(50);
         } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
    }
    }
    public void Initialize(int a[], int size) {
        heap = a;
        CurrentSize = size;

        for (int i = CurrentSize / 2; i >= 1; i--) {
            int y = heap[i]; // root of subtree
            int c = 2 * i;
            while (c <= CurrentSize) {
                if (c < CurrentSize && heap[c] < heap[c + 1])
                    c++;

                if (y >= heap[c])
                    break;

                heap[c / 2] = heap[c];
                c *= 2;
            }
            heap[c / 2] = y;
        }
    }

    public int DeleteMax() {
        // movingThread t = new movingThread();
        if (CurrentSize == 0) {
            System.out.println("error");
        }
        System.out.println(CurrentSize + "size");
        int x = InputPanel.tempp[1];
        int i = 1, ci = 2;
        int y = InputPanel.tempp[CurrentSize--];// 最后一个元素先去temp位置------需要动态移动
        
// "一步"
        preRun(CurrentSize, 0);// 移动 同上
        while (ci <= CurrentSize) {
            if (ci < CurrentSize
                    && InputPanel.tempp[ci] < InputPanel.tempp[ci + 1])
                ci++;// ci应是i的较大孩子;

            if (y >= InputPanel.tempp[ci])// 如果最后一个数>=ci,则推出,自然将其放至i的位置;
                break;
            // 如果最后一个数小于heap[ci];
            InputPanel.tempp[i] = InputPanel.tempp[ci];// 将较大的孩子移至上一个根-------需要动态移动
            
// "一步"

            preRun(ci, i);// i移动至ci同上

            i = ci;// 则将根移至ci
            ci *= 2;// 根的孩子移至2*ci;
        }
        InputPanel.tempp[i] = y;// 最后一个元素移至i的位置---------------------------需要动态移动
        
// "一步"
        preRun(0, i);
        // 最后一个元素从temp位置移至位置i;
        return x;
    }

}///*********************


package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Panel;

public class ShowPanel extends Panel {
    public ShowPanel() {
        Label l = new Label("堆排序算法演示");
        Font font = new Font("Times-Roman", Font.BOLD, 20);
        l.setBackground(Color.BLUE);
        l.setForeground(Color.LIGHT_GRAY);
        l.setFont(font);
        this.setBounds(8, 30, 200, 40);
        this.add(l);
        this.setVisible(true);

    }

}



上面附件是包!

----------------解决方案--------------------------------------------------------
程序代码:
public void repaint()
    {
        update(this.getGraphics());
        paint(this.getGraphics());
    }
重写repaint方法!
添加此代码就可以实时刷新了啊!

已成功解决! 
----------------解决方案--------------------------------------------------------
  相关解决方案