All pastes #283392 Raw Edit

Stuff

public java v1 · immutable
#283392 ·published 2006-12-18 09:13 UTC
rendered paste body
// package threads;import java.awt.*;import java.lang.Long;/** * ThreadGraphicOutput is a class that manages the graphical output * of the buffer from class ThreadBuffer. It interprets the values * of the buffer as the length of a filled rectangle and prints the * rectangles on the screen. * * @author    Frank Drewek * @author    Juergen Schneider * @see       ThreadTA7 * @see       FileGraphicProdThread * @see       ThreadBuffer * @see       Buffer  */public class ThreadGraphicOutput extends Canvas {   ////////////////////////////////////////////////////////////////////////////   // attributes   ////////////////////////////////////////////////////////////////////////////   /** Object that contains the outer iteration-number. */   private ThreadBuffer threadBuffer;      /** object for double buffering */   private Image offscreenImage;      /** Object that contains the graphics. It is used for double buffering. */   private Graphics offscreenGraph;   /** Object that contains the panel. */   private Frame frame;      /** Object that contains the graphics. */   private Panel panel;      /** width of the canvas */   private int width;      /** height of the canvas */   private int height;      /** white color */   static final Color WHITE = Color.white;      /** red color */   static final Color RED = Color.red;   /** state is true if the object for double buffering is initialized. */   private boolean state, clock;      String[] progress, clocks = new String[4];      ////////////////////////////////////////////////////////////////////////////   // constructors   ////////////////////////////////////////////////////////////////////////////   /**    * The constructor ThreadGraphicOutput initializes the width and height    * of the frame that will be printed on the screen.    * It gets the buffer from which it takes the length for the rectangles.    * It creates a frame that shows the graphics on the screen.    * @param b   central object from class ThreadBuffer    * @param wi  width of the canvas    * @param hi  height of the canvas    */   public ThreadGraphicOutput(ThreadBuffer b, int wi, int hi, boolean clock)     {       threadBuffer = b;       this.clock = clock;       width = wi;       height = hi;       setSize(width, height);       setBackground(WHITE);	          state = true;       frame = new Frame("View");       frame.setSize(width+20, height+20);	          panel = new Panel();       panel.setLayout(new GridLayout(1, 1));       panel.add(this);	          frame.add(panel);       frame.setResizable(false);       frame.setVisible(true);      }    public String[] generateClockFields(int t)   {	   int ms = t%1000;	   int sec = (t/1000)%60;	   int min = ((t/1000)/60)%60;	   int h = ((t/1000)/60)/60;	   if(ms <10) clocks[0]= "00"+(new Integer(ms)).toString();	   if(ms>9 && ms<100) clocks[0]="0"+(new Integer(ms)).toString();	   if(ms>99 && ms<1000) clocks[0]=(new Integer(ms)).toString();	   if(sec<10) clocks[1]="0"+(new Integer(sec)).toString();	   else clocks[1]=(new Integer(sec)).toString();	   if(min<10) clocks[2]="0"+(new Integer(min)).toString();	   else clocks[1]=(new Integer(min)).toString();	   if(h<10) clocks[3]="0"+(new Integer(h)).toString();	   else clocks[3]=(new Integer(h)).toString();	   return clocks;   }      public int[] generateCoordinates(int t)   {	   int[] coordinates = new int[2];	   coordinates[0]= 3*(t%400);	   coordinates[1] = 3*(t/400);	   return coordinates;   }   ////////////////////////////////////////////////////////////////////////////   // methods   ////////////////////////////////////////////////////////////////////////////   // access //////////////////////////////////////////////////////////////////   // service /////////////////////////////////////////////////////////////////      /** paint shows the graphics on the screen. */   public synchronized void paint(Graphics g)   {       if (state == true)       {    	   offscreenImage = createImage(width, height);    	   offscreenGraph = offscreenImage.getGraphics();           state = false;       }       for (int i=0; i < threadBuffer.getBuffer(); i++)       {            offscreenGraph.setColor(WHITE);           offscreenGraph.fillRect(0, 0, width, height);           offscreenGraph = offscreenImage.getGraphics();    	   if(!clock)    	   {    		   int[] cord=generateCoordinates(i);    		   offscreenGraph.setColor(Color.green);    		   offscreenGraph.drawOval(cord[0], cord[1], 2, 2);     	   }    	   else    	   {    		   progress=generateClockFields(i);    		   offscreenGraph.setColor(Color.black);    		   offscreenGraph.drawString("ms", 235, 20);    		   offscreenGraph.drawString("sec", 170, 20);    		   offscreenGraph.drawString("min", 120, 20);    		   offscreenGraph.drawString("h", 75, 20);    		   offscreenGraph.drawLine(60, 30, 280, 30);    		   offscreenGraph.drawLine(60, 30, 60, 70);    		   offscreenGraph.drawLine(60, 70, 280, 70);    		   offscreenGraph.drawLine(280, 30, 280, 70);    		   offscreenGraph.drawLine(200, 30, 200, 70);    		   offscreenGraph.drawLine(150, 30, 150, 70);    		   offscreenGraph.drawLine(100, 30, 100, 70);    		   offscreenGraph.drawString(progress[0], 260, 60);    		   offscreenGraph.drawString(progress[1], 160, 60);    		   offscreenGraph.drawString(progress[2], 110, 60);    		   offscreenGraph.drawString(progress[3], 70, 60);    		   offscreenGraph.setColor(Color.white);    	   }       }       g.drawImage(offscreenImage, 0, 0, this);           repaint();     }   /**    * update invokes the paint-method     * @param g    the graphics that will be printed    */   public synchronized void update(Graphics g)     {       paint(g);     }   /**    * refreshView invokes the method update.    * If the buffer is empty the application will end.    */   public synchronized void refreshView()     {       update(getGraphics());       if (threadBuffer.isEmpty() == true)         {	   frame.setVisible(false);	   System.exit(0);         }         else repaint();     }   // debug ///////////////////////////////////////////////////////////////////   /** @deprecated	for debugging purposes */   public final static String revision_GraphicBufferThread =     "$Id: ThreadGraphicOutput.java,v 1.2 1997/10/16 13:52:30 jschneid Exp drewek $";   /** @deprecated	for debugging purposes */   public String getRevision()     { return revision_GraphicBufferThread; }     	 }