2

I'm making a 2d vertical shooter game, in which everything is coded (and working) but the graphics. I have not used the Graphics classes before, so this is all new to me. The following is the code I use to paint everything to the JFrame:

public void paintAll()
{
    Graphics h = new Graphics2D();
    for(Bullet j : GameState.getEnBullets()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Enemy j : GameState.getEnemies()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Bullet j : GameState.getPlayBullets()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    this.paint(h);
}

The first line "Graphics h = new Graphics2D();" produces an error because Graphics2d is abstract, but I have no idea where to go from here.

I need the code to take all the images that I have and paint them to the points in the JFrame. I remind you that I have never done this before, so please tell me if this is the wrong way to do this.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Will
  • 179
  • 3
  • 3
  • 6
  • 2
    Instead of doing custom painting to the `JFrame` itself, it is better to add a `JComponent` or `JPanel` (if there are other components to include). Just when you think that painting in a top-level container is best, you realize you want that rendering in a full-screen `JWindow`, or a `JDialog`, or in a `JInternalFrame`, or in the `CENTER` of a `BorderLayout` in another `JPanel`, or.. – Andrew Thompson Jun 07 '11 at 05:50

2 Answers2

6

Override paintComponent() instead; it will supply the Graphics context. You can cast it to a Graphics2D.

Graphics2D g2d = (Graphics2D) g;

Addendum: This assumes that you are overriding paintComponent() in a JComponent, which is then added to the JFrame.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Instead of calling `paint()`, see how this [example](http://stackoverflow.com/questions/3256269/jtextfields-on-top-of-active-drawing-on-jpanel-threading-problems/3256941#3256941) uses `repaint()` in a Timer's `actionPerformed()` method. – trashgod Jun 07 '11 at 04:44
  • @Andrew Thompson: actually he is partially right. What he says to do works for the paintComponent(Graphics g), which is likely what he intended to say. – Will Jun 07 '11 at 06:58
  • 1
    just upvote, a few threads on this forum are there with huge amount of infos about that, but you were right in original post, before 1st. amendment, for JFrame (RootPane) is there only paint() +1 – mKorbel Jun 07 '11 at 18:15
4

in connections with Will's second thread (my helicopter view) about same thing Error with timer and JFrame

and correct intuition by Andrew Thompson's magics globe then

I added (I hope that's correctly, because I'm not familair with paint, paintComponent or paintComponents together with custom Graphics)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MinimumSize extends JFrame {

    private static final long serialVersionUID = 1L;

    public MinimumSize() {
        setTitle("Custom Component Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponent());
        pack();        
        setMinimumSize(getSize());// enforces the minimum size of both frame and component
        setVisible(true);
    }

    public static void main(String[] args) {
        MinimumSize main = new MinimumSize();
        main.display();
    }
}

class CustomComponent extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Good example of extending [`JComponent`](http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html) and overrriding `paintComponent()`. There's no UI delegate at this level, but there _is_ double buffer support. Previously up-voted, BTW. – trashgod Jun 07 '11 at 20:30