I'm trying to create a mini version of Processing.
I created a subclass of JPanel
and tried to store the Graphics
of paintComponent()
in a variable and created a ton of functions to render everything.
Would there be a way to create a function that would work like rect(0,0,100,100);
without the Processing library? Here is my JPanel
subclass:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Screen extends JPanel{
private static final long serialVersionUID = 1L;
private static BufferedImage image;
private int x, y, w, h;
public Screen() {
repaint();
}
Graphics graphics;
public void paintComponent(Graphics g) {
graphics = g;
}
public void rect(int x, int y, int width, int height) {
graphics.fillOval(x, y, width, height);
}
public void ellipse(int x, int y, int width, int height) {
graphics.fillOval(x, y, width, height);
}
public void fill(int red, int gr, int bl) {
graphics.setColor(new Color(red, gr, bl));
}
public void fill1(int gs) {
graphics.setColor(new Color(gs,gs,gs));
}
public void background(int red, int gr, int bl) {
Color c = graphics.getColor();
graphics.setColor(new Color(red, gr, bl));
graphics.fillRect(0, 0, SnakeGame.gameSizeX, SnakeGame.gameSizeY);
graphics.setColor(c);
}
public void background(int gs) {
Color c = graphics.getColor();
graphics.setColor(new Color(gs, gs, gs));
graphics.fillRect(0, 0, SnakeGame.gameSizeX, SnakeGame.gameSizeY);
graphics.setColor(c);
}
public void text(String text, int x, int y) {
graphics.drawString(text, x, y);
}
public BufferedImage loadImage(String img) {
try {
image = ImageIO.read(getClass().getResourceAsStream("/" + img));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public void image(BufferedImage image, int x, int y, int w, int h) {
graphics.drawImage(image, x, y, w, h, null);
}
}