📄 graphicpaneld.java
字号:
//NG ENG SHEN
//A120358
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class GraphicPanelD extends JPanel {
private Color fColor;
private Color bColor;
private int saiz;
private int row;
private int col;
public GraphicPanelD() {
saiz = 20;
row =10;
col =10;
fColor = Color.black;
bColor = Color.black;
}
public void setGrid(int c, int r) {
row =r;
col =c;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
public void setBColor(Color c){
bColor = c;
}
public void setFColor(Color c){
fColor = c;
}
public Color getBColor(){
return bColor;
}
public Color getFColor(){
return fColor;
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
int x = (getSize().width-saiz*getCol())/2;
int y = (getSize().height-saiz*getRow())/2;
Rectangle2D.Double rect = new Rectangle2D.Double();
GeneralPath triangle = new GeneralPath();
int countRow=1;
int countCol=1;
for (int i=0; i < getCol(); i++)
{
y=(getSize().height-saiz*getRow())/2;
for (int j=0; j < getRow(); j++)
{
if(countCol%2==0)
{
if(countRow%2==0)
triangle = setTriangle(x,y,1);
else
triangle = setTriangle(x,y,3);
}
else
{
if(countRow%2==0)
triangle = setTriangle(x,y,2);
else
triangle = setTriangle(x,y,4);
}
g2d.draw(triangle);
g2d.setPaint(fColor);
g2d.fill(triangle);
rect.setRect(x, y, saiz, saiz);
g2d.setPaint(bColor);
g2d.draw(rect);
y += saiz;
countRow++;
}
x += saiz;
countCol++;
}
}
private GeneralPath setTriangle(int x, int y, int type){
GeneralPath triangle = new GeneralPath();
int x1=0,x2=0,x3=0;
int y1=0,y2=0,y3=0;
switch(type)
{
case 1:
{
x1 = x; y1 = y;
x2 = x; y2 = y+saiz;
x3 = x+saiz; y3 = y+saiz; break;
}
case 2:
{
x1 = x+saiz; y1 = y;
x2 = x; y2 = y;
x3 = x; y3 = y+saiz; break;
}
case 3:
{
x1 = x; y1 = y;
x2 = x+saiz; y2 = y;
x3 = x+saiz; y3 = y+saiz; break;
}
case 4:
{
x1 = x; y1 = y+saiz;
x2 = x+saiz; y2 = y+saiz;
x3 = x+saiz; y3 = y; break;
}
}
triangle.moveTo(x1,y1);
triangle.lineTo(x2, y2);
triangle.lineTo(x3, y3);
triangle.closePath();
return triangle;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -