📄 project1.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.geom.*;
public class project1
{
public static void main(String []args) //entry of the program
{
proframe frame=new proframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show(); //show the frame
}
}
class proframe extends JFrame //main frame
{
public proframe()
{
this.setSize(850,500); //set size and property
this.setLocation(100,100);
this.setTitle("Scheduling ");
Container contentpanel=this.getContentPane();
r_panel=new radiopanel();
d_panel=new demonstration(r_panel);
p_panel=new processpanel(d_panel);
v_panel=new versionpanel();
int i;
int []temp=new int[4];
for(i=0;i<4;i++)
temp[i]=p_panel.get_value(i);
d_panel.set_burst(temp);
contentpanel.add(r_panel,"West"); //add radio panel
contentpanel.add(p_panel,"East"); //add process slider panel
contentpanel.add(v_panel,"South"); //add version panel
contentpanel.add(d_panel,"Center"); //add demonstration panel
}
private radiopanel r_panel;
private demonstration d_panel;
private processpanel p_panel;
private versionpanel v_panel;
}
class demonstration extends JPanel
{
public demonstration(int burst[],radiopanel r_panel)
{
int i;
for(i=0;i<4;i++)
this.burst[i]=burst[i];
this.create_textfield();
this.r_panel=r_panel;
}
public demonstration(radiopanel r_panel)
{
int i;
for(i=0;i<4;i++)
this.burst[i]=0;
this.create_textfield();
this.r_panel=r_panel;
}
public void create_textfield()
{
this.ave_turnaround.setEditable(false);
this.ave_waiting.setEditable(false);
Box hbox1=Box.createHorizontalBox();
hbox1.add(new JLabel("ave turnaround "));
hbox1.add(this.ave_turnaround);
Box hbox2=Box.createHorizontalBox();
hbox2.add(new JLabel("ave waiting "));
hbox2.add(this.ave_waiting);
Box vbox=Box.createVerticalBox();
vbox.add(hbox1);
vbox.add(Box.createVerticalStrut(5));
vbox.add(hbox2);
this.add(vbox);
}
public void set_burst(int i,int value)
{
this.burst[i]=value;
repaint();
}
public void set_burst(int burst[])
{
int i;
for(i=0;i<4;i++)
this.burst[i]=burst[i];
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
Rectangle2D rect=new Rectangle2D.Double();
int i;
for(i=0;i<4;i++)
{
rect.setFrame(LEFT_X+HOR_OFFSET*i,TOP_Y,WIDTH,this.burst[i]*BASE);
g2.draw(rect);
}
if(ready)
{
for(i=0;i<4;i++)
{
rect.setFrame(LEFT_X+HOR_OFFSET*i,TOP_Y,WIDTH,(this.burst[i]-process[i].getTime())*BASE);
g2.setPaint(Color.GREEN);
g2.fill(rect);
// this.setfield();
}
}
}
public boolean get_ready()
{
return this.ready;
}
public void areyouready()
{
this.ready=!ready;
if(ready)
{
q=new Queue(10);
int i;
for(i=0;i<4;i++)
{
process[i]=new proc(this.burst[i],"process"+Integer.toString(i));
q.enqueue(process[i]);
}
cpuobj=new cpu(q,this,this.r_panel);
cpuobj.start();
}
}
public void setfield()
{
this.ave_turnaround.setText(Double.toString(cpuobj.get_ave_turnaround()));
this.ave_waiting.setText(Double.toString(cpuobj.get_ave_waiting()));
validate();
}
private int []burst=new int[4];
private boolean ready=false;
public static final int LEFT_X=100;
public static final int TOP_Y=100;
public static final int WIDTH=20;
public static final int HOR_OFFSET=50;
public static final int BASE=3;
private radiopanel r_panel;
private Queue q;
private proc process[]=new proc[4];
cpu cpuobj;
private JTextField ave_turnaround=new JTextField("0",10);
private JTextField ave_waiting=new JTextField("0",10);
}
class versionpanel extends JPanel //version panel
{
public versionpanel()
{
// Border b=BorderFactory.createEtchedBorder(Color.BLUE,Color.RED);
// this.setBorder(b);
add(new JLabel("Made by jiangjun"),"East");
}
}
class radiopanel extends JPanel //radio panel
{
public radiopanel()
{
Border b=BorderFactory.createEtchedBorder(Color.BLUE,Color.RED);
this.setBorder(b);
roundrobin=new JRadioButton("round robin",false);
fifo=new JRadioButton("fifo",false);
sjf=new JRadioButton("sjf",false);
roundrobin.addActionListener(new algorithm_listener());
fifo.addActionListener(new algorithm_listener());
sjf.addActionListener(new algorithm_listener());
Box hbox1=Box.createVerticalBox();
hbox1.add(roundrobin);
hbox1.add(fifo);
hbox1.add(sjf);
group =new ButtonGroup();
group.add(roundrobin);
group.add(fifo);
group.add(sjf);
add(hbox1);
}
private class algorithm_listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JRadioButton source=(JRadioButton)event.getSource();
//source.getText();
JOptionPane.showMessageDialog(null,source.getText());
algorithm=source.getText();
}
}
public void judge_button()
{
if(algorithm.equals("round robin"))
choice=ROUNDROBIN;
else if(algorithm.equals("fifo"))
choice=FIFO;
else if(algorithm.equals("sjf"))
choice=SJF;
}
public int get_choice()
{
judge_button();
// JOptionPane.showMessageDialog(null,Integer.toString(choice));
return this.choice;
}
private JRadioButton roundrobin;
private JRadioButton fifo;
private JRadioButton sjf;
private ButtonGroup group;
private String algorithm="";
private int choice=-1;
public static final int ROUNDROBIN=0;
public static final int FIFO=1;
public static final int SJF=2;
}
class processpanel extends JPanel //slider panel
{
public processpanel(demonstration d_panel)
{
this.d_panel=d_panel;
Border b=BorderFactory.createEtchedBorder(Color.BLUE,Color.RED);
this.setBorder(b);
int i;
Box vbox=Box.createVerticalBox();
JLabel label=new JLabel("CPU BURST TIME");
vbox.add(label);
for(i=0;i<4;i++)
{
this.makeslider(i);
vbox.add(this.panel[i]);
}
vbox.add(Box.createVerticalStrut(20));
this.okbutton.addActionListener(new buttonlistener());
vbox.add(okbutton);
vbox.add(Box.createVerticalStrut(20));
vbox.add(Box.createVerticalStrut(20));
vbox.add(new JLabel("We assume that 4 processes"));
vbox.add(new JLabel("arrive at the same time"));
vbox.add(new JLabel("in the order of p0,p1,p2,p3"));
vbox.add(Box.createVerticalStrut(20));
ready_label=new JLabel("");
vbox.add(ready_label);
this.bigpanel.add(vbox);
this.add(this.bigpanel);
}
private class buttonlistener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
d_panel.areyouready();
if(d_panel.get_ready())
{
ready_label.setText("ready");
}
else
ready_label.setText("not ready");
}
}
private class slider_listener implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
JSlider source=(JSlider)event.getSource();
int i=Integer.parseInt(source.getName());
textfield[i].setText(Integer.toString(source.getValue()));
d_panel.set_burst(i,source.getValue());
}
}
public void makeslider(final int i)
{
p[i]=new JSlider();
p[i].setName(Integer.toString(i));//set the name of the sliders :0,1,2,3
p[i].setPaintTicks(true);
p[i].setPaintLabels(true);
p[i].setMajorTickSpacing(20);
p[i].setMinorTickSpacing(5);
p[i].addChangeListener(new slider_listener());
panel[i]=new JPanel();
String description="process"+Integer.toString(i);
panel[i].add(new JLabel(description));
panel[i].add(p[i]);
textfield[i]=new JTextField("",4);
textfield[i].setEditable(false);
panel[i].add(textfield[i]);
}
public int get_value(int i)
{
if(i>=4 || i<0)
return -1;
else //0,1,2,3
return p[i].getValue();
}
private JSlider []p=new JSlider[4];//p0,p1,p2,p3
private JPanel []panel=new JPanel[4];
private JTextField []textfield=new JTextField[4];
private JButton okbutton=new JButton("ok");
private JPanel bigpanel=new JPanel();
private demonstration d_panel;
private JLabel ready_label;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -