Peach
Peach

Reputation: 37

Java Jpanels and Gradient Backgrounds

I am an early beginner, trying to create a GUI. I have 4 basic Classes (See Below):

I found the following code to add gradient backgrounds:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            TestPanel panel = new TestPanel();
            frame.add(panel);
            frame.setSize(200, 200);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

This works when added to InnerPanel.java, but not when added to MainPanel.Java

No errors, I just get the default grey colour, Note: InnerPanel.Java is set to transparent.

So how can resolve this? Surely I can add the gradient background to the main panel and add transparent panels over it?

My 4 Classes:

public class GuiApp {
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainFrame();
            }
        });
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
class MainFrame extends JFrame {
    private MainPanel mainPanel;
    public MainFrame() {
        super("GuiApp");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(300, 500);
        // setBackground(new Color(95, 158, 160));
        setLayout(new BorderLayout());

        mainPanel = new MainPanel();
        add(mainPanel, BorderLayout.CENTER);
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
public class MainPanel extends JPanel {
    private InnerPanel panel;
    public MainPanel() {
        setLayout(new BorderLayout());
        panel = new InnerPanel();
        add(panel, BorderLayout.CENTER);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        int w = getWidth();
        int h = getHeight();
        Color color1 = Color.RED;
        Color color2 = Color.GREEN;
        GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
public class InnerPanel extends JPanel {
    public InnerPanel() {
        setBackground(new Color(0, 0, 0, 0));
        JLabel label = new JLabel("testing");
        add(label);
   }

}

Upvotes: 1

Views: 1950

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

So, you main problem is here:

setBackground(new Color(0, 0, 0, 0));

Swing only knows how to paint full transparent or full opaque components, this is control via the opaque property. By supplying a alpha based color, Swing won't know that it's suppose to paint the components the appear beneath this component when ever either is updated for some reason

Instead, you should be using setOpaque and be passing it false

InnerPanel

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class InnerPanel extends JPanel {

    public InnerPanel() {
        setOpaque(false);
        // To prove the point
        setBackground(Color.RED);
        JLabel label = new JLabel("testing");
        add(label);
    }
}

MainPanel

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

public class MainPanel extends JPanel {

    private InnerPanel panel;

    public MainPanel() {
        setLayout(new BorderLayout());
        panel = new InnerPanel();
        add(panel, BorderLayout.CENTER);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        int w = getWidth();
        int h = getHeight();
        Color color1 = Color.RED;
        Color color2 = Color.GREEN;
        GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}

GuiApp

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GuiApp {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Which, when put together generates...

Gradient Fill

Upvotes: 1

Related Questions