我遇到了Java图形问题。在C#中我使用笔和solidbrush,但我可以在Java中使用什么 创建或填充多边形?我想用C#I' m刷一些矩阵的一部分
public void Draw( Graphics oGraph)
{
Pen blackpen = new Pen(Color.Black, 2);
Point[] tmpPoints = (Point[]) thePoints.Clone();
for(int i=0;i<iNumPoints;i++)
{
tmpPoints[i].X += iPosX;
tmpPoints[i].Y += iPosY;
}
oGraph.DrawPolygon(blackpen, tmpPoints);
oGraph.FillPolygon(piecebrush, tmpPoints, FillMode.Winding);
blackpen.Dispose();
}
并且它在C#中工作,但在java中它不会工作。我应该在java中使用什么代码来创建那个多边形,如果在java图形中我需要drawPolygon(int [] xPoints,int [] yPoints,int nPoints)。
可以提供帮助的Java代码
private int[][] iMatrix;
private Point[] thePoints;
private int iNumPoints;
private Graphics g;
public figura()
{
iMatrix = new int[3][3];
}
public void NapraviFiguru(int oblik)
{
switch(oblik)
{
case 1:
iMatrix[0][0] = 1;
iMatrix[1][0] = 1;
iMatrix[2][0] = 0;
iMatrix[0][1] = 1;
iMatrix[1][1] = 1;
iMatrix[2][1] = 0;
iMatrix[0][2] = 0;
iMatrix[1][2] = 0;
iMatrix[2][2] = 0;
g.setColor(Color.blue);
break;
case 2:
iMatrix[0][0] = 1;
iMatrix[1][0] = 1;
iMatrix[2][0] = 1;
iMatrix[0][1] = 0;
iMatrix[1][1] = 0;
iMatrix[2][1] = 0;
iMatrix[0][2] = 0;
iMatrix[1][2] = 0;
iMatrix[2][2] = 0;
g.setColor(Color.green);
break;
case 3:
iMatrix[0][0] = 1;
iMatrix[1][0] = 0;
iMatrix[2][0] = 0;
iMatrix[0][1] = 1;
iMatrix[1][1] = 1;
iMatrix[2][1] = 0;
iMatrix[0][2] = 0;
iMatrix[1][2] = 0;
iMatrix[2][2] = 0;
g.setColor(Color.yellow);
break;
case 4:
iMatrix[0][0] = 1;
iMatrix[1][0] = 1;
iMatrix[2][0] = 0;
iMatrix[0][1] = 1;
iMatrix[1][1] = 0;
iMatrix[2][1] = 0;
iMatrix[0][2] = 1;
iMatrix[1][2] = 0;
iMatrix[2][2] = 0;
g.setColor(Color.red);
break;
case 5:
iMatrix[0][0] = 1;
iMatrix[1][0] = 0;
iMatrix[2][0] = 0;
iMatrix[0][1] = 1;
iMatrix[1][1] = 0;
iMatrix[2][1] = 0;
iMatrix[0][2] = 0;
iMatrix[1][2] = 0;
iMatrix[2][2] = 0;
g.setColor(Color.pink);
break;
}
CreatePoints();
}
private void CreatePoints()
{
final int _RECTSIZE_ = 20;
Point[] tmpPoints = new Point[100];
int iPoints = 0, i = 0, j = 0;
iNumPoints = 0;
Point[] tmpPoints2 = new Point[36];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(iMatrix[j][i] == 0)
{
if(j != 0)
{
if(iMatrix[j-1][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
if(j != 2)
{
if(iMatrix[j+1][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_)+_RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
if(i != 0)
{
if(iMatrix[j][i-1] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
}
}
if(i != 2)
{
if(iMatrix[j][i+1] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_)+_RECTSIZE_;
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
}
if(j == 0)
{
if(iMatrix[j][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
if(j == 2)
{
if(iMatrix[j][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
if(i == 0)
{
if(iMatrix[j][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_)+_RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_);
iPoints++;
}
}
if(i == 2)
{
if(iMatrix[j][i] == 1)
{
tmpPoints[iPoints].x = (j * _RECTSIZE_);
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
tmpPoints[iPoints].x = (j * _RECTSIZE_) + _RECTSIZE_;
tmpPoints[iPoints].y = (i * _RECTSIZE_) + _RECTSIZE_;
iPoints++;
}
}
}
}
然后我用
public void Paint( Graphics Graph)
{
Point[] tmpPoints = (Point[]) thePoints.clone();
for(int i=0;i<iNumPoints;i++)
{
tmpPoints[i].x += iPosX;
tmpPoints[i].y += iPosY;
}
Graph.DrawPolygon(tmpPoints,iNumPoints);
Graph.FillPolygon(piecebrush, tmpPoints);
blackpen.Dispose();
}