Reputation: 19
I need help. I'm trying to experiment with Java GUI, and trying to draw up a Mancala Game board. Below is the code for my Mancala Board that I have so far, however, when I change the bold part to CENTER, it works just fine. How come it doesn't work when I use LINE_START or LINE_END?
The View is called in main via the following statement:
BoardView board = new BoardView();
Below is the BoardView Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class BoardView extends JFrame{
public BoardView(){
setTitle("Mancala Game");
setSize(550, 450);
setLayout(new BorderLayout(10, 10));
drawBoardPanel board = new drawBoardPanel();
**add(board, BorderLayout.LINE_START);**
JPanel footer = new JPanel();
JButton newGame = new JButton("New Game");
footer.add(newGame);
JButton quit = new JButton("Quit");
footer.add(quit);
add(footer, BorderLayout.PAGE_END);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public class drawBoardPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double p1x = 25;
double p1y = 25;
double height = 300;
double width = 50;
double arcw = 10;
double arch = 10;
g2.draw(new RoundRectangle2D.Double(p1x, p1y, width, height, arcw, arch));
}
}
}
Upvotes: 0
Views: 39
Reputation: 324157
however, when I change the bold part to CENTER, it works just fine. How come it doesn't work when I use LINE_START or LINE_END?
When you use CENTER, the components gets all the extra space available in the frame.
When you use LINE_START or LINE_END, the preferred size of the component is used to reserve space for the component.
You are doing custom painting and your component has a size of (0, 0), so there is nothing to paint.
You need to override the getPreferredSize()
of your component to give the component a size that the layout manager can use:
@Override
public Dimension getPreferredSize()
{
return new Dimension(325, 75);
}
Upvotes: 1