📄 mainpane.java
字号:
//gy=gtmp.y-4+(ZeroY/Global.LENGTH-gtmp.y)%4;
//int gx=gtmp.x,gy=gtmp.y-3;
//Point pos=myGrid.getPos(gx,gy);
//boolean bool=myGrid.LocateIns(actins,pos.x,pos.y);
boolean bool=myGrid.LocateIns(actins,actins.getX()+Global.LENGTH/2,actins.getY()+Global.LENGTH/2);
//System.out.println(bool);
if(!bool){
actins.setLocation(foreloc);
}
/*else{
//检查actins是否压在其他标签上
if(myGrid.CheckIfOver(actins))
actins.setLocation(foreloc);
Square stmp=myGrid.getIns(actins,e.getX()-picX,e.getY());
if(stmp!=null)
{//如果有其他标签就交换位置
//stmp.setLocation(foreloc);
actins.setLocation(foreloc);
}
}*/
actins.selected=false;
actins=null;
}
repaint(0,0,getWidth(),getHeight());
}
public void MouseDragged(MouseEvent e)
{//鼠标拖拽
if(picked)curpnt.setLocation(e.getX()-picX,e.getY());
else
if(actins!=null){
int x=e.getX()-picX-divsize.width,y=e.getY()-divsize.height;
if(y>ZeroY-4*Global.LENGTH)y=ZeroY-4*Global.LENGTH;
actins.setLocation(x,y);
}
repaint(0,0,getWidth(),getHeight());
}
public void mouseWheelMoved(MouseWheelEvent e)
{//鼠标滚动
if(e.getWheelRotation()>0)
{
scroll.setValue(scroll.getValue()+scroll.getVisibleAmount());
}else
{
scroll.setValue(scroll.getValue()-scroll.getVisibleAmount());
}
picX=-scroll.getValue();
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent e)
{//滚动条事件
picX=-scroll.getValue();
repaint();
}
//功能函数
public void MarkIns(Square[] s,boolean bool)
{//标记数据内内的S
Square stmp[]=myGrid.getAllIns();
for(int i=0;i<stmp.length;i++)
stmp[i].selected=false;
if(s==null)return;
for(int i=0;i<s.length;i++)
s[i].selected = bool;
}
public void MarkIns(Square s,boolean bool)
{//标记单个
Square stmp[]=myGrid.getAllIns();
for(int i=0;i<stmp.length;i++)
stmp[i].selected=false;
if(s==null)return;
s.selected=bool;
}
public void SetEditable(boolean bool)
{//设置编辑状态
isEditable=bool;
editbtn.setEnabled(!bool);
readbtn.setEnabled(bool);
//btnpane.setEnabled(bool);
//if(bool)detailloc=null;
repaint(0,Global.LENGTH,getWidth(),Global.LENGTH);
}
public void CreateNewImage()
{
//新建文件
setEnabled(false);
repaint();
initdlg.setVisible(true);
setEnabled(true);
repaint();
if(initdlg.isReady)
{
int width=initdlg.lenX*Global.LENGTH+Global.START_X;
StartupNew(width,initdlg.style);
this.ShowMsgPane("新工作图像已建立,尺寸"+width+"*"+getHeight()+" pixels",true);
}else
{
this.ShowMsgPane("建立计划取消",true);
}
}
public void ShowImage(BufferedImage image)
{//显示图像
for(int i=0;i<viewer.length;i++)
{
if(!viewer[i].isVisible())
{
viewer[i].setVisible(true);
viewer[i].setImage(image);
return;
}
}
ImageView view=new ImageView(ImageView.SYSDISPOSE);
view.setVisible(true);
view.setImage(image);
}
public void SaveImage(BufferedImage image)
{//保存图像
chooser.setTitle("选择导出位置");
chooser.setMode(FileDialog.SAVE);
chooser.setVisible(true);
String path=chooser.getDirectory()+chooser.getFile();
String form=path.substring(path.length()-4);
if(form.equals(Global.IMAGEFORM)){
try{
ImageIO.write(image,"PNG",new File(path));
this.ShowMsgPane("导出图像操作成功,图像被保存在["+path+"].",true);
}catch(Exception e){
this.ShowMsgPane("导出图像操作失败,请确认文件路径合法性和文件编辑完整性.",true);
}
}else
this.ShowMsgPane("导出图像文件格式应为"+Global.IMAGEFORM+",请确认文件格式正确.",true);
}
public boolean SaveFiles(String path,String content)
{//存储文件
try{
RandomAccessFile writer=new RandomAccessFile(path,"rw");
writer.write(content.getBytes());
writer.close();
return true;
}catch(Exception e){}
return false;
}
public boolean SaveImage(String path,BufferedImage img)
{//保存图像
try{
ImageIO.write(img,"PNG",new File(path));
return true;
}catch(Exception e){}
return false;
}
public void SaveRecord()
{//保存记录
chooser.setTitle("选择保存文件");
chooser.setMode(FileDialog.SAVE);
chooser.setDirectory(Global.OUTPUT_DIR);
chooser.setVisible(true);
File file=new File(chooser.getDirectory()+chooser.getFile());
String form=file.getPath().substring(file.getPath().length()-4);
if(form.equals(Global.RECORD))
{//有找到有效的文件
try{
RandomAccessFile writer=new RandomAccessFile(file,"rw");
writer.write(myGrid.output().getBytes());
writer.close();
this.ShowMsgPane("记录被保存在["+file.getAbsolutePath()+"],文件已保存.",true);
}catch(Exception e){
this.ShowMsgPane("记录保存失败,请检查文件路径有效性以及编辑是否缺损.",true);
}
}
else
this.ShowMsgPane("记录保存格式应为"+Global.RECORD+"请确认文件格式正确.",true);
}
public void LoadRecord()
{//加载记录
chooser.setTitle("选择读取文件");
chooser.setMode(FileDialog.LOAD);
chooser.setDirectory(Global.OUTPUT_DIR);
chooser.setVisible(true);
String path=chooser.getDirectory()+chooser.getFile();
String form=path.substring(path.length()-4);
if(form.equals(Global.RECORD))
{//有找到有效的文件
try{
RandomAccessFile reader=new RandomAccessFile(path,"rw");
byte[] buf=new byte[1024];
StringBuffer strbuf=new StringBuffer();
while((reader.read(buf))>=0)
{
strbuf.append(new String(buf));
}
reader.close();
this.StartupNew(strbuf.toString());
this.ShowMsgPane("读取记录成功,记录文件为["+path+"]",true);
}catch(Exception e){
e.printStackTrace();
this.ShowMsgPane(path+"读取记录失败,请检查文件完整性",true);
}
}else
this.ShowMsgPane("读取记录格式应为"+Global.RECORD+",请确认文件格式正确.",true);
}
public void ExportAll()
{//导出全部
setEnabled(false);
// 开始导出数据
File file=new File(Global.OUTPUT_DIR);
if(!file.exists())
file.mkdir();
Calendar date=Calendar.getInstance();
String filename=""+date.get(Calendar.HOUR_OF_DAY)+date.get(Calendar.MINUTE)+date.get(Calendar.MINUTE);
String dirname=Global.OUTPUT_DIR+"\\"+date.get(Calendar.HOUR_OF_DAY)+date.get(Calendar.MINUTE)+date.get(Calendar.MINUTE)+"\\";
file=new File(dirname);
file.mkdir();
//开始保存
//保存记录
this.SaveFiles(dirname+filename+Global.RECORD,myGrid.output());
//保存计算
conpane.showText(myGrid.outputResult());
this.SaveFiles(dirname+filename+Global.RESAULT,conpane.getConsole());
//导出图像
this.SaveImage(dirname+filename+"-1"+Global.IMAGEFORM,myGrid.outputReportImage());
// 导出图像
this.SaveImage(dirname+filename+"-2"+Global.IMAGEFORM,myGrid.outputReportImage2());
//导出优化图像
this.SaveImage(dirname+filename+"-3"+Global.IMAGEFORM,myGrid.outputDivSample());
this.ShowMsgPane("您的数据已经批量导出至[ "+dirname+filename+" ]下,请注意检查",true);
//
setEnabled(true);
}
public void SaveReport()
{//保存计算结果
String result=myGrid.outputResult();
if(result==null)
{
this.ShowMsgPane("不能产生有效的计算结果,请检查您的工作图像是否有元素.",true);
return;
}
conpane.showText(result);
if(myGrid.size()==0)
{
this.ShowMsgPane("您的工作画面内还没有有效的元素,请编辑后再保存.",true);
return;
}
chooser.setTitle("选择保存文件");
chooser.setMode(FileDialog.SAVE);
chooser.setVisible(true);
//开始保存过程
String path=chooser.getDirectory()+chooser.getFile();
String form=path.substring(path.length()-4);
if(form.equals(Global.RESAULT))
{//有找到有效的文件
try{
RandomAccessFile writer=new RandomAccessFile(path,"rw");
writer.write(conpane.getConsole().getBytes());
writer.close();
this.ShowMsgPane("计算结果被保存在["+path+"],文件已保存.",true);
}catch(Exception e){
this.ShowMsgPane("计算保存失败,请检查文件路径有效性以及编辑是否缺损.",true);
}
}
else
this.ShowMsgPane("计算结果保存格式应为"+Global.RESAULT+",请确认文件格式正确.",true);
}
/**
* 图像处理区
* */
public void initBackImage(int w,int h)
{//初始化背景图像和网格尺寸
//初始化图像
backimage=this.getGraphicsConfiguration().createCompatibleImage(w,h);
Graphics2D g=backimage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//开始绘制
//填充背景
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
//绘制网格
int gridX=w/Global.LENGTH+1,gridY=h/Global.LENGTH,x,y;
g.setColor(Color.LIGHT_GRAY);
for(int i=0;i<gridX;i++)
for(int j=0;j<gridY;j++)
{
y=Global.LENGTH*j;
x=Global.START_X+Global.LENGTH*i;
g.drawLine(x,0,x,h);
g.drawLine(Global.START_X,y,w,y);
if(j==gridY-2 )
{
ZeroY=y;
}
}
g.setColor(Color.GRAY);
for(int i=0;i<gridX;i++)
{
x=Global.START_X+Global.LENGTH*i+3;
g.drawString("t "+i,x,ZeroY+14);
}
//绘制坐标线
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(1.2f));
g.drawLine(Global.START_X,0,Global.START_X,h);
g.drawLine(Global.START_X,ZeroY,w,ZeroY);
g.drawLine(w,ZeroY,w-10,ZeroY-5);
g.drawLine(w,ZeroY,w-10,ZeroY+5);
//绘制边框
// 底部
GradientPaint paint=new GradientPaint(0,ZeroY+Global.LENGTH/2,new Color(255,255,255,60),0,h-Global.LENGTH,Color.WHITE);
g.setPaint(paint);
g.fillRect(0,ZeroY+Global.LENGTH/2,w,h-ZeroY);
// 顶部
paint=new GradientPaint(0,Global.LENGTH*2,new Color(255,255,255,60),0,Global.LENGTH,Color.WHITE);
g.setPaint(paint);
g.fillRect(0,0,w,Global.LENGTH*2);
// 释放资源
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0,22,w,22);
g.dispose();
//初始化网格属性
if(myGrid==null){
myGrid=new Grid(w,ZeroY,this);
myGrid.setStyle(style);
}
scroll.setValues(picX,10,0,w-getWidth());
}
public void update(Graphics g)
{//刷新
//picSize.setSize(getWidth()*2,getHeight());
if(offimage==null || offG==null)
{
offimage=this.getGraphicsConfiguration().createCompatibleImage(picSize.width,picSize.height);
offG=offimage.createGraphics();
offG.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//offG.setFont(new Font(offG.getFont().getFontName(),Font.PLAIN,14));
SetEditable(true);
detailloc=null;
picX=0;
}
if(backimage==null){
initBackImage(picSize.width,picSize.height);
}
offG.drawImage(backimage,0,0,this);
//绘制文字
offG.setColor(Color.BLACK);
offG.drawString("S 空间/总计"+myGrid.size()+"条指令/状态:"+((isEditable)?"可写":"只读"),Global.START_X+2-picX,Global.LENGTH*2-5);
offG.drawString("T 时间",getWidth()-Global.LENGTH*2-picX,ZeroY+Global.LENGTH);
//绘制命令
//myGrid=null;
if(myGrid!=null)
myGrid.paint(offG);
else
offG.drawString("没有生成有效网格",getWidth()/2-42,getHeight()/2-Global.LENGTH);
//绘制活动命令
if(actins!=null)actins.paint(offG);
//绘制选择框
if(picked){
offG.setColor(selecolor);
offG.fillRect(mousepnt.x,mousepnt.y,curpnt.x-mousepnt.x,curpnt.y-mousepnt.y);
offG.setColor(Color.GRAY);
offG.drawRect(mousepnt.x,mousepnt.y,curpnt.x-mousepnt.x,curpnt.y-mousepnt.y);
}
paint(g);
}
public void paint(Graphics g)
{//绘制
if(isbool)
{
isbool=false;
//colorpane
colorpane.setLocation(getWidth(),23);
//rcdpane
rcdpane.setBounds(colorpane.getX(),colorpane.getY()+colorpane.getHeight()+1,220,200);
// conpane
conpane.setBounds(colorpane.getX(),rcdpane.getY()+rcdpane.getHeight()+1
,220,300);
//scroll
scroll.setBounds(1,getHeight()-14,getWidth()-2,12);
//talker
talker.setLocation(father.getX()+getWidth()/2-talker.getWidth()/2
,father.getY()+getHeight()/2-talker.getHeight()/2);
taskdlg.setSize(250,125);
taskdlg.setLocation(father.getX()+getWidth()/2-taskdlg.getWidth()/2,
father.getY()+getHeight()/2-taskdlg.getHeight()/2);
picSize=new Dimension(getWidth(),getHeight());
}
if(offimage==null)
repaint();
g.drawImage(offimage,picX,0,this);
if(!isEnabled())
{
g.setColor(unablecolor);
g.fillRect(0,0,getWidth(),getHeight());
}
super.paint(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -