Сжимается paintComponent на GridBagLayout
Есть paintComponent (поле 10х10), размещаю его в GridBagLayout вместе с label и button, paintComponent не отрисовывается полноразмерно, сильно сжимается, в отличие от кнопок и надписей. Кто-то сталкивался с таким? Как быть? Меняла варианты размещения и растягивания именно paintComponent, но ничего не получается.
package my.seabattle.client.view;
import my.seabattle.client.GameClient;
import my.seabattle.domain.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
public class MapCreator extends JFrame implements ActionListener, MapControl.MapClickListener {
JFrame frame;
public void init() {
final String ImageBG = "C:\\SeaBattle\\seaBattle.jpg";
final ImageIcon bgI = new ImageIcon((ImageBG));
JLabel mainPanel = new JLabel(bgI){
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
Dimension ImprefSize = getLayout().preferredLayoutSize(this);
size.width = Math.max(size.width, ImprefSize.width);
size.height = Math.max(size.height, ImprefSize.height);
return size;
}
};
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame = new JFrame("Расстановка кораблей");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS));
mp = new MapControl(new ArrayList<>());
mp.setMinimumSize(new Dimension(30, 30));
mp.setMaximumSize(new Dimension(30,30));
mp.setMapClickListener(this);
//frame.setResizable(false);
frame.setSize(600, 400);
SpinnerModel model = new SpinnerNumberModel(1, 1, 4, 1);
decks = new JSpinner(model);
decks.setSize(50,50);
Font font = new Font("Century Gothic", Font.BOLD, 20);
Font fontAll = new Font("Century Gothic", Font.BOLD, 18);
JLabel nameGame = new JLabel("Морской бой");
nameGame.setVerticalAlignment(JLabel.TOP);
nameGame.setHorizontalAlignment(JLabel.CENTER);
Dimension sizelblNameGame = new Dimension(300,300);
nameGame.setPreferredSize(sizelblNameGame);
nameGame.setFont(font);
nameGame.setForeground(Color.BLACK);
SpinnerModel coordHoriz = new SpinnerNumberModel(0, 0, 9, 1);
SpinnerModel coordVert = new SpinnerNumberModel(0, 0, 9, 1);
horiz = new JSpinner(coordHoriz);
vert = new JSpinner(coordVert);
horiz.setBounds(50,50,70,70);
mainPanel.add(nameGame, new GridBagConstraints(
0,0,
9,1,
0,0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0
));
mainPanel.add(mp, new GridBagConstraints
(0, 1,
7, 7,
0, 0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(5,5,5, 5),0,0));
mainPanel.add(decks, new GridBagConstraints
(7, 1,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
mainPanel.add(new JLabel("Количество палуб"), new GridBagConstraints
(8, 1,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
mainPanel.add(horiz, new GridBagConstraints
(7, 2,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
mainPanel.add(new JLabel("Координата корабля по горизонтали"), new GridBagConstraints
(8, 2,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 5), 0, 0));
mainPanel.add(vert, new GridBagConstraints
(7, 3,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
mainPanel.add(new JLabel("Координата корабля по вертикали"), new GridBagConstraints
(8, 3,
1, 1,
0, 0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 5), 0, 0));
orient = new JCheckBox();
mainPanel.add(orient, new GridBagConstraints(
7, 4,
1,1,
0,0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0,0,5,5),0,0
));
mainPanel.add(new JLabel("Установить корабль по горизонтали"), new GridBagConstraints(
8,4,
1,1,
0,0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,5,5),0,0
));
JButton submitButton = new JButton("Добавить корабль");
submitButton.addActionListener(this);
mainPanel.add(submitButton, new GridBagConstraints(
7,5,
2,1,
0,0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0,0,5,5),0,0
));
JButton generate = new JButton("Сгенерировать карту");
generate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ships = GameClient.getInstance().createGameMap().Ships;
mp.setShips(ships);
mp.updateUI();
}
});
mainPanel.add(generate, new GridBagConstraints(
7,6,
2,1,
0,0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0,0,5,5),0,0
));
JButton sendButton = new JButton("Приступить к игре");
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameMap map = new GameMap(ships);
try {
MapCheckResult result = GameClient.getInstance().sendMap(map);
if (result.State == MapCheckResult.MapCheckState.OK) {
frame.dispose();
GameView gameView = new GameView(map);
gameView.init();
} else {
JOptionPane.showMessageDialog(null,
"Неправильная карта!",
"Предупреждение!",
JOptionPane.WARNING_MESSAGE);
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
});
mainPanel.add(sendButton, new GridBagConstraints(
7,7,
1,2,
0,0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0,0,5,5),0,0
));
JFrame.setDefaultLookAndFeelDecorated(true);
frame.add(mainPanel);
frame.setVisible(true);
ships = new ArrayList<>();
}
JSpinner horiz, vert, decks;
JCheckBox orient;
java.util.List<Ship> ships;
MapControl mp;
@Override
public void actionPerformed(ActionEvent e) {
int x = (Integer) horiz.getValue();
int y = (Integer) vert.getValue();
int d = (Integer) decks.getValue();
boolean isHoriz = orient.isSelected();
ships.add(new Ship(d, new GamePoint(x, y), isHoriz ? ShipOrientation.HORIZONTAL : ShipOrientation.VERTICAL));
mp.setShips(ships);
mp.updateUI();
}
@Override
public void click(int gameX, int gameY) {
for (int i = 0; i < ships.size(); i++) {
boolean needToDelete = false;
Ship ship = ships.get(i);
for (Ship.ShipPoint point : ship.ShipPoints) {
if (point.X == gameX && point.Y == gameY) {
needToDelete = true;
break;
}
}
if (needToDelete) {
ships.remove(i);
mp.setShips(ships);
mp.updateUI();
break;
}
}
}
}
