⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui.java

📁 Reader Writer Problem to solve synchronization issues
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;import java.lang.*;public class Gui extends Canvas{   private Image[] RWimg;   private Image Currentimg;   private int Currentpos[][]= new int[5][2];   private int tn,xpos,ypos,prevxpos,prevypos,scale=10;   private int rcval=0,writeval=1;   private String r[]={"R1>","R2>","R3>"};   private String move[]={"HF","VF","HL","VL"};   private int init[][]={{5,5},{65,5},{35,40},{425,35},{460,35}};   private int mutexdb[][]={{5,160},{65,160},{35,160},{415,170},{415,210}};   private int mutexright[][]={{200,175},{200,210},{200,245}};   private int datapos[][]={{185,5},{250,5},{315,5},{425,5},{460,5}};   private int dbasepos[][]={{220,82},{260,82},{300,82},{260,82},{260,82}};   private int mutexbottom[]={35,247};   private int dbbottom[]={375,247};   private int dbtop[]={375,140};   private int dbleft[]={335,190};   private int db[]={370,180};   private int mutex[]={25,200};   private int readval[]={0,0,0,0,0};   private int dbase[]={150,35};   private int rcpos[]={dbase[0]-40,dbase[1]+40};   private int dbsize[]={250,95};   private int writebox[]={dbase[0]+105,dbase[1]+20};   private boolean blinkerflag=false;   private Image mutexlock, mutexunlock;   private Image dblock, dbunlock;   private Image Current_mutexlock,Current_dblock;   private Image textbox;  public Gui(Image[] img,Image[] other) {    Panel p=new Panel();    p.setLayout(new BorderLayout());    RWimg=img;	mutexunlock= other[0];	mutexlock=other[1]; 	dbunlock=other[2];	dblock=other[3]; 	textbox=other[4];	Current_mutexlock=mutexunlock;	Current_dblock=dbunlock;    Currentimg=RWimg[0];     for(int i=0;i<5;i++)	{ 		Currentpos[i][0]=init[i][0];		Currentpos[i][1]=init[i][1];	} }public void reset(){    Current_mutexlock=mutexunlock;    Current_dblock=dbunlock;	blinkerflag=false;   for(int i=0; i<4;i++) readval[i]=0;    rcval=0;writeval=1;     Currentimg=RWimg[0];     for(int i=0;i<5;i++)        {                Currentpos[i][0]=init[i][0];                Currentpos[i][1]=init[i][1];        }	repaint();} public void paint(Graphics g) {   int n=RWimg.length;    for (int i=0;i<n;i++)   	g.drawImage(RWimg[i],Currentpos[i][0],Currentpos[i][1],this);   g.drawImage(Current_mutexlock,mutex[0],mutex[1],this);   g.drawImage(Current_dblock,db[0],db[1],this);   g.setColor(getBackground());   g.draw3DRect(dbase[0],dbase[1],dbsize[0],dbsize[1],false);   g.setColor(getForeground());   g.setFont(new Font("Helvetica",Font.BOLD,14));   g.drawString("Database",dbase[0]+100,dbase[1]+15);    g.drawImage(textbox,writebox[0],writebox[1],this);    g.drawString(String.valueOf(writeval),writebox[0]+15,writebox[1]+18);    g.setFont(new Font("Helvetica",Font.BOLD,12));    g.drawImage(textbox,rcpos[0],rcpos[1],this);    g.drawString("RC",rcpos[0]+12,rcpos[1]-3);    g.setFont(new Font("Helvetica",Font.BOLD,14));    g.drawString(String.valueOf(rcval),rcpos[0]+15,rcpos[1]+18);    g.setFont(new Font("Helvetica",Font.BOLD,12));    g.drawString("MUTEX",mutex[0]+5,mutex[1]+42);    g.setFont(new Font("Helvetica",Font.BOLD,12));    g.drawString("DB",db[0]+6,db[1]+60);    for (int i=0; i<n; i++){ 	if((i==0)||(i==3))        g.setColor(Color.red);        else if((i==2)||(i==4))        g.setColor(Color.blue);        else        g.setColor(Color.green);	if(i<3){    		g.setFont(new Font("Helvetica",Font.BOLD,12));	 	g.drawString(r[i],datapos[i][0]-25,datapos[i][1]+15);	}        g.drawImage(textbox,datapos[i][0],datapos[i][1],this);    	g.setFont(new Font("Helvetica",Font.BOLD,14));        g.drawString(String.valueOf(readval[i]),				datapos[i][0]+15,datapos[i][1]+18);    }   g.drawLine(0,290,499,290);	 }synchronized void object_move (int x11,int y11,int x14,int y14,String str) {        int x1=x11,x2,x3,x4=x14;	int y1=y11,y2,y3,y4=y14;	int x_value,y_value;        String mov=str;          Graphics g=getGraphics();        g.clearRect(Currentpos[tn][0],Currentpos[tn][1],                     Currentimg.getWidth(this),Currentimg.getHeight(this));        g.dispose();              this.prevxpos=x1;            this.prevypos=y1;          if(mov.equals("VF")){         //Vertical First            x2 = x1;            x3 = x4;            y2 = (y1 + y4) / 2;            y3 = y2; }          else if (mov.equals("HL")){         //Horizontal First	   x3=x14;           x2=x3;           y2=y1;           y3=y14;}         else if(mov.equals("VL")){         //vertical L first           x2=x1;	   y3=y14;           y2=y3;           x3=x14; }          else 	 {            x2 = (x1+x4)/2;            x3 = x2;            y2 = y1;            y3 = y4; 	  } /*======================================*/        if ((x1==x2) && (y1<y2))        {            this.xpos = x1;            this.ypos = y1;            top_to_bottom(y1,y2);        }        else if ((x1==x2) && (y1>y2))        {            this.xpos = x1;            this.ypos = y1;            bottom_to_top(y1,y2);        }         else if ((x1<x2) && (y1 == y2))        {            this.xpos = x1;            this.ypos=y1;            left_to_right (x1,x2);        }        else if((x1>x2) && (y1 == y2))        {            this.xpos = x1;            this.ypos=y1;            right_to_left(x1 ,x2);        } /*======================================*/        if ((x2==x3) && (y2<y3))        {            this.xpos = x2;            this.ypos = y2;            top_to_bottom (y2, y3);        }         else if  ((x2==x3) && (y2>y3))        {            this.xpos = x2;            this.ypos = y2;            bottom_to_top (y2, y3);        }         else if ((x2<x3) && (y2==y3))        {            this.xpos = x2;            this.ypos = y2;            left_to_right (x2, x3);        }         else if((x2>x3) && (y2 == y3))        {            this.xpos = x2;            this.ypos = y2;            right_to_left (x2 ,x3);        } /*======================================*/        if ((x3==x4) && (y3<y4))        {         this.xpos = x3;         this.ypos = y3;         top_to_bottom(y3,y4);        }        else if ((x3==x4) && (y3>y4))        {            this.xpos = x3;            this.ypos = y3;            bottom_to_top(y3,y4);        }         else if ((x3<x4) && (y3 == y4))        {            this.xpos = x3;            this.ypos = y3;            left_to_right (x3,x4);        }        else if((x3>x4) && (y3 == y4))        {            this.xpos = x3;            this.ypos = y3;            right_to_left(x3 ,x4);        }    } /*======================================*/    void top_to_bottom (int start, int end){        for(int i = start; i <=end; i+= 10)        {this.prevypos = this.ypos;         this.ypos = i;        pos_to_paint();}        this.prevypos=this.ypos;        this.ypos=end;        pos_to_paint();    }      void bottom_to_top (int start, int end){        for(int i = start; i >=end; i-= 10)        {this.prevypos = this.ypos;         this.ypos = i;        pos_to_paint();}        this.prevypos=this.ypos;        this.ypos=end;        pos_to_paint();    }      void left_to_right (int start, int end){        for(int i = start; i <=end; i+= 10)        {this.prevxpos = this.xpos;         this.xpos = i;        pos_to_paint();}        this.prevxpos=this.xpos;        this.xpos=end;        pos_to_paint();    }      void right_to_left (int start, int end){        for(int i = start; i >=end; i-= 10)        {this.prevxpos = this.xpos;        this.xpos = i;        pos_to_paint();}        this.prevxpos=this.xpos;        this.xpos=end;        pos_to_paint();    }     void pos_to_paint(){        guipaint();        pause (550 - (50*scale));    }    void pause (int time){        try{Thread.sleep(time);}        catch(InterruptedException e) {}    }    public void guipaint() {	Graphics g=getGraphics();	g.clearRect(prevxpos,prevypos,		Currentimg.getWidth(this),Currentimg.getHeight(this));	Currentpos[tn][0]=xpos;	Currentpos[tn][1]=ypos;        g.drawImage(Currentimg,xpos, ypos, this);	   	g.setColor(getBackground());   	g.draw3DRect(dbase[0],dbase[1],dbsize[0],dbsize[1],false);   	g.setColor(getForeground());	this.prevxpos=this.xpos;	this.prevypos=this.ypos;	g.dispose();    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -