0
import javax.swing.*;
import java.awt.*;

public class TestBracket extends JComponent
{
    //Constructor
    public TestBracket ()
    {       
        super ();
        this.setPreferredSize(new Dimension(1000, 1000));
    }

    // Paint a Tournament Bracket
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        /**The First Pair*/
        g2.drawRect(100, 75, 150, 50);
        g2.drawRect(100, 125, 150, 50);

        g2.drawRect(100, 250, 150, 50);
        g2.drawRect(100, 300, 150, 50);

        g2.drawLine(100, 125, 400, 125); //Links the winner from the 1st
        g2.drawLine(100, 300, 400, 300); //Links the winner from the 2nd

        //Linking the two lines
        g2.drawLine(400, 125, 400, 150);
        g2.drawLine(400, 300, 400, 150);

        //The connector to the winners of the first pair
        g2.drawLine(400, 200, 420, 200);;

        /**The Second Pair*/
        g2.drawRect(100, 500, 150, 50);
        g2.drawRect(100, 550, 150, 50);

        g2.drawRect(100, 675, 150, 50);
        g2.drawRect(100, 725, 150, 50);

        g2.drawLine(100, 550, 400, 550); //Links the winner from the 1st
        g2.drawLine(100, 725, 400, 725); //Links the winner from the 2nd

        //Linking the two lines
        g2.drawLine(400, 550, 400, 550);
        g2.drawLine(400, 725, 400, 550);

        //The Connector to the winners of the second pair
        g2.drawLine(400, 625, 420, 625);

        /**The Third Pair*/
        g2.drawRect(420, 150, 150, 50);
        g2.drawRect(420, 200, 150, 50);

        //Line from the pair that will connect to the fourth pair
        g2.drawLine(420, 200, 650, 200);

        /**The Fourth Pair*/
        g2.drawRect(420, 575, 150, 50);
        g2.drawRect(420, 625, 150, 50);

        //Line from the pair that will connect to the third pair
        g2.drawLine(420, 625, 650, 625);

        /**Connecting the Third and Fourth Pair */
        g2.drawLine(650, 200, 650, 625);

        /**Line to Connect to the Grand Final Box */
        g2.drawLine(650, 388, 670, 388);

        /**The Grand Final Box */
        g2.drawRect(670, 338, 150, 50);
        g2.drawRect(670, 388, 150, 50);

        /**The Line That Separates The Bracket from the Player Names*/

        //g2.drawLine(850, 50, 850, 900);

    }  

}


import javax.swing.*;
import java.awt.*;

public class TestBracketTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        JPanel name = new JPanel(null);
        JLabel label = new JLabel("HI");
        label.setOpaque(true);
        label.setLocation(0, 0);
        name.add(label);

        TestBracket brack = new TestBracket();
        panel.add(smile);
        panel.add(label);

        frame.setContentPane(panel);
        frame.setTitle("Tournament Bracket");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 100);
        frame.setSize(1500, 1000);


    }

}

I'm trying to overlap a JLabel into one of the boxes in my bracket visual but it always puts it to the side. Any help?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
J. Doe
  • 1
  • 2
    One problem: You're adding components to more than one container, something that is not allowed. – Hovercraft Full Of Eels Jan 09 '17 at 14:59
  • 1
    Your best bet: don't put a JLabel but rather draw your text or image in the drawing JPanel. Otherwise you could create your own layout manager, one to handle the brackets, give your JPanel that layout manager and place your components in that JPanel. – Hovercraft Full Of Eels Jan 09 '17 at 15:02
  • 1
    Myself, I'd make the brackets a BufferedImage, draw it in the background of a drawing JPanel and then create draggable Shapes that have team names in them, and draw those on top of the BufferedImage. Another option is to use a JLayeredPane to layer components. Many ways to skin this cat. – Hovercraft Full Of Eels Jan 09 '17 at 15:10
  • 1
    If this is not a duplicate, please edit your question to include a [mcve] that shows your chosen approach. – trashgod Jan 09 '17 at 21:17

0 Answers0