📄 basicgrid.java
字号:
cWidth, rHeight,
size, color);
return true;
}
public boolean setGridRectangle(int cFrom, int rFrom,
int cWidth, int rHeight) {
return this.setGridRectangle(cFrom, rFrom, cWidth, rHeight, 2, Color.black);
}
public boolean setGridCircle(int c, int r) {
if (!checkBounds(c, r, "setGridCircle - Normal")) {
return false;
}
if (m_evCircle == null) {
m_evCircle = new GridCircle();
m_evCircle.coords = new Rectangle();
}
setGridCircle(c, r, m_evCircle.color);
return true;
}
public void clearGridCircle() {
m_evCircle = null;
}
public void clearGridRectangles() {
m_evRects.clear();
}
public boolean setGridCircle(int c, int r, Color color) {
if (!checkBounds(c, r, "setGridCircle - Set Color")) {
return false;
}
if (m_evCircle == null) {
m_evCircle = new GridCircle();
m_evCircle.coords = new Rectangle();
}
int bounds = m_cellSize / m_evCircleBounds;
m_evCircle.coords.setBounds(c * m_cellSize + bounds,
r * m_cellSize + bounds,
m_cellSize - bounds * 2,
m_cellSize - bounds * 2);
m_evCircle.pos.setLocation(c, r);
m_evCircle.color = color;
m_evCircle.c = c;
m_evCircle.r = r;
return true;
}
public boolean checkBounds(int c, int r, String callingFunc) {
if ( (r < 0 || r >= m_nVCells) ||
(c < 0 || c >= m_nHCells)) {
System.err.println(callingFunc + ": Out of bounds error!");
return false;
}
else {
return true;
}
}
/**
* This is an overriding of the {@link Panel#paint(Graphics g)} method
* to draw the Panel.
* <P>
* In this method the offscreen image for double buffering is created
* (if doesn't exists).
*
* @param g The Graphics class where to paint.
*/
public void paint(Graphics g) {
if (m_areaSize == null) {
this.setDrawAreaSize();
}
this.drawGrid(m_offScreenGraphics);
g.drawImage(m_offScreenImage,
m_centeredImgStartPos.x,
m_centeredImgStartPos.y, this);
}
protected void setDrawAreaSize() {
m_areaSize = new Dimension();
m_areaSize.setSize(m_nHCells * m_cellSize,
m_nVCells * m_cellSize);
m_centeredImgStartPos.x = m_borderGap;
m_centeredImgStartPos.y = m_borderGap;
// Double buffering
m_offScreenImage = createImage(m_areaSize.width + 1,
m_areaSize.height + 1);
m_offScreenGraphics = m_offScreenImage.getGraphics();
// If no evidencing rectangle has been defined set
// the default one
if (m_evRects.isEmpty()) {
GridRect tmpRect = new GridRect(0, 0,
m_nHCells, m_nVCells,
1, Color.black);
m_evRects.add(tmpRect);
}
}
/**
* Overriding of the of the {@link Container} method
* to get the dimensions of the icon.
*
* @return The preferred size of the icon, which means the picture size.
*/
public Dimension getPreferredSize() {
if (m_areaSize != null) {
return new Dimension(m_areaSize.width + m_borderGap * 2,
m_areaSize.height + m_borderGap * 2);
}
else {
return super.getPreferredSize();
}
}
protected void drawGrid(Graphics g) {
// Let's first clean everything
g.setColor(BasicGrid.GRID_BACKGROUND_COLOR);
g.fillRect(0, 0, m_areaSize.width + 1, m_areaSize.height + 1);
// now draw the internal grid
g.setColor(BasicGrid.GRIDS_COLOR);
int c, r;
for (c = 0; c < m_nHCells + 1; ++c) {
g.drawLine(c * m_cellSize, 0, c * m_cellSize, m_areaSize.height);
}
for (r = 0; r < m_nVCells + 1; ++r) {
g.drawLine(0, r * m_cellSize, m_areaSize.width, r * m_cellSize);
}
this.drawCellFillColors(g);
this.drawInteractCell(g);
this.drawEvCircle(g);
// now draw a rectangle
this.drawEvRectangle(g);
this.drawValues(g);
}
protected void drawValues(Graphics g) {
int x, y;
int r, c;
Graphics2D g2d = (Graphics2D) g;
if (m_activeAntialias) {
g2d.setRenderingHints(m_rhAntiAliasOn);
}
FontMetrics fm = getFontMetrics(m_font);
int fontHeightDiv = fm.getAscent() / 2 - (int)m_zoomLevel; //fm.getHeight() / 2 - fm.getDescent();
int cellSizeDiv = m_cellSize / 2;
int fontWidth = 0;
String val;
g.setFont(m_font);
g.setColor(Color.black);
for (r = 0; r < m_nVCells; ++r) {
for (c = 0; c < m_nHCells; ++c) {
if (m_cells[c][r].getVal() != null) {
val = m_cells[c][r].getVal();
fontWidth = fm.stringWidth(val);
x = c * m_cellSize + cellSizeDiv - (fontWidth / 2);
y = r * m_cellSize + cellSizeDiv + fontHeightDiv;
//g.drawRect(x,y,x+5,y+5);
g.drawString(val, x, y);
}
}
}
}
protected void drawCellFillColors(Graphics g) {
int r, c;
int xPos, yPos;
for (r = 0; r < m_nVCells; ++r) {
for (c = 0; c < m_nHCells; ++c) {
if (m_cells[c][r].getColor() != BasicGrid.GRID_BACKGROUND_COLOR) {
//System.out.println("Color found: " + m_matrixColor[c][r]);
xPos = c * m_cellSize;
yPos = r * m_cellSize;
g.setColor(m_cells[c][r].getColor());
g.fillRect(xPos + 1, yPos + 1,
m_cellSize - 1, m_cellSize - 1);
}
}
}
}
protected void drawInteractCell(Graphics g) {
if (m_currentInteractCell == null) {
return;
}
//GridRect cellRect = new GridRect();
//cellRect.coords = new Rectangle();
int col = m_currentInteractCell.getColumn();
int row = m_currentInteractCell.getRow();
int xPos = m_currentInteractCell.getColumn() * m_cellSize;
int yPos = m_currentInteractCell.getRow() * m_cellSize;
GridRect cellRect = new GridRect(col, row, col+1, row+1, 1, Color.black);
cellRect.coords.setBounds( (col) * m_cellSize,
(row) * m_cellSize,
m_cellSize,
m_cellSize);
if (m_currentInteractCell.IsPressed()) {
g.setColor(m_currentInteractCell.getMousePressColor());
cellRect.size = m_currentInteractCell.getMousePressBorder();
}
else {
g.setColor(m_currentInteractCell.getMouseOverColor());
cellRect.size = m_currentInteractCell.getMouseOverBorder();
}
g.fillRect(xPos + 1, yPos + 1,
m_cellSize - 1, m_cellSize - 1);
drawRectangle(g, cellRect);
}
protected void drawRectangle(Graphics g, GridRect rect) {
g.setColor(rect.color);
// size <= 1
if (rect.size <= 1) {
g.drawRect(rect.coords.x, rect.coords.y,
rect.coords.width, rect.coords.height);
return;
}
// size > 1
int i;
int sizeDiv = rect.size / 2;
for (i = -sizeDiv; i < sizeDiv + (rect.size % 2); ++i) {
g.drawRect(rect.coords.x - i, rect.coords.y - i,
rect.coords.width + i * 2, rect.coords.height + i * 2);
}
}
protected void drawEvRectangle(Graphics g) {
ListIterator lIt = m_evRects.listIterator();
while (lIt.hasNext()) {
drawRectangle(g, (GridRect)lIt.next());
}
}
protected void drawEvCircle(Graphics g) {
if (m_evCircle == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
if (m_activeAntialias) {
g2d.setRenderingHints(m_rhAntiAliasOn);
}
g2d.setColor(m_evCircle.color);
g2d.fillOval(m_evCircle.coords.x, m_evCircle.coords.y,
m_evCircle.coords.width, m_evCircle.coords.height);
int holeSize = m_evCircle.coords.width / 8; // smaller number is thicker
g2d.setColor(m_cells[m_evCircle.c][m_evCircle.r].getColor());
g2d.fillOval(m_evCircle.coords.x + holeSize,
m_evCircle.coords.y + holeSize,
m_evCircle.coords.width - holeSize * 2,
m_evCircle.coords.height - holeSize * 2);
if (m_activeAntialias) {
g2d.setRenderingHints(m_rhAntiAliasOff);
}
}
// --------------- Interactivity grid section -------------------------
public void clearInteractiveCells() {
m_interactCells.clear();
}
public void addInteractiveCell(CellElement e) {
m_interactCells.add(e);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
if (m_interactCells.size() == 0) {
return;
}
CellElement prevInteract = m_currentInteractCell;
m_currentInteractCell = getInteractOnCoord(e.getPoint());
if (m_currentInteractCell != prevInteract) {
paint(this.getGraphics());
}
}
private CellElement getInteractOnCoord(Point coord) {
coord.x -= m_borderGap;
coord.y -= m_borderGap;
Point cellCoord = new Point();
cellCoord.x = coord.x / m_cellSize;
cellCoord.y = coord.y / m_cellSize;
ListIterator a = m_interactCells.listIterator();
CellElement tmpCell;
while (a.hasNext()) {
tmpCell = (CellElement) a.next();
if (tmpCell.getColumn() == cellCoord.x &&
tmpCell.getRow() == cellCoord.y) {
return tmpCell;
}
}
return null;
}
/** Mouse Listeners */
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (m_currentInteractCell != null) {
m_currentInteractCell.setPressed(true);
paint(this.getGraphics());
}
}
public void mouseReleased(MouseEvent e) {
if (m_currentInteractCell == null) {
return;
}
m_currentInteractCell.setPressed(false);
CellElement prevInteract = m_currentInteractCell;
m_currentInteractCell = getInteractOnCoord(e.getPoint());
if (m_currentInteractCell == prevInteract) {
m_currentInteractCell = null;
m_cellInterface.onInteractPress(prevInteract);
}
else {
paint(this.getGraphics());
}
}
// --------------------------------------------------------------------
/**
* Invoked when the component (indirectly by the window) is resized.
*
* @param e The event occurred.
*/
public void componentResized(ComponentEvent e) {
//m_areaSize = null;
}
/**
* Invoked when the component is hided.
* <P>
* It is here just because it's needed by the implementation of
* the component listener.
*
* @param e The event occurred.
*/
public void componentHidden(ComponentEvent e) {}
/**
* Invoked when the component is moved.
* <P>
* It is here just because it's needed by the implementation of
* the component listener.
*
* @param e The event occurred.
*/
public void componentMoved(ComponentEvent e) {}
/**
* Invoked when the component is showed.
* <P>
* It is here just because it's needed by the implementation of
* the component listener.
*
* @param e The event occurred.
*/
public void componentShown(ComponentEvent e) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -