📄 basicgrid.java
字号:
package norman.baba.grids;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
//import javax.swing.*;
/**
* <p>Title: BABA</p>
* <p>Description: Bioinformatique Research Project</p>
* <p>Copyright: Copyright Norman Casagrande (c) 2003</p>
* @author Norman Casagrande
* @version 1.0
*/
public class BasicGrid
extends Panel
implements ComponentListener, MouseListener, MouseMotionListener {
/** -------------------------------------------
* Graphical section
* ------------------------------------------- */
/** The image */
protected Image m_offScreenImage = null;
/** Graphic bitmap for double buffering */
protected Graphics m_offScreenGraphics = null;
/** Image size */
protected Dimension m_areaSize = null;
/** Gap space between the border and the grid */
protected int m_borderGap = 8;
/**
* Start (x1, y1) position of the image in the control. Used when
* the control is bigger than the image.
*/
protected Point m_centeredImgStartPos = new Point();
protected static Color GRIDS_COLOR = Color.lightGray;
protected static Color GRID_BACKGROUND_COLOR = Color.white;
protected class GridRect {
public Point from = new Point(); // Used when zooming
public Point dim = new Point(); // Used when zooming
public Rectangle coords = null;
public int size = 1;
public Color color = Color.black; // Default color = black
public GridRect(int cFrom, int rFrom,
int cWidth, int rHeight,
int size, Color color) {
this.coords = new Rectangle(cFrom * m_cellSize,
rFrom * m_cellSize,
(cWidth - 1) * m_cellSize + m_cellSize,
(rHeight - 1) * m_cellSize + m_cellSize);
this.from.setLocation(cFrom, rFrom);
this.dim.setLocation(cWidth, rHeight);
this.size = size;
this.color = color;
}
}
protected class GridCircle {
Point pos = new Point(); // Used when zooming
public Rectangle coords = null;
public Color color = Color.red; // Default color = red
int c = 0, r = 0;
}
protected class Highlight {
public static final int HL_SIDEONLY = 0;
public static final int HL_CENTERONLY = 1;
public static final int HL_SIDEANDCENTER = 2;
CellElement cell = null;
int highlightType = HL_SIDEONLY;
Highlight(CellElement cell, int highlightType) {
this.cell = cell;
this.highlightType = highlightType;
}
}
// protected GridRect m_evRect = null;
protected LinkedList m_evRects = null;
protected GridCircle m_evCircle = null;
protected int m_evCircleBounds = 7; // smaller value = bigger circle (in the cell)
protected RenderingHints m_rhAntiAliasOn;
protected RenderingHints m_rhAntiAliasOff;
protected boolean m_activeAntialias = true;
/** -------------------------------------------
* Normal Grid Section
* ------------------------------------------- */
public static int EMPTY_TABLE_SIZE = 6; // default empty size (pixels)
protected int m_cellSize = 40; // default cell size (pixels)
/** Zoom Level */
protected int m_originalCellSize = m_cellSize;
protected int m_originalFontSize = 13;
protected double m_zoomLevel = 1;
protected Font m_font = null;
protected int m_nHCells = EMPTY_TABLE_SIZE;
protected int m_nVCells = EMPTY_TABLE_SIZE;
protected CellElement m_cells[][];
// ----------- Grid Interactivity --------------
protected LinkedList m_interactCells = new LinkedList();
protected CellElement m_currentInteractCell = null;
protected CellInteractInterface m_cellInterface = null;
// ---------------------------------------------
/**
* Default Constructor
*/
public BasicGrid() {
this(EMPTY_TABLE_SIZE, EMPTY_TABLE_SIZE);
}
public BasicGrid(int nHCells, int nVCells) {
addComponentListener(this);
addMouseMotionListener(this);
addMouseListener(this);
m_rhAntiAliasOn =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
m_rhAntiAliasOff =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
m_cellSize = (int) (m_originalCellSize * m_zoomLevel);
m_font = new Font(null,
Font.PLAIN,
(int) (m_originalFontSize * m_zoomLevel));
m_evRects = new LinkedList();
this.setFont(m_font);
this.setGridSize(nHCells, nVCells, false);
}
public void setCellListener(CellInteractInterface cellInterface) {
this.m_cellInterface = cellInterface;
}
public void setGridSize(int nHCells, int nVCells, boolean updateAreaSize) {
m_nHCells = nHCells;
m_nVCells = nVCells;
m_cells = new CellElement[m_nHCells][m_nVCells];
int c, r;
for (r = 0; r < m_nVCells; ++r) {
for (c = 0; c < m_nHCells; ++c) {
m_cells[c][r] = new CellElement(c, r,
BasicGrid.GRID_BACKGROUND_COLOR);
}
}
m_areaSize = null;
if (updateAreaSize) {
this.setDrawAreaSize();
}
}
public int getHCellsCount() {
return m_nHCells;
}
public int getVCellsCount() {
return m_nVCells;
}
public void setZoomLevel(double zoom) {
m_zoomLevel = zoom;
m_cellSize = (int) (m_originalCellSize * m_zoomLevel);
m_font = new Font(null,
Font.PLAIN,
(int) (m_originalFontSize * m_zoomLevel));
this.setFont(m_font);
m_areaSize = null;
this.updateGridRects();
if (m_evCircle != null) {
int bounds = m_cellSize / m_evCircleBounds;
m_evCircle.coords.setBounds(m_evCircle.pos.x * m_cellSize + bounds,
m_evCircle.pos.y * m_cellSize + bounds,
m_cellSize - bounds * 2,
m_cellSize - bounds * 2);
}
}
protected void updateGridRects() {
ListIterator lIt = m_evRects.listIterator();
GridRect tmpRect;
while (lIt.hasNext()) {
tmpRect = (GridRect)lIt.next();
tmpRect.coords.setBounds(tmpRect.from.x * m_cellSize,
tmpRect.from.y * m_cellSize,
(tmpRect.dim.x - 1) * m_cellSize +
m_cellSize,
(tmpRect.dim.y - 1) * m_cellSize +
m_cellSize);
}
}
public double getZoomLevel() {
return m_zoomLevel;
}
public void setAntiAlias(boolean activate) {
m_activeAntialias = activate;
m_areaSize = null;
}
public boolean getAntiAlias() {
return m_activeAntialias;
}
public void setGridSize(int nHCells, int nVCells) {
setGridSize(nHCells, nVCells, true);
}
public boolean setCellValue(int c, int r, int value) {
return this.setCellValue(c, r, Integer.toString(value));
}
public boolean setCellValue(int c, int r, char value) {
return this.setCellValue(c, r, "" + value);
}
public boolean setCellValue(int c, int r, String value) {
if (!checkBounds(c, r, "setCellValue")) {
return false;
}
m_cells[c][r].setVal(value);
return true;
}
public String getCellValue(int c, int r) {
if (!checkBounds(c, r, "getCellValue")) {
return "";
}
return m_cells[c][r].getVal();
}
public CellElement getCell(int c, int r) {
if (!checkBounds(c, r, "getCell")) {
return null;
}
return m_cells[c][r];
}
public CellElement getLastCell() {
return m_cells[m_nHCells - 1][m_nVCells - 1];
}
public int getCellValueInt(int c, int r) {
if (!checkBounds(c, r, "getCellValueInt")) {
return Integer.MAX_VALUE;
}
String val = m_cells[c][r].getVal();
int retVal = 0;
if (val == null ||
val == "") {
retVal = Integer.MAX_VALUE;
}
else {
retVal = Integer.parseInt(val);
}
return retVal;
}
public boolean clearCellContent(int c, int r) {
if (!checkBounds(c, r, "clearCellContent")) {
return false;
}
m_cells[c][r].clearCell();
return true;
}
public boolean setCellColor(int c, int r, Color newColor) {
if (!checkBounds(c, r, "setCellColor")) {
return false;
}
m_cells[c][r].setColor(newColor);
return true;
}
public boolean clearCellColor(int c, int r) {
if (!checkBounds(c, r, "clearCellColor")) {
return false;
}
m_cells[c][r].setColor(BasicGrid.GRID_BACKGROUND_COLOR);
return true;
}
public void setHighlight(Highlight hl,
Color centerColor, Color sideColor) {
int c = hl.cell.getColumn();
int r = hl.cell.getRow();
// paint
switch (hl.highlightType) {
case Highlight.HL_CENTERONLY:
hl.cell.setHLColor(centerColor);
break;
case Highlight.HL_SIDEONLY:
m_cells[c]
[0].setHLColor(sideColor);
m_cells[0]
[r].setHLColor(sideColor);
break;
case Highlight.HL_SIDEANDCENTER:
hl.cell.setHLColor(centerColor);
m_cells[c]
[0].setHLColor(sideColor);
m_cells[0]
[r].setHLColor(sideColor);
break;
}
}
public boolean clearHighlight(Highlight hl) {
int c = hl.cell.getColumn();
int r = hl.cell.getRow();
if (!checkBounds(c, r, "clearHighlight")) {
return false;
}
switch (hl.highlightType) {
case Highlight.HL_CENTERONLY:
hl.cell.clearHLColor();
break;
case Highlight.HL_SIDEONLY:
m_cells[c]
[0].clearHLColor();
m_cells[0]
[r].clearHLColor();
break;
case Highlight.HL_SIDEANDCENTER:
hl.cell.clearHLColor();
m_cells[c]
[0].clearHLColor();
m_cells[0]
[r].clearHLColor();
break;
}
return true;
}
public void clearHighlightColors() {
int c, r;
CellElement tmpCell;
for (r = 0; r < m_nVCells; ++r) {
for (c = 0; c < m_nHCells; ++c) {
tmpCell = m_cells[c][r];
tmpCell.clearHLColor();
}
}
}
public boolean addGridRectangle(int cFrom, int rFrom,
int cWidth, int rHeight,
int size, Color color) {
if (!checkBounds(cFrom, rFrom, "addGridRectangle") ||
!checkBounds(cWidth, rHeight, "addGridRectangle")) {
return false;
}
GridRect tmpRect = new GridRect(cFrom, rFrom,
cWidth, rHeight,
size, color);
m_evRects.add(tmpRect);
return true;
}
/** Setting just one grid rectangle */
public boolean setGridRectangle(int cFrom, int rFrom,
int cWidth, int rHeight,
int size, Color color) {
if (!checkBounds(cFrom, rFrom, "setGridRectangle") ||
!checkBounds(cWidth, rHeight, "setGridRectangle")) {
return false;
}
m_evRects.clear();
this.addGridRectangle(cFrom, rFrom,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -