0

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|
|                |       |
|                |       |
+------------------------+
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rafalini
  • 23
  • 1
  • 5
  • You're setting the size of the sub-components and most Swing layout managers don't respect sizes. Better to 1) not use Canvas but rather JPanel or subclasses of JPanel, 2) set the JPanel's preferred size via `setPreferredSize(new Dimension(...))`, or better still 3) override the JPanels `public Dimension getPreferredSize()` method. – Hovercraft Full Of Eels Apr 02 '20 at 15:42
  • Using JPanel insead Camvas and setPreferredSize(new Dimension()) instead setSize(...) doesn't change anything, still empty window appears. What would be a point in overriding public Dimension getPreferredSize()? – Rafalini Apr 02 '20 at 15:57
  • 1
    Please see [kleopatra's answer to this question](https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). She is one of the most knowledgeable Swing coders on the planet, having been principle author on one of the major commercial 3rd party Swing libraries. – Hovercraft Full Of Eels Apr 02 '20 at 16:03
  • Also, get all sizing code *out* of the painting method and into the constructor. That's madness and can lead to infinite loops. Only painting should go inside of painting methods. – Hovercraft Full Of Eels Apr 02 '20 at 16:04
  • When window is resized I would like to resize all Canvas/JPanels accordingly. Thats why setSize() is in Paint() metod, I expect it to be changed before drawing. For know there is constat value, but aim is to adopt size. – Rafalini Apr 02 '20 at 16:11
  • That's bad reasoning, since again a painting method should only contain painting code, and doing that simply won't work (as you're finding out). Consider using a GridLayout or MiGLayout (a 3rd party layout) if you want relative sizes preserved. – Hovercraft Full Of Eels Apr 02 '20 at 16:16
  • 1
    Or if you want only the left panel to expand, then use a BorderLayout as per [this code](https://pastebin.com/LvxF8Q7W) – Hovercraft Full Of Eels Apr 02 '20 at 16:19
  • Thanks a lot, such example solves the problem :) – Rafalini Apr 02 '20 at 17:18

0 Answers0