I would like to fit two drawing plates in one window (one for game, other for minimap). So far I can add one Canvas directly to JFrame and paint on it.
Im stuck with adding another Canvas. I've tried adding LayoutManager to frame and add these to canvas to specific position but with no succes. Also using JPanel and LayoutManager like here: How to display two canvas in a JFrame java but it doesnt work for me, I get empty window.
To be honest, using JPanel to add one Canvas to JFrame doesnt work neither (again get empty window) so I suspect some mistake here but I have no idea what that might be.
package display;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MainWindow extends JFrame
{
Menu myMenu;
SpaceMap mySpaceMap;
JPanel generalPanel;
public MainWindow()
{
myMenu = new Menu();
mySpaceMap = new SpaceMap();
generalPanel = new JPanel();
generalPanel.setLayout(new FlowLayout());
this.getContentPane().add(generalPanel);
generalPanel.add(myMenu);
generalPanel.add(mySpaceMap);
this.setSize(500,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Title");
this.setLayout(new BorderLayout());
this.setVisible(true);
}
}
Menu and SpaceMap are defined like this (for now Menu has YELLOW color fill, and SpaceMap has BLACK):
package display;
import java.awt.*;
@SuppressWarnings("serial")
public class SpaceMap extends Canvas
{
public void paint(Graphics g)
{
setSize(200,200);
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
}
}
My Java version is 14.
I suppose that proportion between size of them has to be defined as proportion between size of each. Generaly I would like to achive something like this:
+------------------------+
| | |
| | |
| Canvas1 |Canvas2|
| | |
| | |
+------------------------+