0

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...

writzlpfrimpft
  • 333
  • 3
  • 14
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 18 '18 at 16:40
  • Everytime I've done something with LayoutManagers it failed really badly because they seemed bugged out for me... Also in my case I only want to draw pixel per pixel and put these paintings at a specific x y location, I don't want any LayoutManager + it don't has to work on every OS and screen 2) I already removed everything that I think wasn't necessary to understand what I did... – writzlpfrimpft Apr 18 '18 at 16:44
  • have you tried pChar.setVisible(true); in your main method? – Marcelo Tataje Apr 18 '18 at 16:53
  • @MarceloTataje no that's not doing anything either – writzlpfrimpft Apr 18 '18 at 16:57

1 Answers1

0

You have a couple of problems:

  1. You would never use getGraphics() and paintComponent() on a Swing component. Swing will determine when a component needs to be painted and will pass the proper Graphics object to the painting method. All you can ever do is suggest a component should be repainted by using the repaint() method.

  2. Your components have a size of (0, 0) so there is nothing to paint.

To fix these problems you need a redesign of your code. You need to better understand the basics of painting. I suggest you start by reading the section from the Swing tutorial on Custom Painting for more information and working examples to get your started.

Once you better understand how custom painting works you can decide which of the two approach you want to use.

You can use a BufferedImage to create a custom "character" to add to your game. You only need one image to represent each unique character. The next step is to decide what you want to do with your character. That is you need to paint your character on a panel. So the question is how to you want to do this?

You could:

Use the BufferedImage to create an ImageIcon and then add the ImageIcon to a JLabel and add the label to your panel. Using this approach you would need to manage the size/location of the label. The size would be the preferred size of the label and the location would change based on the rules of your game.

This approach takes advantage of using Swing components, you just provide the image to paint. You would create as many JLabels as you want, with the same or different images to represent your characters.

Or you could:

Manage the painting of your characters at specific locations on your panel manually. You would need to override the paintComponent(...) method of your panel to do the painting of each character object.

Using this approach you would keep an ArrayList of objects to paint. This object would contain at least two pieces of data:

  1. the object to paint (ie. your BufferedImage)
  2. the location of the object on the panel.

Then the logic in the paintComponent() method simply iterates through the ArrayList and paints each object. So you will obviously also need a method that allows you to add your character objects to the ArrayList.

You can check out the Draw On Component example found in Custom Painting Approaches for an example of this approach.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • `I used the paintComponent way` - that would be the more efficient approach as you manage the painting yourself instead of creating extra overhead by creating Swing components to do the painting. – camickr Apr 19 '18 at 16:42