📄 mapeditor.java
字号:
public void run()
{
// while(true)
// {
// try
// {
// if(tilePanel.selectedTileID != -1)
// {
// repaint();
// }
// m_t.sleep(100);
// }
// catch(Exception e)
// {
// }
// }
}
public static void main(String args[])
{
new MapEditor();
}
/*
*
*
*The following code deal with events
*
*
*/
class TileDropTargetListener implements DropTargetListener
{
public void dragEnter(DropTargetDragEvent dtde)
{
System.out.println("map dragEntered");
}
public void dragExit(DropTargetEvent dte)
{
System.out.println("map dragexited");
}
public void dragOver(DropTargetDragEvent dtde)
{
}
public void drop(DropTargetDropEvent dtde)
{
System.out.println("map dropped");
}
public void dropActionChanged(DropTargetDragEvent dtde)
{
}
}
class FileItemListener implements ActionListener
{
int ID;
FileItemListener(int id)
{
ID = id;
}
public void actionPerformed(ActionEvent e)
{
String[] imageExtension = {"png"};
String[] mapExtension = {"map"};
switch(ID)
{
case MENUITEM_LOADIMAGE:
JFileChooser fchooser = new JFileChooser();
fchooser.addChoosableFileFilter(new DefaultFileFilter(imageExtension, "(*.png)"));
File openFile = new File("X:\\MyProject\\Sanguo\\data\\Triplets\\Map");
//File openFile = new File("X:\\pinball");
if(openFile.exists())
{
fchooser.setCurrentDirectory(openFile);
}
int openOption = fchooser.showOpenDialog(MapEditor.this);
if(openOption == JFileChooser.APPROVE_OPTION)
{
String imageName = fchooser.getSelectedFile().getName();
editingPanel.initImage(imageName);
}
break;
case MENUITEM_SAVE:
if(destFile != null && destFile.exists())
{
save(destFile);
break;
}
case MENUITEM_SAVEAS:
fchooser = new JFileChooser();
fchooser.addChoosableFileFilter(new DefaultFileFilter(mapExtension, "(*.map)"));
File saveFile = new File("X:\\MyProject\\Sanguo\\data\\Triplets\\Map");
//File saveFile = new File("X:\\pinball");
if(saveFile.exists())
{
fchooser.setCurrentDirectory(saveFile);
}
int saveOption = fchooser.showSaveDialog(MapEditor.this);
if(saveOption == JFileChooser.APPROVE_OPTION)
{
saveFile = fchooser.getSelectedFile();
if(!saveFile.getName().endsWith(".map"))
{
JOptionPane.showMessageDialog (null,"End with .map", "Error" ,JOptionPane.ERROR_MESSAGE);
actionPerformed(e);
return;
}
if(saveFile.exists())
{
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing file?","Confirm Overwrite",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(response != JOptionPane.CANCEL_OPTION)
{
save(saveFile);
}
}
else
{
save(saveFile);
}
}
break;
case MENUITEM_OPEN:
fchooser = new JFileChooser();
fchooser.addChoosableFileFilter(new DefaultFileFilter(mapExtension, "(*.map)"));
openFile = new File("X:\\MyProject\\Sanguo\\data\\Triplets\\Map");
//openFile = new File("X:\\pinball");
if(openFile.exists())
{
fchooser.setCurrentDirectory(openFile);
}
openOption = fchooser.showOpenDialog(MapEditor.this);
if(openOption == JFileChooser.APPROVE_OPTION)
{
destFile = fchooser.getSelectedFile();
open(destFile);
editingPanel.repaintMap = true;
}
break;
case MENUITEM_EXIT:
System.exit(0);
break;
}
}
}
class ZoomIn extends AbstractAction
{
ZoomIn(String name, Icon icon)
{
super(name, icon);
}
public void actionPerformed(ActionEvent e)
{
if(editingPanel.tile == null)
{
return;
}
if(zoomFactor < MAX_ZOOM_FACTOR)
{
zoomFactor *= 2;
editingPanel.notifyImage();
}
}
}
class ZoomOut extends AbstractAction
{
ZoomOut(String name, Icon icon)
{
super(name, icon);
}
public void actionPerformed(ActionEvent e)
{
if(editingPanel.tile == null)
{
return;
}
if(zoomFactor > 1)
{
zoomFactor /= 2;
editingPanel.notifyImage();
}
}
}
class ButtonListener implements ActionListener
{
int btID;
ButtonListener(int id)
{
btID = id;
}
public void actionPerformed(ActionEvent e)
{
switch(btID)
{
case BT_SET_TILE:
int tw = Integer.parseInt(tf_tileWidth.getText());
int th = Integer.parseInt(tf_tileHeight.getText());
if(editingPanel.tile != null)
{
if((editingPanel.imageWidth/zoomFactor) % tw != 0)
{
tf_tileWidth.setText("Invalid");
return;
}
if((editingPanel.imageHeight/zoomFactor) % th != 0)
{
tf_tileHeight.setText("Invalid");
return;
}
}
editingPanel.tileWidth = tw;
editingPanel.tileHeight = th;
editingPanel.nb_tile_in_tile_image_H = editingPanel.imageWidth / zoomFactor / tw;
editingPanel.nb_tile_in_tile_image_V = editingPanel.imageHeight / zoomFactor / th;
break;
case BT_SET_MAP:
int mw = Integer.parseInt(tf_mapWidth.getText());
int mh = Integer.parseInt(tf_mapHeight.getText());
if(mw % editingPanel.tileWidth != 0)
{
tf_mapWidth.setText("Invalid");
return;
}
if(mh % editingPanel.tileHeight != 0)
{
tf_mapHeight.setText("Invalid");
return;
}
editingPanel.mapWidth = mw;
editingPanel.mapHeight = mh;
initMapBuffer();
break;
case BT_SET_SCREEN:
int sw = Integer.parseInt(tf_screenWidth.getText());
int sh = Integer.parseInt(tf_screenHeight.getText());
editingPanel.screenWidth = sw;
editingPanel.screenHeight = sh;
break;
case BT_PREVIEW:
editingPanel.setPreview();
preview.setVisible(false);
quitPreview.setVisible(true);
break;
case BT_QUIT_PREVIEW:
editingPanel.quitPreview();
preview.setVisible(true);
quitPreview.setVisible(false);
break;
}
}
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -