How can i draw on one Label with multiple classes? (Java)

RAVN Mateus :

I am currently programming a game wich includes lots of graphical elements. Currently the code looks like this:

protected void paintComponent(Graphics g)
{
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.drawImage(ImageLoader.gui_iconbar, xToRel(480), yToRel(810), xToRel(960), yToRel(270), null);
        g.drawImage(ClientVariables.getSplashart(Main.selectedCharacter, 0), xToRel(495), yToRel(825), xToRel(135), yToRel(240), null);

        //... (don't get confused with xToRel, it just makes changes to the drawing position if you move the camera)
}

The problem is: there are TONS of thise g.draw(whatever); lines. I want to split them in multiple classes but just making 4 classes and copy paste gameFrame.add(GraphicclassXY); doesn't work, logically. Does anyone know how to fix this issue? (It's not a problem if the solution includes making changes from jlabel to canvas for example)

Hovercraft Full Of Eels :

Create logical non-component classes (that don't extend JLabel, JPanel, JFrame, JComponent or other Swing component), give them a public void draw(Graphics g) method, give the class that does the drawing instances of your logical classes, perhaps within some sort of collection, and iterate through the collection and call draw on your instances within the paintComponent method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356241&siteId=1