package graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class Rectangle extends JPanel {
private int posX, posY, width, height;
private Color fill;
public Rectangle() {
this(100, 100, 2, 2);
}
public Rectangle(int x, int y, int w, int h, Color c) {
posX = x;
posY = y;
width = w;
height = h;
fill = c;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
Color fill = new Color(255, 255, 255);
g2d.setColor(fill);
g2d.fillRect(posX, posX, width, height);
}
}
package applet;
import graphics.Rectangle;
import java.awt.Color;
import javax.swing.JApplet;
public class Applet extends JApplet {
@Override
public void init() {
Rectangle r = new Rectangle(10, 10, 80, 80, Color.WHITE);
setContentPane(r);
}
}
Chciałbym do konstruktora klasy Rectangle przekazywać również kolor fill, ale nie chce mi to działać.