我基本上有一个矩形矩阵,想分别绘制它们,但是绘制的每一个都会删除之前的任何一个,最后我得到一个最后一个孤独的矩形。而且我一直在谷歌上搜索几个小时,我发现的唯一建议是一次全部画出来,我尝试过,但似乎完全毁了我的听众,这些听众是围绕每个单独的组件构建的。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class GraphicEdge extends JComponent
{
public Rectangle box;
private Edge edge;
/**
* Creates a graphical box corresponding to the given edge at the given
* position
* @param x x coordinate
* @param y y coordinate
* @param e edge represented
*/
public GraphicEdge(int x, int y, int width, int length, Edge e)
{
this.edge = e;
this.box = new Rectangle(x, y, width, length);
}
/**
* Paints said edge. Will be recalled whenever the edge switches from
* active to inactive.
* @param g graphics.
*/
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
if (this.edge.getActive()==0)
{
g2.setColor(Color.red);
}
else
{
g2.setColor(Color.green);
}
g2.fill(this.box);
g2.draw(this.box);
}
/**
* Calls for the redrawing of the component.
*/
public void redrawComponent()
{
repaint();
}
/**
* Gets edge.
*/
public Edge getEdge()
{
return this.edge;
}
/**
* Returns the edge's rectangle.
* @return
*/
public Rectangle getBox()
{
return this.box;
}
}