我正在尝试用Java制作一个矩形,完成了。我也可以用单色填充它,完成。但是我实际上想更改形状本身的纯色。我知道使用Graphics可以使用g.setColor();。但是我已经以特殊方式设置了组件,如下所示:
public class Design extends JComponent {
private static final long serialVersionUID = 1L;
private List<Shape> shapesDraw = new ArrayList<Shape>();
private List<Shape> shapesFill = new ArrayList<Shape>();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
int screenHeight = gd.getDisplayMode().getHeight();
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(Shape s : shapesDraw){
g2d.draw(s);
}
for(Shape s : shapesFill){
g2d.fill(s);
}
}
public void drawRect(int xPos, int yPos, int width, int height) {
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void fillRect(int xPos, int yPos, int width, int height) {
shapesFill.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void drawTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesDraw.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public void fillTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesFill.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(screenWidth, screenHeight);
}
public int getWidth() {
return screenWidth;
}
public int getHeight() {
return screenHeight;
}
}
如您所见,它不仅使用绘图和填充,还使用列表进行绘制。有什么方法可以更改list 内的颜色吗?我最好希望每种绘制/填充形状内部的颜色都是可更改的。
谢谢您的帮助。
从答案更新:
我的类来自您的ShapeWrapper示例:
public class Design extends JComponent {
private static final long serialVersionUID = 1L;
private List<ShapeWrapper> shapesDraw = new ArrayList<ShapeWrapper>();
private List<ShapeWrapper> shapesFill = new ArrayList<ShapeWrapper>();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
int screenHeight = gd.getDisplayMode().getHeight();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(ShapeWrapper s : shapesDraw){
g2d.setColor(s.color);
g2d.draw(s.shape);
}
for(ShapeWrapper s : shapesFill){
g2d.setColor(s.color);
g2d.fill(s.shape);
}
}
public void drawRect(int xPos, int yPos, int width, int height) {
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void fillRect(int xPos, int yPos, int width, int height) {
shapesFill.add(new Rectangle(xPos, yPos, width, height));
repaint();
}
public void drawTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesDraw.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public void fillTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesFill.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(getWidth(), getHeight());
}
public int getWidth() {
return screenWidth;
}
public int getHeight() {
return screenHeight;
}
}
class ShapeWrapper {
Color color;
Shape shape;
public ShapeWrapper(Color color , Shape shape){
this.color = color;
this.shape = shape;
}
}
现在,我正在Eclipse中进行编码,除了一件事情外,其他一切都正常!
每当它说shapesDraw / shapesFill.add()时,它说:
类型List中的方法add(ShapeWrapper)不适用于参数(Rectangle)
很近!请回答。
最佳答案
您可以使用类似:
private class ShapeWrapper {
private Color color;
private Shape shape;
public ShapeWrapper(Color color , Shape shape){
this.color = color;
this.shape = shape;
}
}
而不是普通的
Shape
用于存储Shape
+ Color
。然后像下面这样绘制它们:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(ShapeWrapper s: shapesDraw){
g2d.setColor(s.color);
g2d.draw(s.shape);
}
for(ShapeWrappers s : shapesFill){
g2d.setColor(s.color);
g2d.fill(s.shape);
}
}
编辑:根据您的例外,您尝试将另一个类(
ShapeWrapper
)的对象添加到类型列表(Shape
),修复方法类似于next:public void drawRect(int xPos, int yPos, int width, int height) {
ShapeWrapper wr = new ShapeWrapper(Color.RED,new Rectangle(xPos, yPos, width, height));
shapesDraw.add(wr);
repaint();
}