5

Here's the context if it's necessary for any answers. I'm building an engine in which I'm going to make a videogame. It involves a 96 x 54 (columns x rows) table of letters, to keep an even spacing between them. Because of this, it would be very helpful if any solutions could be as least resource intensive as reasonably achievable. I made a web demo for this engine which worked exactly as intended, apart from being a bit slow. Now I am migrating the project over to Java, and some things aren't working quite as intended.

To mimic the HTML <table> I have used a GridLayout of JLabels inside a couple JPanels to keep the sizing constant, all inside a JFrame. The issue I'm facing is that, due to changing the size of the table to improve the look, the JLabels overlap slightly. In the web demo, this was fine, as the letters simply went into the next box. This is what I am trying to achieve in Java and cannot for the life of me find out how.

Here's an image to show you what I mean:

Here's an image to show you what I mean.

In the web demo on the left we have a circle of underscores inside a circle of "a"s, and the letters "pqyjg" on the right. There is also a small grey box on one of the underscores. This is the highlighted box below the underscore, showing that underscore overlaps 1 pixel into it.

When we put this same code into the Java version, the underscores are nowhere to be seen, and the tails on the letters "pqyjg" are cut off. The desired effect is for it to work like the example on the left.

I've searched this site, the rest of the internet and through many Java class pages in search of a method that will help, to no avail.

Can anyone point me to a class or method I could invoke on the JLabels or any other component to achieve this effect, without changing the size of the table?

Here is my current code for setting everything up in case it helps anyone.

import javax.swing.*;
import static java.lang.Math.*;
import java.awt.*;

public class transparencyExample{

    //Declaring constants
    public static final Color[] MAINFRAME = {new Color(0x35ce4a), new Color(0x111111)};

    //Creating static variables and methods
    private static JLabel tempLabel;
    private static JLabel[][] table = new JLabel[54][96];
    private static JPanel layout = new JPanel(new GridLayout(54,96));
    private static JPanel background = new JPanel();
    private static BoxLayout box = new BoxLayout(background, 0);
    private static JFrame frame = new JFrame("Transparency Example");
    private static void initialise(){

        //Adding labels to table
        for (int i = 0; i < 5184; i++){
            tempLabel = new JLabel("M", SwingConstants.CENTER);
            tempLabel.setFont(new Font("Courier", Font.PLAIN, 15));
            table[(int) floor((double) i / 96)][i % 96] = tempLabel;
        }

        //Laying out the table
        layout.setPreferredSize(new Dimension(1056, 594));
        layout.setOpaque(false);
        for(int i = 0; i < 5184; i++){
            layout.add(table[(int) floor((double) i / 96)][i % 96]);
        }
        background.setBackground(MAINFRAME[1]);
        background.add(layout);

        //Laying out the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(background);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    //Fill table
    public static void fill(String s){
        for(int i = 0; i < 5184; i++){
            table[(int) floor((double) i / 96)][i % 96].setText(String.valueOf(s.charAt(i)));
        }
    }
    public static void main(String[] args){
        initialise();
        transparencyExample.fill("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         aaaaaaaa                                                                                       a   a    aa                                                                                    a _____     a                                                                                  a _     ___  a                                                                                 a_         __  a                                                                               a _           _ a     pqyjg                                                                   aa _            _ a                                                                             a  _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a    _           _  a                                                                           a    __          _ a                                                                            a      __      __  a                                                                             a       _______   a                                                                             a                a                                                                              a               aa                                                                               aa            a                                                                                   aa       aa                                                                                       aa aaaa                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ");
    }
}
  • Hello and welcome. For the purposes of helping us better understand your problem, please consider simplifying and improving your posted code such that it 1) has no code inside of it not related to your problem, 2) very small, 3) compiles and runs unmodifed, that is, it is a whole class / program on its own and 4) demonstrates your problem for us, a [mcve]. This code should be posted here, as you are currently doing, and not in a link. – Hovercraft Full Of Eels Aug 03 '16 at 14:13
  • For instance, I cannot currently run your code above as it has dependencies that I do not have access to, including MAINFRAME, floor(...), an image resource,... and it is not posted as a compilable and runnable class, complete with class heading and imports. – Hovercraft Full Of Eels Aug 03 '16 at 14:14
  • ... and I'm not 100% as to the effect you're trying to create and the problems that you may be having, but often problems in this category are due to trying to artificially constrain the sizes of components and containers. – Hovercraft Full Of Eels Aug 03 '16 at 14:18
  • I've edited it so it should compile and run correctly. I've tried compiling and running it and it seems to work fine for me. Hopefully that's what you were asking for. – The G-Meister Aug 03 '16 at 14:29

2 Answers2

3

Per my comment:

... and I'm not 100% as to the effect you're trying to create and the problems that you may be having, but often problems in this category are due to trying to artificially constrain the sizes of components and containers

Yes, your problem is that you're artificially constraining the size of your layout JPanel with layout.setPreferredSize(new Dimension(1056, 594));, and this is preventing all the components that it holds to be well displayed. If you get rid of that line, and then you pack and setVisible(true) your JFrame, after filling it with data, you'll see the full text.


Your comment:

I was trying to keep the size of the table the same, but instead let the JLabels overlap so that their text can overflow from one into the other.

Consider option 2: don't use JLabels but rather draw your desired text directly into either a BufferedImage that is displayed in your JPanel's paintComponent method or directly within the JPanel's paintComponent method itself.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I was trying to keep the size of the table the same, but instead let the `JLabel`s overlap so that their text can overflow from one into the other. I'll try and make that more clear in the post, sorry. – The G-Meister Aug 03 '16 at 14:58
  • How intensive is that? And the idea was to make the text easily editable, would this method allow that? – The G-Meister Aug 03 '16 at 15:09
  • 1
    If you want overlap, a dirty way would be to remove the Layout from `layout` (set it null) and to position JLabel with coordinates (should work because of the fixed size of the container) – Quentin Morrier Aug 03 '16 at 15:10
  • @TheG-Meister: you can edit it just like any other non text component GUI text. If you're MVC is well written, it is easy to modify and edit. – Hovercraft Full Of Eels Aug 03 '16 at 15:13
1

If I've well understood, one part of your question is how to set the background color of a JLabel? If so then:

label.setOpaque(true);
label.setBackground(Color.lightGray);// Background color

I've modified your code to allow resize of the entire window. Then problem is visible when the font size is to big to fit in the label space. In my opinion you should calculate the font size depending of the label size, something similar to this: https://stackoverflow.com/a/19194949/4448511

import javax.swing.*;
import static java.lang.Math.*;
import java.awt.*;



public class testG {

    //Declaring constants
    //public static final Color[] MAINFRAME = {new Color(0x35ce4a), new Color(0x111111)};
    public static final Color[] MAINFRAME = {Color.GREEN, Color.BLACK};

    //Creating static variables and methods
    private static JLabel tempLabel;
    private static JLabel[][] table = new JLabel[54][96];
    private static JPanel layout = new JPanel(new GridLayout(54,96));
    private static JPanel background = new JPanel(new BorderLayout());
    private static BoxLayout box = new BoxLayout(background, 0);
    private static JFrame frame = new JFrame("Transparency Example");
    private static void initialise(){

        //Adding labels to table
        for (int i = 0; i < 5184; i++){
            tempLabel = new JLabel("M", JLabel.CENTER);
            //tempLabel.setFont(new Font("Courier", Font.PLAIN, 15));
            tempLabel.setForeground(Color.GREEN);
            tempLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            table[(int) floor((double) i / 96)][i % 96] = tempLabel;

        }

        //Laying out the table
        //layout.setPreferredSize(new Dimension(1056, 594));
        layout.setOpaque(false);
        for(int i = 0; i < 5184; i++){
            layout.add(table[(int) floor((double) i / 96)][i % 96]);
        }
        background.setBackground(MAINFRAME[1]);
        background.add(layout, BorderLayout.CENTER);

        //Laying out the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(background);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    //Fill table
    public static void fill(String s){
        for(int i = 0; i < 5184; i++){
            JLabel labelTemp = table[(int) floor((double) i / 96)][i % 96];
            labelTemp.setText(String.valueOf(s.charAt(i)));
        }
    }
    public static void main(String[] args){
        initialise();
        testG.fill("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         aaaaaaaa                                                                                       a   a    aa                                                                                    a _____     a                                                                                  a _     ___  a                                                                                 a_         __  a                                                                               a _           _ a     pqyjg                                                                   aa _            _ a                                                                             a  _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a    _           _  a                                                                           a    __          _ a                                                                            a      __      __  a                                                                             a       _______   a                                                                             a                a                                                                              a               aa                                                                               aa            a                                                                                   aa       aa                                                                                       aa aaaa                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ");
    }

}
Community
  • 1
  • 1
Quentin Morrier
  • 675
  • 5
  • 14
  • If you look in the image and code, I've already done that, sorry. What did you read that made you think that, by the way? If I can clear that up anywhere it would prevent other people from making the same mistake. – The G-Meister Aug 03 '16 at 14:54