Sk4llsRPG
Sk4llsRPG

Reputation: 259

Java changing colour of shape

i'm trying to make a rectangle in Java, done. I can also fill it in with solid colour, done. But I want to actually change the solid colour of the shape itself. I know with Graphics you can use g.setColor(); but I have had my component setup a special way as shown below:

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;
}

}

As you can see, instead of just drawing and filling, it uses a list to draw off of that. Is there a way I can change the colour inside the list< shape >? I preferably want the colour to be changeable inside each draw/fill shape.

Thanks for the help.

Updated from answer:

My class as follows from your ShapeWrapper example:

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;
    }
}

Now I am coding in Eclipse and everything works fine except for ONE thing!! Every time it says shapesDraw/shapesFill.add() it says:

The method add(ShapeWrapper) in the type List is not applicable for the arguments (Rectangle)

So close! Please respond.

Upvotes: 1

Views: 2968

Answers (1)

alex2410
alex2410

Reputation: 10994

You can use something like:

private class ShapeWrapper {

    private Color color;
    private Shape shape;

    public ShapeWrapper(Color color , Shape shape){
        this.color = color;
        this.shape = shape;
    }
}

instead of plain Shape for storing Shape+Color.

And paint them like next :

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);
    }
}

EDIT: according your exception, you try to add to typed list(ShapeWrapper) an object of another class(Shape), fix your methods like 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();
}

Upvotes: 3

Related Questions