});
//设置菜单栏
menuBar.add(fileMenu);
fileMenu.addSeparator();
fileMenu.add(exitItem);
//将菜单栏添加到主框架
mainFrame.setJMenuBar(menuBar);
}
public void showTool()
{
//绘图区面板的实现
drawPanel=new MyPanel();
drawPanel.setBackground(Color.BLACK);
drawPanel.setSize(WIDTH/4*3,HEIGHT/5*3);
//按边界布局方式将绘图区面板放在CENTER部分
Container contentPane = mainFrame.getContentPane();
contentPane.add(drawPanel, BorderLayout.CENTER);
//成绩列表工具条的实现
lineTool=new JToolBar();
lineTool.setSize(WIDTH/4,HEIGHT/5*3);
contentPane.add(lineTool, BorderLayout.EAST);
setLinePanel=new JPanel();
setLinePanel.setSize(WIDTH/5,HEIGHT/5*4);
setLinePanel.setBorder(BorderFactory.createTitledBorder(\成绩列表\
setLinePanel.setLayout(new GridLayout(8,1));
setLinePanel.add(new JLabel(\
setLinePanel.add(new JLabel(\
setLinePanel.add(new JLabel(\
lineTool.add(setLinePanel);
//当前状态工具条的实现
stateTool=new JToolBar();
stateTool.setSize(WIDTH,HEIGHT/5);
//按边界布局方式将当前状态工具条放在SOUTH部分
contentPane.add(stateTool, BorderLayout.SOUTH);
info=new JLabel(\当前状态:\
stateTool.add(info);
}
public static void main(String args[])
{
Game g=new Game();
g.showPaint();
} }
//定义画图的基本图形单元,基本图形单元为正方形
class Drawings implements Serializable//基本图形单元,用到串行化接口,保存时所用 {
public int x,y; //定义坐标属性
public int R,G,B; //定义色彩属性
public int width; //定义线条粗细属性
public Drawings(){}
public void copyData(Drawings s)//拷贝基本图元数据
{
x=s.x; y=s.y; R=s.R; G=s.G; B=s.B; width=s.width;
} }
class MyPanel extends JPanel {
private int lineWidth=1;//线宽,初值为1象素
private Color lineColor=Color.BLACK;//画笔颜色,初值为黑色
private Drawings[] itemList; //用来存放绘制过的基本图元的数组
private int index=-1;//已绘制基本图元下标,初值为-1表示没有绘图
public MyPanel()
{
//定义基本图元数组为一个较大的数组
itemList=new Drawings[20000];
index++;
itemList[index]=new Drawings();
itemList[index].x=20;
itemList[index].y=20;
itemList[index].R=255;
itemList[index].G=0;
itemList[index].B=0;
itemList[index].width=19;
index++;
itemList[index]=new Drawings();
itemList[index].x=20;
itemList[index].y=40;
itemList[index].R=255;
itemList[index].G=0;
itemList[index].B=0;
itemList[index].width=19;
index++;
itemList[index]=new Drawings();
itemList[index].x=40;
itemList[index].y=20;
itemList[index].R=255;
itemList[index].G=0;
itemList[index].B=0;
itemList[index].width=19;
index++;
itemList[index]=new Drawings();
itemList[index].x=40;
itemList[index].y=40;
itemList[index].R=255;
itemList[index].G=0;
itemList[index].B=0;
itemList[index].width=19;
}
public void setLineWidth(int w)//设置线宽
{
if(w>0) lineWidth=w;
}
public void setLineColor(Color c)//设置画笔颜色