I'm currently working on a 2D game so I'm using no layout manager:
Main.java
public class Main {
static JFrame mainframe = new JFrame();
private static JPanel pchar;
public static void main(String[] args) {
mainframe.getContentPane().setBackground(Color.DARK_GRAY);
mainframe.setLayout(null);
pchar = Characters.basiccharacter();
}
}
I designed my own JPanel because I haven't found an easy way to paint a component per pixel (Area is one of my classes as well):
PixelPanel.java
public class PixelPanel extends JPanel {
private BufferedImage img;
private int scale;
public PixelPanel(Dimension d, int scale) {
this.setLayout(null);
img = new BufferedImage(d.width * scale, d.height * scale, BufferedImage.TYPE_INT_ARGB);
this.scale = scale;
drawImage();
}
// this will draw the pixel(s) according to the set scale
public void drawPixel(int x, int y, int rgb) {
drawArea(new Area(x * scale, (x + 1) * scale, y, (y + 1) * scale), rgb);
}
public void drawArea(Area a, int rgb) {
for(int x = a.x1(); x < a.x2(); x++) {
for(int y = a.y1(); y < a.y2(); y++) {
drawTruePixel(x, y, rgb);
}
}
drawImage();
}
// this will only draw the one pixel at x : y
private void drawTruePixel(int x, int y, int rgb) {
img.setRGB(x, y, rgb);
}
private void drawImage() {
this.paintComponent(img.getGraphics());
}
}
And now I was trying to make some assets
Characters.java
public class Characters {
static int w = 10;
static int h = 12;
static int scale = 4;
public static JPanel basiccharacter() {
PixelPanel pchar = new PixelPanel(new Dimension(w, h), scale);
pchar.drawPixel(1, 2, 000);
// drawing some more pixels
return pchar;
}
}
and was to put them in my game:
Main.java
mainframe.add(pchar);
However, the JFrame is of dark gray color, but the PixelPanel is not showing up...