Java绘画问题

时间:2014-03-23 19:49:56

标签: java swing graphics paintcomponent

我试图对我们学校的招生系统进行模拟。但我坚持一个问题,我无法弄清楚为什么,我呈现的框闪烁。我不需要它。你能帮助我吗?感谢。

import java.awt.Color;
import java.awt.*;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates and open the template
 * in the editor.
 */
/**
 *
 * @author paolo
 */
public class simulator extends JPanel {

    Thread tr;
    Image i;
    Graphics gbfr;
    Dimension size;
    int num = 0, check, wid, hyt, oq;
    Random rand = new Random();
    Dimension locs[];
    list waiting, queue;
    boolean allowMove = false;

    simulator(int w, int h) {
        oq = 0;
        wid = w;
        hyt = h;
        tr = new tr1();
        locs = new Dimension[30];
        waiting = new list(50);
        queue = new list(30);
        int tw, u;
        for (u = 0, tw = 64; u < 10; u++, tw += 55) {
            locs[u] = new Dimension(tw, 275);
            locs[19 - u] = new Dimension(tw, 325);
            locs[u + 20] = new Dimension(tw, 375);
        }

    }

    public void paint(Graphics g) {
        i = createImage(getWidth(), getHeight());
        gbfr = i.getGraphics();
        gbfr.setColor(getBackground());
        gbfr.fillRect(0, 0, getWidth(), getHeight());

        gbfr.setColor(getForeground());
        paintComponent(gbfr);

        g.drawImage(i, 0, 0, this);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(new Color(225, 225, 225));
        g.fillRect(0, 0, getWidth(), getHeight());
        drawSettings(g);
        drawStudents(g);

    }

    protected void drawSettings(Graphics g) {
        g.setColor(new Color(225, 225, 225));
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(new Color(200, 200, 200));
        g.fillRect(0, 0, 600, 200);
        g.setColor(Color.black);
        g.drawRect(0, 200, 20, 20);
        g.drawRect(145, 200, 20, 20);
        g.drawRect(290, 200, 20, 20);
        g.drawRect(435, 200, 20, 20);
        g.drawRect(580, 200, 20, 20);
        g.drawRect(0, 0, 600, 200);
        g.setColor(new Color(200, 200, 200));
        g.fillRect(1, 180, 19, 40);
        g.fillRect(146, 180, 19, 40);
        g.fillRect(291, 180, 19, 40);
        g.fillRect(436, 180, 19, 40);
        g.fillRect(581, 180, 19, 40);
        g.setColor(Color.black);
        g.drawString("Cashier 1", 62, 190);
        g.drawString("Cashier 2", 207, 190);
        g.drawString("Cashier 3", 352, 190);
        g.drawString("Cashier 4", 497, 190);

        g.fillRect(20, 270, 560, 20);//
        g.fillRect(20, 320, 560, 20);
        g.fillRect(20, 370, 560, 20);

    }

    protected void drawStudents(Graphics g) {
        waiting.drawStudents(g);
    }

    public class tr1 extends Thread implements Runnable {

        public void run() {
            for (check = 0;; check++) {
                num++;
                try {
                    if (num % 100 == 0 && waiting.max != waiting.countL) {
                        waiting.add();
                    }

                    repaint();
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                }
                //repaint();
            }
        }
    }

    public class list {

        int max, countL;
        students temp, head;

        list() {
            head = new students();
            countL = 0;
            head.setNext(null);
            max = 0;

        }

        list(int m) {

            countL = 0;
            head = new students(1);
            list();
            max = m;
            head.setNext(null);
        }

        void add() {
            students t;
            t = head;
            if (countL == 0) {
                countL++;
                t.setNext(new students());
            } else {
                temp = head.getNext();
                while (temp != null) {
                    temp = temp.getNext();
                }
                countL++;
                head.setNext(new students());
                //updateCount();
            }
        }

        void delete(int c) {
            students t = head, t1;
            int aa = 1;
            if (countL == 1) {
                if (c == 1) {
                    t.setNext(null);
                }
            } else {
                if (c == 1) {
                    t.setNext(t.getNext());
                } else {
                    for (t = t.getNext(); aa < c; aa++, t = t.getNext()) {
                    }
                    t.setNext(t.getNext().getNext());
                }
            }
        }

        void updateCount() {
            students t = new students();
            t = head;
            t = t.getNext();
            System.out.println(countL + "__");
            for (int q = 1; q < countL; q++) {
                System.out.println(t);
                 t.setCount(q);
                t = t.getNext();
            }
        }

        void drawStudents(Graphics g) {
            students t = head;
            if (countL != 0) {
                t = t.getNext();
                while (t != null) {
                    g.setColor(t.getClr());
                    g.fillRect(t.xpos, t.ypos, 10, 10);
                    t = t.getNext();
                }
            }
        }

    }

    public class students {

        int xpos, ypos, year, level, servtime, waittime, pos, destx, desty, count;
        Color clr;
        boolean active, onq;
        students next;

        students() {
            //System.out.print("Recieved");
            xpos = rand.nextInt((wid - 580) + 1) + 580;
            onq = false;
            ypos = rand.nextInt((hyt - 280) + 1) + 280;
            waittime = rand.nextInt((100 - 50) + 1) + 50;
            servtime = rand.nextInt((200 - 100) + 1) + 100;
            count = 0;
            level = 0;
            next = null;
            year = rand.nextInt((4 - 1) + 1) + 1;

            clr = new Color(12, 12, 12);
            if (year == 1) {
                clr = Color.GREEN;
            } else if (year == 2) {
                clr = Color.YELLOW;
            } else if (year == 3) {
                clr = Color.RED;
            } else {
                clr = Color.BLUE;
            }
            pos = 0;
            active = false;
        }

        students(int i) {
            count = 0;
        }

        void setxpos(int x) {
            xpos = x;
        }

        void setypos(int y) {
            ypos = y;
        }

        void setpos(int p) {
            pos = p;
        }

        void setCount(int q) {
            count = q;
        }

        students getNext() {
            return next;
        }

        Color getClr() {
            return clr;
        }

        void setNext(students n) {
            next = n;
        }

        int getpos() {
            return pos;
        }

        boolean isActive() {
            return active;
        }

        void activate() {
            active = true;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 不要覆盖模拟器JPanel的paint(Graphics g)方法
  2. 不要直接调用paintComponent。通过做这两件事你搞乱了所有Swing的图形。
  3. 将稳定的背景图像绘制到BufferedImage,然后在JPanel的paintComponent(Graphics g)方法中绘制图像。
  4. 请注意,您的代码不能为我重现您的问题。我没有看到任何眨眼。是否应该有一些我们没有看到的动画?


    如,

    // class names should begin with an upper-case letter
    public class Simulator extends JPanel {
       // ....
    
       private BufferedImage settings = null; 
    
       Simulator(int w, int h) {
          // ....
    
          settings = createSettings();
    
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(wid, hyt);
       }
    
    //   public void paint(Graphics g) {
    //      i = createImage(getWidth(), getHeight());
    //      gbfr = i.getGraphics();
    //      gbfr.setColor(getBackground());
    //      gbfr.fillRect(0, 0, getWidth(), getHeight());
    //
    //      gbfr.setColor(getForeground());
    //      paintComponent(gbfr);
    //
    //      g.drawImage(i, 0, 0, this);
    //   }
    
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
    
          g.setColor(new Color(225, 225, 225));
          g.fillRect(0, 0, getWidth(), getHeight());
          if (settings != null) {
             g.drawImage(settings, 0, 0, this);
          }
          // !! drawSettings(g);
          drawStudents(g);
    
       }
    
       private BufferedImage createSettings() {
          int width = 700;
          int height = 500;
          BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          Graphics g = img.getGraphics();
    
          g.setColor(new Color(225, 225, 225));
          g.fillRect(0, 0, getWidth(), getHeight());
          g.setColor(new Color(200, 200, 200));
          g.fillRect(0, 0, 600, 200);
          g.setColor(Color.black);
          g.drawRect(0, 200, 20, 20);
          g.drawRect(145, 200, 20, 20);
          g.drawRect(290, 200, 20, 20);
          g.drawRect(435, 200, 20, 20);
          g.drawRect(580, 200, 20, 20);
          g.drawRect(0, 0, 600, 200);
          g.setColor(new Color(200, 200, 200));
          g.fillRect(1, 180, 19, 40);
          g.fillRect(146, 180, 19, 40);
          g.fillRect(291, 180, 19, 40);
          g.fillRect(436, 180, 19, 40);
          g.fillRect(581, 180, 19, 40);
          g.setColor(Color.black);
          g.drawString("Cashier 1", 62, 190);
          g.drawString("Cashier 2", 207, 190);
          g.drawString("Cashier 3", 352, 190);
          g.drawString("Cashier 4", 497, 190);
    
          g.fillRect(20, 270, 560, 20);//
          g.fillRect(20, 320, 560, 20);
          g.fillRect(20, 370, 560, 20);
          g.dispose();
          return img;
       }
    
       // ...
    
    
    }