0

I try to make a bubble shooter game and I have problem with drawing bubbles on MyPanel which extend JPanel. Class bubble(extend JButton) has method paintComponent:

public void paintComponent(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(qualityHints);
    g2d.setColor(c);
    g2d.fillOval(this.x,this.y,this.r, this.r);
}

How I should make constructor of MyPanel and method paint(); in MyPanel class, if I want to display Bubbles in 20 columns, 10 rows?

santiag
  • 21
  • 10
  • This may help you.. http://stackoverflow.com/questions/29947016/problems-with-detecting-mouseclick-in-a-jpanel-and-preventing-circle-from-painti/ – Subhan May 01 '15 at 10:48
  • *"Class bubble(extend JButton).."* That's where it starts to go wrong. `Bubble` should not extend anything, but have a `draw(Graphics)` method that knows where and how to draw each bubble. – Andrew Thompson May 01 '15 at 11:04
  • You woulds also need some for loops to create this bubble grid with specific offsets to create the interweaving bubbles (much like an Isometric game grid), I recommend starting here: ISOMETRICS: http://stackoverflow.com/questions/892811/drawing-isometric-game-worlds. Hexagons/Isometrics: http://www.gamedev.net/page/resources/_/technical/game-programming/isometric-n-hexagonal-maps-part-i-r747 – Daedric May 01 '15 at 11:09

2 Answers2

0

It looks like you are struggling with these bubbles for some time now: Bubble shooter game on Graphics2D, only one bubble display [closed] and Painting balloons on Graphics2D appear to be about similar problems. Please include your previous code and/or links to your related questions when you ask a new question.

A custom panel for the bubbles could look like this:

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class BubblePanel extends JPanel {
    private final List<Bubble> bubbles;

    public BubblePanel() {
        bubbles = new ArrayList<>();
        for (int rowIndex = 0; rowIndex < 10; rowIndex++)
            for (int columnIndex = 0; columnIndex < 20; columnIndex++)
                bubbles.add(new Bubble(100 + columnIndex * 60, 100 + rowIndex * 60,
                            28, Color.YELLOW));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (final Bubble bubble : bubbles)
            bubble.paintComponent(g);
    }
}

Note: as camickr already wrote in an answer to one of your previous questions, with Swing you override the paintComponent method to implement your own custom painting. You can read more about this topic in the official Java lesson about Performing Custom Painting. The paintComponent method is introduced in step 2.

Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
0

You can try using GridBagLayout(). You can easily manage columns and rows using GridBagConstraints. To know more about GridBagLayout() checkout the documentation here.

Udit Chugh
  • 681
  • 2
  • 8
  • 26