Grapic输出在java中不起作用

时间:2018-04-18 07:57:55

标签: java swing user-interface graphics

我试图通过扩展JPanel类和使用Graphics方法来绘制一个客户端队列。但是我无法从图形JFrame中获得输出。 这是我想要画的课程。

public class Simulating extends JPanel{

    // Server server;
    //private ArrayList<JPanel> remember=new ArrayList<JPanel>(server.no_list);s
    public Simulating() {}

    protected void colorareComponente(Graphics gr) {
        super.paintComponent(gr);
        int x = 0;
        for (int i = 0; i <Server.listele.length; i++) {
            int y = 0;
            // desenez casele
            deseneazaCasa(gr, x, y,i);
            for (int j = 0; j <Server.listele[i].getSize(); j++) {
                y =y+ 25;
                // desenezClienti
                //  SimulareCoada.cozi[i].getClientCurent(j).getId()
                int id =  Server.listele[i].getTask(j).getId()+ 1;
                deseneazaClient(gr, x, y, id);
            }
            x =x+ 100;
        }

    }
    private void deseneazaCasa(Graphics g, int x, int y,int i) {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 100, 20);
        g.setColor(Color.BLUE);
        g.drawString("Server"+i+1 , x + 20, y + 20);
        this.repaint();
    }

    private void deseneazaClient(Graphics g, int x, int y, int id) {
        g.setColor(Color.CYAN);
        g.fillOval(x, y, 35, 35);
        g.drawString(Integer.toString(id), x + 8, y + 16);
        this.repaint();
    }

    public void rewrite() {
         this.repaint();
    }
}

我正在另一个类的initalizer方法中实现它:

public Simulating sim=new Simulating();
public static   JFrame frm= new JFrame();
frm.setLayout(null);
frm.setBounds(10, 10, 450, 450);
sim.setBounds(10, 10, 200,200);
frm.add(sim);
frm.setTitle("Simulare servere");
frm.setVisible(true);
frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE);

然而,似乎没有任何输出。 我用另一种方法调用sim来重绘帧:

public void run(){
    this.initializeServer();

    for (Server.currentTime = 0; Server.taskCount > 0; Server.currentTime++){
        int TimeHours =calculateTimeHour(currentTime);
        int TimeMinutes = calculateTimeMinute(currentTime);
        writeHourConsole(TimeHours,TimeMinutes);
        writeHourLog(TimeHours,TimeMinutes);
        this.Log.setLog(keepingLog);
        this.Log.repaint();
        int val=0;
        if(val==0) {
            for (int i = 0; i < taskurile.size(); i++){
                if (taskurile.get(i).getGettingTime() == Server.currentTime){
                    int index = taskurile.get(i).chooseLista(this);
                    this.listele\[index\].addTaskLast(taskurile.get(i));            
                    System.out.println("\n Taskul " + i + " a fost preluat la " + decimal.format(TimeHours) + ":" + decimal.format(TimeMinutes) + "si este procesat pentru " +  taskurile.get(i).getProcessTime());
                    Server.appendLog("\n Taskul " + i + " a fost preluat la " + decimal.format(TimeHours) + ":" + decimal.format(TimeMinutes) + "si este procesat pentru " +  taskurile.get(i).getProcessTime()+ "de serverul " + taskurile.get(i).chooseLista(this));
                    sim.rewrite();
                    this.Log.setLog(keepingLog);
                    this.Log.repaint();
                }
            }
        }
    }
}

这就是我得到的: result

有一个日志面板可以跟踪工作正常的事件。我没有错误或异常消息。

有人可以解释为什么它不起作用吗? 谢谢!

0 个答案:

没有答案