Java 我的重绘()会';当我在另一节课上叫它时,它就不工作了 RePrimeType()方法在GUI类的开头和结尾都很好,但是当我在另一个类中在程序中间重新调用时,它不起作用。我已经搜索和尝试了将近一个星期了。我之前问过同样的问题,但我猜其中一位管理员

Java 我的重绘()会';当我在另一节课上叫它时,它就不工作了 RePrimeType()方法在GUI类的开头和结尾都很好,但是当我在另一个类中在程序中间重新调用时,它不起作用。我已经搜索和尝试了将近一个星期了。我之前问过同样的问题,但我猜其中一位管理员,java,user-interface,paintcomponent,runnable,repaint,Java,User Interface,Paintcomponent,Runnable,Repaint,我的重绘()会';当我在另一节课上叫它时,它就不工作了 RePrimeType()方法在GUI类的开头和结尾都很好,但是当我在另一个类中在程序中间重新调用时,它不起作用。我已经搜索和尝试了将近一个星期了。我之前问过同样的问题,但我猜其中一位管理员让我在我做了他喜欢的事情后发布整个程序。这就是我的整个计划,希望能得到一些帮助 package TEST_DiningPhilosophersproblem; import java.awt.Color; import java.awt.Dim

我的重绘()会';当我在另一节课上叫它时,它就不工作了

RePrimeType()方法在GUI类的开头和结尾都很好,但是当我在另一个类中在程序中间重新调用时,它不起作用。我已经搜索和尝试了将近一个星期了。我之前问过同样的问题,但我猜其中一位管理员让我在我做了他喜欢的事情后发布整个程序。这就是我的整个计划,希望能得到一些帮助

package TEST_DiningPhilosophersproblem;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class DiningPhilosopherProblem {
      // Makes the code more readable.
    //===================================================================================================================
    //================================================ChopStick class==================================================================
    //===================================================================================================================
      public static class ChopStick {
        // Make sure only one philosopher can have me at any time.
        Lock up = new ReentrantLock();
        // Who I am.
        private final int id;

        public ChopStick(int id) {
          this.id = id;
        }

        public boolean pickUp(Philosopher who, String where) throws InterruptedException {
          if (up.tryLock(10, TimeUnit.MILLISECONDS)) {
            System.out.println(who + " picked up " + where + " " + this);
            return true;
          }
          return false;
        }

        public void putDown(Philosopher who, String name) {
          up.unlock();
          System.out.println(who + " put down " + name + " " + this);
        }

        @Override
        public String toString() {
          return "Chopstick-" + id;
        }
      }




    public static Graphics g;

      // One philosoper.
    //===================================================================================================================
    //================================================Philosopher class==================================================================
    //===================================================================================================================
      public static class Philosopher implements Runnable {
        // Which one I am.
        private final int id;
        // The chopsticks on either side of me.
        private final ChopStick leftChopStick;
        private final ChopStick rightChopStick;
        // Am I full?
        volatile boolean isTummyFull = false;
        // To randomize eat/Think time
        private Random randomGenerator = new Random();
        // Number of times I was able to eat.
        private int noOfTurnsToEat = 0;
        private GUI y=new GUI();

        private int noOfTurnsToThink = 0;
        private int noOfTurnsToHungry = 0;

        /**
         * **
         *
         * @param id Philosopher number
         *
         * @param leftChopStick
         * @param rightChopStick
         */

        public Philosopher(int id, ChopStick leftChopStick, ChopStick rightChopStick) {
          this.id = id;
          this.leftChopStick = leftChopStick;
          this.rightChopStick = rightChopStick;

        }
    //    GUI GU = new GUI();

        @Override
        public void run() {


          try {
            while (!isTummyFull) {
              // Think for a bit.
             // TDPP.start();
            //  table.start();
              think();
             // GU.Test_exit();

              System.out.println("think()  "+T);
              // Make the mechanism obvious.
              hungry();
              System.out.println("hungry()   "+T);

              if (leftChopStick.pickUp(this, "left")) {
                  //asus++;
                if (rightChopStick.pickUp(this, "right")) {
                  // Eat some.
                  eat();
                  // Finished.
                  rightChopStick.putDown(this, "right");
                }
                // Finished.
                leftChopStick.putDown(this, "left");
              }
            }
           // TDPP.exit();
          } catch (Exception e) {
            // Catch the exception outside the loop.
            e.printStackTrace();
          }
        }

        private void think() throws InterruptedException {

          System.out.println(this + " is thinking");
        //  System.out.println(this);
          Philosopher s= this;
          String g= s.toString();
          // Thread.sleep(randomGenerator.nextInt(1000));
          //!!!!!
          T=0;
          //GU.repaint();
          noOfTurnsToThink++;
          if(g.equals("Philosopher-0") ){
          asus= asus + noOfTurnsToThink;
          }
          Thread.sleep(randomGenerator.nextInt(10));
          y.rrepaint();
        }

        //hungry
        private void hungry() throws InterruptedException {
            T=1;
            //GU.paintComponent(g);
            //GU.Test_exit();
              System.out.println(this + " is hungry");
              // Thread.sleep(randomGenerator.nextInt(1000));
              //!!!!!!
              noOfTurnsToHungry++;
              Thread.sleep(randomGenerator.nextInt(10));
            y.rrepaint();
            }
        //end

        private void eat() throws InterruptedException {
          System.out.println(this + " is eating");
          noOfTurnsToEat++;
          Thread.sleep(randomGenerator.nextInt(10));
        }

        // Accessors at the end.
        public int getNoOfTurnsToEat() {
          return noOfTurnsToEat;
        }
        //!!!!!!!!
        public int getNoOfTurnsToThink() {
              return noOfTurnsToThink;
            }

        public int getNoOfTurnsToHungry() {
              return noOfTurnsToHungry;
            }
        //!!!!!!!!

        @Override
        public String toString() {
          return "Philosopher-" + id;
        }
      }

    //===================================================================================================================
    //================================================GUI class==================================================================
    //===================================================================================================================

      public static class GUI extends JPanel {
            private final Dimension PANEL_SIZE = new Dimension(500,500);
            private final Color BACKGROUND_COLOR = Color.white;
            private JPanel colorPanel = new JPanel();
            private JLabel label = new JLabel();

            ImageIcon redDevil = new ImageIcon("C:\\Users\\LNU\\Documents\\Java_2013\\1DV012OperatingSystems\\src\\DiningPhilosophersproblem\\pic\\red_Devil.gif");
            ImageIcon green_devil = new ImageIcon("C:\\Users\\LNU\\Documents\\Java_2013\\1DV012OperatingSystems\\src\\DiningPhilosophersproblem\\pic\\green_devil.gif");


            public GUI() {
                setPreferredSize(PANEL_SIZE);
                setBackground(BACKGROUND_COLOR);

                /* Color panel */

                /* Create the button panel */
                JPanel buttonSubPanel = createButtonPanel();
                add(buttonSubPanel);

            }



            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
            //  g=pp;
                if(T == 0)
                    redDevil.paintIcon(this, g, 0, 0);
                if(T== 1)
                    green_devil.paintIcon(this, g, 220, 220);
                //System.out.println("!!  "+T);
                //repaint();
                rrepaint();
            }

            private JPanel createButtonPanel() {
                int panelWidth = PANEL_SIZE.width;
                int panelHeight = PANEL_SIZE.height/10;
                //Dimension  panelSize = new Dimension(panelWidth,panelHeight);
                JPanel buttonSubPanel = new JPanel();
                //buttonSubPanel.setPreferredSize(panelSize);

                //buttonSubPanel.setBackground(Color.blue);

                /* Create and add the button */
                JButton ButtonStart = new JButton("START");
                buttonSubPanel.add(ButtonStart);
                JButton ButtonExit = new JButton("EXIT");
                buttonSubPanel.add(ButtonExit);

                /* Add an event handler */
                //ButtonStart.addActionListener(new ButtonListener());
                ButtonListener listener = new ButtonListener(ButtonStart,ButtonExit);
                ButtonStart.addActionListener(listener);
                ButtonExit.addActionListener(listener);

                return buttonSubPanel;
            }

            public void rrepaint(){
                repaint();
            }
            /*
             * Event handler for the button
             */
            private class ButtonListener implements ActionListener {
                //private final Random random = new Random();
        private JButton Start, Exit;

                public ButtonListener(JButton S, JButton E) {
                    Start = S;
                    Exit = E;
                }

                public void actionPerformed(ActionEvent event) {
                    //TheDiningPhilosophersProblem TDPP = new TheDiningPhilosophersProblem();
                    if (event.getSource() == Start) {




                         // public static void main(String args[]) {
                          ExecutorService executorService = null;
                          Philosopher[] philosophers = null;

                           int outnow = 0;

                         // public void start(){



                            try {

                              philosophers = new Philosopher[NO_OF_PHILOSOPHER];

                              //As many forks as Philosophers
                              ChopStick[] chopSticks = new ChopStick[NO_OF_PHILOSOPHER];
                              // Cannot do this as it will fill the whole array with the SAME chopstick.
                              //Arrays.fill(chopSticks, new ReentrantLock());

                              for (int i = 0; i < NO_OF_PHILOSOPHER; i++) {
                                chopSticks[i] = new ChopStick(i);
                              }

                              executorService = Executors.newFixedThreadPool(NO_OF_PHILOSOPHER);

                              for (int i = 0; i < NO_OF_PHILOSOPHER; i++) {
                                philosophers[i] = new Philosopher(i, chopSticks[i], chopSticks[(i + 1) % NO_OF_PHILOSOPHER]);
                                executorService.execute(philosophers[i]);
                              }
                              // Main thread sleeps till time of simulation

                              try {
                                  System.out.println("====================================SIMULATION_MILLIS==========================================================");

                                  Thread.sleep(SIMULATION_MILLIS);
                                 // Thread.sleep((Long) null);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                              // Stop all philosophers.
                              for (Philosopher philosopher : philosophers) {
                                philosopher.isTummyFull = true;
                              }
                              System.out.println("====================================Stop all philosophers==========================================================");

                            }//end try

                            finally {

                              // Close everything down.
                              executorService.shutdown();

                              // Wait for all thread to finish
                              while (!executorService.isTerminated()) {
                                  //Thread.sleep(1000);
                                try {
                                    Thread.sleep(10);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                              }
                              System.out.println("asus = "+asus);
                              for (Philosopher philosopher : philosophers) {
                                  System.out.println(philosopher);
                                  String PH = philosopher.toString();
                                  if(PH.equals("Philosopher-0"))
                              System.out.println(philosopher + " avreg "+(double) asus/philosopher.getNoOfTurnsToThink());
                              }
                              // Time for check
                              for (Philosopher philosopher : philosophers) {
                                System.out.println(philosopher + " => No of Turns to Eat =" + philosopher.getNoOfTurnsToEat());
                              }
                              //!!!!!!!!
                              for (Philosopher philosopher : philosophers) {
                                    System.out.println(philosopher + " => No of Turns to think =" + philosopher.getNoOfTurnsToThink());
                                  }

                              for (Philosopher philosopher : philosophers) {
                                    System.out.println(philosopher + " => No of Turns to Hungry =" + philosopher.getNoOfTurnsToHungry());
                                  }
                              //!!!!!!!!
                            }
                          }


                    //}
                     if (event.getSource() == Exit) {   // Clear-button

                         System.exit(0);
                        //TDPP.exit();
                    }
                }
            }
        }




    //===================================================================================================================
    //================================================Main class==================================================================
    //===================================================================================================================


      // How many to test with.
      private static final int NO_OF_PHILOSOPHER = 5;
      //private static final int SIMULATION_MILLIS = 1000 * 60 * 8;
    //  private static final int SIMULATION_MILLIS = 10 * 10;
      private static int asus=0;
      private static int T=0;
      private static Random random = new Random();
    private  final static int SIMULATION_MILLIS =  random.nextInt(100)*10;
      public static void main(String args[]) throws InterruptedException {


          JFrame frame = new JFrame("Random Colours");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(100,100);

            GUI panel= new GUI();

            frame.getContentPane().add(panel);
            //frame.getContentPane().validate();
            //frame.getContentPane().repaint();
            frame.pack();
            frame.setVisible(true);


      }
    }
package TEST\u ding哲学问题;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.TimeUnit;
导入java.util.concurrent.locks.Lock;
导入java.util.concurrent.locks.ReentrantLock;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共课教学哲学问题{
//使代码更具可读性。
//===================================================================================================================
//===================================================================================================筷子课==================================================================
//===================================================================================================================
公共静态类筷子{
//确保在任何时候只有一个哲学家可以拥有我。
锁定=新的可重入锁定();
//我是谁。
私有最终int id;
公共筷子(国际标识){
this.id=id;
}
公共布尔拾取(哲学家,字符串where)抛出InterruptedException{
if(向上tryLock(10,时间单位毫秒)){
System.out.println(who+“pick”+where+“”+this);
返回true;
}
返回false;
}
公共无效放下(哲学家,字符串名称){
up.unlock();
System.out.println(who+“放下”+姓名+“”+此项);
}
@凌驾
公共字符串toString(){
返回“筷子-”+id;
}
}
公共静态图形;
//一位哲学家。
//===================================================================================================================
//========================================================================================================================哲学家课程==================================================================
//===================================================================================================================
公共静态类实现Runnable{
//我是哪一个。
私有最终int id;
//我两边的筷子。
私人最后筷子左筷子;
私人最后筷子右筷子;
//我吃饱了吗?
易失性布尔值isTummyFull=false;
//随机化进食/思考时间
私有随机生成器=新随机();
//我能吃东西的次数。
private int noOfTurnsToEat=0;
私有GUI y=新GUI();
private int noOfTurnsToThink=0;
private int noofturnstourning=0;
/**
* **
*
*@param id编号
*
*@param leftchostick
*@param right筷子
*/
公共哲学家(int id,筷子左筷子,筷子右筷子){
this.id=id;
this.leftchostick=leftchostick;
this.rightboostick=rightboostick;
}
//GUI GU=新GUI();
@凌驾
公开募捐{
试一试{
而(!isTummyFull){
//想一想。
//TDPP.start();
//table.start();
思考();
//GU.Test_exit();
System.out.println(“think()”+T);
//使机制变得明显。
饥饿();
System.out.println(“饥饿()”+T);
if(leftchostick.pick(这个“左”)){
//华硕++;
如果(右筷子拾音器(此“右”)){
//吃点。
吃();
//完成了。
右筷子。放下(这个“右”);
}
//完成了。
左筷子。放下(这个“左”);
}
}
//TDPP.exit();
}捕获(例外e){
//捕获循环外部的异常。
e、 printStackTrace();
}
}
private void think()抛出InterruptedException{
System.out.println(这个+“正在思考”);
//System.out.println(本文件);
哲学家s=这一点;
字符串g=s.toString();
//sleep(randomGenerator.nextInt(1000));
//!!!!!
T=0;
//GU.repaint();
noOfTurnsToThink++;
如果(g.equals(“哲学家-0”)){
华硕=华硕+正午思维;
}
sleep(randomGenerator.nextInt(10));
y、 rrepaint();
}
//饥肠辘辘
private void hunger()抛出InterruptedException{
T=1;
//GU.组分(g);
//GU.Test_exit();
System.out.println(这个+“饿了”);
//sleep(randomGenerator.nextInt(1000));
//!!!!!!
Noofturnstohunger++;
sleep(randomGenerator.nextInt(10));
y、 rrepaint();
}
//结束
私有void eat()抛出InterruptedException{
System.out.println(这个+“正在吃”);
noOfTurnsToEat++;
sleep(randomGenerator.nextInt(10));
}
//交流电
View v= new View();
v.getMyPanel().repaint();       //returns the panel and repaint it
v.getMyPanel().revalidate();    //returns the panel and revalidates it