bmppanel.java
来自「用java实现的对位图文件读取的源程序代码。」· Java 代码 · 共 332 行
JAVA
332 行
/**
*
* 负责显示位图,支持int[][]的矩阵数组,每一位表示一个像素
*/
import java.awt.*;
import java.awt.event.*;
class BMPPanel extends Panel implements MouseListener,MouseMotionListener {
private Color[][] pixel;
private int bmpWidth;
private int bmpHeight;
private int newX0 = 0;
private int newY0 = 0;
private int oldX0 = 0;
private int oldY0 = 0;
private int mousePressedX = 0;
private int mousePressedY = 0;
private int mouseReleasedX = 0;
private int mouseReleasedY = 0;
private int mouseDraggedX = 0;
private int mouseDraggedY = 0;
private double zoomSize = 0;
private boolean isDragged = false;
private int drawWhich;
private final int DRAW_BMP = 0;
private final int DRAW_MOVE_BORDER = 1;
private final int DRAW_ZOOM_BORDER = 2;
private final int DRAW_ZOOMING_BORDER = 3;
BMPPanel() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public BMPPanel(int[][] data) {
this();
setPixel(data);
}
public void drawBMP(int[][] data,int x,int y,int bmpWidth,int bmpHeight) {
oldX0 = newX0 = x;
oldY0 = newY0 = y;
this.bmpWidth = bmpWidth;// > getWidth() ? getWidth() - 50 : bmpWidth;
this.bmpHeight = bmpHeight;//> getHeight() ? getHeight() - 50 : bmpHeight;
setPixel(data);
repaint();
}
public void drawBMP(int[][] data,int x,int y) {
int bmpWidth = data.length;
int bmpHeight = data[0].length;
drawBMP(data,x,y,bmpWidth,bmpHeight);
}
public void drawBMP(int[][] data) {
int bmpWidth = data[0].length;
int bmpHeight = data.length;
int x = (this.getWidth() - bmpWidth) / 2;
int y = (this.getHeight() - bmpHeight) / 2;
drawBMP(data,x,y,bmpWidth,bmpHeight);
}
public int getBMPX0() {
return newX0;
}
public int getBMPY0() {
return newY0;
}
public int getBMPWidth() {
return bmpWidth;
}
public int getBMPHeight() {
return bmpHeight;
}
public void setDefaultSize() {
bmpWidth = pixel[0].length;
bmpHeight = pixel.length;
}
public void setDefaultLocation() {
newX0 = (this.getWidth() - bmpWidth) / 2;
newY0 = (this.getHeight() - bmpHeight) / 2;
oldX0 = newX0;
oldX0 = newX0;
}
public void paint(Graphics g) {
if (pixel == null) {
return ;
}
//.g.clearRect(0,0,this.getWidth(),this.getHeight());
switch(drawWhich) {
case DRAW_BMP : drawBMP(g); break;
case DRAW_MOVE_BORDER : drawMoveBorder(g); break;
case DRAW_ZOOM_BORDER : drawZoomBorder(g); break;
}
}
//public void
private void drawMoveBorder(Graphics g) {
g.setColor(Color.GREEN);
g.drawRect(newX0,newY0,bmpWidth,bmpHeight);
}
private void drawZoomBorder(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(newX0,newY0,bmpWidth,bmpHeight);
g.setColor(Color.PINK);
g.fillRect(newX0+bmpWidth-5,newY0+bmpHeight-5,10,10);
}
private void drawBMP(Graphics g) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
int row = pixel.length;
int col = pixel[0].length;
double zoomWidth = 1.0 * bmpWidth / col;
double zoomHeight = 1.0 * bmpHeight / row;
int pixelWidth = (int)(zoomWidth+1);
int pixelHeight = (int)(zoomHeight+1);
double x = newX0;
double y = newY0;
for (int i=0; i<row; i++) {
x = newX0;
for (int j=0; j<col; j++) {
g.setColor(pixel[i][j]);
g.fillRect((int)x,(int)y,pixelWidth,pixelHeight);
x += zoomWidth;
}
//new Drawer(g,pixel[i],newX0,y,pixelWidth,pixelHeight,zoomWidth).start();
//y += zoomHeight;
}
try {
while (Thread.activeCount() > 2) {
Thread.currentThread().suspend();
}
}
catch (Exception ex) {
}
//Toolbox.println("zoomWidth=" + zoomWidth);
//Toolbox.println("zoomHeight=" + zoomHeight);
}
class Drawer extends Thread {
Graphics g;
Color[] pixel;
int x0;
int pixelWidth;
int pixelHeight;
double x;
int y;
double zoomWidth;
Drawer(Graphics g,Color[] pixel,int x0,double y,int pixelWidth,int pixelHeight,double zoomWidth) {
this.g = g;
this.pixel = pixel;
this.x = x0;
this.y = (int)y;
this.pixelWidth = pixelWidth;
this.pixelHeight = pixelHeight;
this.zoomWidth = zoomWidth;
}
public void run() {
for (int j=0; j<pixel.length; j++) {
g.setColor(pixel[j]);
g.fillRect((int)x,y,pixelWidth,pixelHeight);
x += zoomWidth;
}
}
}
public void setPixel(int[][] data) {
pixel = new Color[data.length][data[0].length];
for (int i = 0; i<data.length; i++) {
for (int j = 0; j<data[i].length; j++) {
pixel[i][j] = new Color(data[i][j]);
}
}
data = null;
}
public void drawReverseBMP() {
}
public void locateToCenter(){}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
drawWhich = this.DRAW_BMP;
return;
}
mousePressedX = e.getX();
mousePressedY = e.getY();
drawWhich = this.DRAW_ZOOM_BORDER;
}
public void mousePressed(MouseEvent e) {
if (drawWhich == DRAW_ZOOM_BORDER) {
return;
}
mousePressedX = e.getX();
mousePressedY = e.getY();
setCursor(new Cursor(Cursor.MOVE_CURSOR));
drawWhich = this.DRAW_MOVE_BORDER;
}
public void mouseReleased(MouseEvent e) {
if(pixel == null) {
return;
}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
mouseReleasedX = e.getX();
mouseReleasedY = e.getY();
switch (drawWhich) {
case DRAW_MOVE_BORDER : doWhenMoveOver(); break;
case DRAW_ZOOM_BORDER : doWhenZoomOver(); break;
}
drawWhich = this.DRAW_BMP;
repaint();
}
public void doWhenZoomOver() {
}
public void doWhenMoveOver() {
newX0 = oldX0 + mouseReleasedX - mousePressedX;
newY0 = oldY0 + mouseReleasedY - mousePressedY;
oldX0 = newX0;
oldY0 = newY0;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
boolean isInZoomRect = false;
public void mouseMoved(MouseEvent e) {
if (this.drawWhich != this.DRAW_ZOOM_BORDER) {
return;
}
int x = e.getX();
int y = e.getY();
int SEX = newX0 + bmpWidth;
int SEY = newY0 + bmpHeight;
if (x > SEX-5 && x < SEX+5 && y > SEY-5 && y < SEY+5) {
this.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
isInZoomRect = true;
} else {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
isInZoomRect = false;
}
}
public void mouseDragged(MouseEvent e) {
mouseDraggedX = e.getX();
mouseDraggedY = e.getY();
Cursor cursor = this.getCursor();
if (this.drawWhich == this.DRAW_MOVE_BORDER) {
doWhenMove();
} else if (this.drawWhich == this.DRAW_ZOOM_BORDER && isInZoomRect) {
doWhenZoom();
}
repaint();
}
private void doWhenMove() {
newX0 = oldX0 + mouseDraggedX - mousePressedX;
newY0 = oldY0 + mouseDraggedY - mousePressedY;
}
private void doWhenZoom() {
bmpWidth = mouseDraggedX - newX0;
bmpHeight = mouseDraggedY - newY0;
bmpWidth = bmpWidth < 20 ? 20 : bmpWidth;
bmpHeight = bmpHeight < 20 ? 20 : bmpHeight;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?