Problems when drawing an oval on a panel (JAVA)

Asked

Viewed 294 times

4

Greetings dear fellow programmers.

I’m encountering a somewhat irritating error: I consider drawing a graph on the screen using circular/oval shapes. After a good number of researches, I was suggested to use a class that extends Jpanel/Jcomponent (tested with both), and used a painting method using Graphics, which was called by paintComponent.

I did it in many ways, and at the end of the day, the same thing persists: Little squares painted white.

It’s being drawn this way today. I would like the panels to become circles.

This is the code I use to do such a thing. I have described in the comments for easy viewing.


Here’s the class I created based on the 300 examples of drawOval in java.

public class Bolinha extends JPanel{


public void pintar(Graphics g){
    g.setColor(Color.white);
    g.fillOval(50, 50, 20, 20);
}

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

Unfortunately I can not post more images, but using this code:

 for (int i = 0; i < g.getListaDeVertices().size(); i++){
        v1 = g.getListaDeVertices().get(i); //Pega o vértice
        Bolinha bola = new Bolinha();
        bola.setSize(30, 30);
        bola.setLocation(v1.getPosX(), v1.getPosY() + 180);
        bola.setVisible(true);
        bola.setBackground(Color.white);
        bola.add(new Label(Integer.toString(g.getListaDeVertices().get(i).getIdMatriz())));
        PainelDesenho.add(bola);
        listaDePaineis.add(p);

I end up getting the same result (squares) instead of polka dots.

I’m sorry for the lack of the other images. I thank anyone who can help! A great big hug, Momentanius.

  • A guess: on the overdone method paintComponent, calling super.paintComponent(g) and only then, paint(g).

  • 1

    A friend of mine informed the same thing, and Nothing. Very odd. I appreciate the help, still!

1 answer

1

From what I understand, the error is in every node of the graph being a new Jpanel. The function setBackground ball(Color.white); is setting the background of your Jpanel for white and therefore that becomes a white square.

One solution I can suggest, is to create a class called Graph that extends a Jpanel. This class will receive as parameter in your constructor the list of vertices and draw the respective nodes. That is, you will have only one Jpanel that will draw all the graph nodes on your Jframe, got it? An example, just to get an idea, how it would be:

  class Grafo extends JPanel {

  Lista listaDeVertices

  Grafo(Lista listaDeVertices) {

    this.listaDeVertices = listaDeVertices;

    setFocusable(true);
    setDoubleBuffered(true);
    setLayout(new FlowLayout());

    repaint();
    setVisible(true);

  }

  @Override
  public void paint(Graphics g) {

    super.paint(g);
    update(g);

  }

  @Override
  public void update(Graphics g) {

    for(int i = 0; i < listaDeVertices.size(); i++) {

      v1 = listaDeVertices.get(i);
      g.drawOval(v1.getPosX(), v1.getPosY()+180, 30, 30);

    }

  }

}
  • I wanted this to be a comment, but I don’t have enough reputation. It’s like a very average response, but okay.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.