📄 tilepanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.net.*;
import java.io.*;
import java.util.*;
class TilePanel extends JPanel implements Runnable, MouseListener, MouseMotionListener,KeyListener,
DragGestureListener, DragSourceListener
{
public static final int TILE_PANEL_WIDTH = 400;
public static final int TILE_PANEL_HEIGHT = 300;
static int tileWidth = 4;
static int tileHeight = 4;
static int nb_tile_horizontal;
static int nb_tile_vertical;
static int selectedTileID = -1;
int srcImageWidth;
int srcImageHeight;
int imageWidth;
int imageHeight;
static int mouseX;
static int mouseY;
Thread m_t = new Thread(this);
Image tile;
Image screenBuffer;
DragSource ds = new DragSource();
static Border titleBorder;
TilePanel(boolean isDoubleBuffered)
{
super(isDoubleBuffered);
initImage("hand.png");
setPreferredSize(new Dimension(TILE_PANEL_WIDTH,TILE_PANEL_HEIGHT));
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);
m_t.start();
}
public void drawCell(Graphics g, int x, int y, int imageW, int imageH, int tileW, int tileH, int factor)
{
g.setColor(Color.gray);
for(int i = 0;i < imageW/factor/tileW + 1; i++)
{
g.drawLine(x + i * tileW * factor, y, x + i * tileW * factor, y + imageH);
}
for(int j = 0;j < imageH/factor/tileH + 1;j++)
{
g.drawLine(x, y + j * tileH * factor, x + imageW, y + j * tileH * factor);
}
}
public void paint(Graphics g)
{
g.setClip(0,0,TILE_PANEL_WIDTH,TILE_PANEL_HEIGHT);
g.setColor(Color.white);
g.fillRect(0,0,TILE_PANEL_WIDTH,TILE_PANEL_HEIGHT);
if(tile == null)
{
return;
}
g.drawImage(tile, 0, 0, null);
drawCell(g, 0, 0, imageWidth, imageHeight, tileWidth, tileHeight, MapEditor.zoomFactor);
g.setColor(Color.red);
g.drawRect((selectedTileID%nb_tile_horizontal)*tileWidth*MapEditor.zoomFactor,
(selectedTileID/nb_tile_horizontal)*tileHeight*MapEditor.zoomFactor,
tileWidth*MapEditor.zoomFactor,
tileHeight*MapEditor.zoomFactor
);
}
public void update(Graphics g)
{
if(screenBuffer == null)
{
screenBuffer=createImage(TILE_PANEL_WIDTH, TILE_PANEL_HEIGHT);
}
Graphics g2=screenBuffer.getGraphics();
paint(g2);
g.drawImage(screenBuffer,TILE_PANEL_WIDTH,TILE_PANEL_HEIGHT,null);
}
public void run()
{
while(true)
{
try
{
repaint();
m_t.sleep(100);
}
catch(Exception e)
{
}
}
}
void initImage(String name)
{
tile = (new ImageIcon(name)).getImage();
srcImageWidth = tile.getWidth(this) * MapEditor.zoomFactor;
srcImageHeight = tile.getHeight(this) * MapEditor.zoomFactor;
imageWidth = srcImageWidth;
imageHeight = srcImageHeight;
nb_tile_horizontal = imageWidth/tileWidth;
nb_tile_vertical = imageHeight/tileHeight;
}
void notifyImage()
{
if(tile == null)
{
return;
}
imageWidth = srcImageWidth * MapEditor.zoomFactor;
imageHeight = srcImageHeight * MapEditor.zoomFactor;
tile = tile.getScaledInstance(imageWidth,imageHeight,Image.SCALE_SMOOTH);
}
/*
*
*
*The following code deal with events
*
*
*/
public void dragGestureRecognized(DragGestureEvent dge)
{
System.out.println("drag Gesture Recognized");
ds.startDrag(dge, DragSource.DefaultMoveDrop, tile, new Point(0, 0), new StringSelection(""), this);
}
public void dragEnter(DragSourceDragEvent e)
{
}
public void dragExit(DragSourceEvent e)
{
}
public void dragOver(DragSourceDragEvent e)
{
}
public void dragDropEnd(DragSourceDropEvent e)
{
}
public void dropActionChanged(DragSourceDragEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
requestFocus();
if(e.getX() > imageWidth || e.getY() > imageHeight)
{
return;
}
mouseX = e.getX()/MapEditor.zoomFactor;
mouseY = e.getY()/MapEditor.zoomFactor;
selectedTileID = nb_tile_horizontal * (mouseY/tileHeight) + (mouseX/tileWidth);
System.out.println("selectedTileID="+selectedTileID);
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
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 + -