Simple problem for Swing regular costumers.
The idea is to load an image and use it as a background of a JPanel through a JComponent object. It seems to load just fine as the image information from toString() method is loading properly and the paintComponent() method is running, but for some reason it is not rendering correctly inside the JFrame leading to an empty frame. Here's the code:
RicochetFrame.java
public class RicochetFrame extends JFrame {
RicochetStartPanel startPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RicochetFrame window = new RicochetFrame();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RicochetFrame() throws IOException {
startPanel = new RicochetStartPanel();
this.getContentPane().add(startPanel);
this.setBounds(0, 0, 500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pack();
this.setVisible(true);
}
}
RicochetStartPanel.java
public class RicochetStartPanel extends JPanel {
RicochetStartPanel() throws IOException {
BufferedImage myImage = ImageIO.read(new File("frame_bg.jpg"));
this.add(new RicochetImagePanel(myImage));
this.setVisible(true);
this.validate();
this.repaint();
}
}
RicochetImagePanel.Java
public class RicochetImagePanel extends JComponent {
private Image image;
public RicochetImagePanel(Image image) {
this.setVisible(true);
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
Your RicochetImagePanel is sizing itself to its preferred size => [0, 0]. Override its getPreferredSize() to return the dimensions of the image (if not null). Better still, why not simply display the image as an ImageIcon in a JLabel?
I tried to make game with 2 panel stack each other, first panel is for canvas where all tiles and all game object is drawing on it and the second one is for the top layer frame where I have png image with transparency for the background and will contain JTextBox, JTabbedPanel and JLabel
My problems is when i repaint the canvas, part of the top frame becoming covered by a canvas.
Why is that? and also when i add JButton on canvas, the JButton is draw on top of my top frame panel.
Edit:
I have tried using JLayeredPane but still give me same result.
// initComponent from MyGame
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
JLayeredPane layered = this.getLayeredPane();
layered.add(new Canvas(), 0);
layered.add(new TopFrame(), 1);
}
This top frame and my canvas:
What I get | what I want
Main
#SuppressWarnings("serial")
public class MyGame extends JFrame {
public MyGame() {
this.initComponent();
}
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
this.add(new TopFrame());
this.add(new Canvas());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame game = new MyGame();
game.setVisible(true);
}
});
}
}
TopFrame
#SuppressWarnings("serial")
public class TopFrame extends JPanel {
private static final int FRAME_WIDTH = 320;
private static final int FRAME_HEIGHT = 240;
private BufferedImage bgImage;
public TopFrame() {
this.initComponent();
}
private void initComponent() {
this.setLayout(null);
this.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
bgImage = new BufferedImage(FRAME_WIDTH, FRAME_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
try {
bgImage = ImageIO.read(new File("FRAME.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
}
Canvas
#SuppressWarnings("serial")
public class Canvas extends JPanel {
private static final int CANVAS_WIDTH = 227;
private static final int CANVAS_HEIGHT = 240;
BufferedImage image;
BufferedImage imgBuffer;
Graphics2D g2d;
public Canvas() {
this.initComponent();
}
private void initComponent() {
Timer timer = new Timer();
this.setBounds(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
imgBuffer = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
g2d = imgBuffer.createGraphics();
try {
image = ImageIO.read(new File("tile.png"));
} catch (IOException e) {
e.printStackTrace();
}
timer.schedule(new TimerTask(){
#Override
public void run() {
drawComponent();
repaint();
}
}, 0, 100);
}
public void drawComponent() {
/* will do tile drawing */
g2d.drawImage(image, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imgBuffer, 0, 0, this);
}
}
Is it possible to get my canvas with all contained object drawn at the bottom?
[Solved] using JLayeredPane and make TopFrame setOpaque(false);
I have wrong code when try using JLayeredPane.
My final code working code now:
Main
#SuppressWarnings("serial")
public class MyGame extends JFrame {
public MyGame() {
this.initComponent();
}
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
JLayeredPane layered = this.getLayeredPane();
layered.add(new Canvas(), new Integer(0));
layered.add(new TopFrame(), new Integer(1));
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame game = new MyGame();
game.setVisible(true);
}
});
}
}
TopFrame
#SuppressWarnings("serial")
public class TopFrame extends JPanel {
private static final int FRAME_WIDTH = 320;
private static final int FRAME_HEIGHT = 240;
private BufferedImage bgImage;
public TopFrame() {
this.initComponent();
}
private void initComponent() {
this.setLayout(null);
this.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
this.setOpaque(false);
bgImage = new BufferedImage(FRAME_WIDTH, FRAME_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
try {
bgImage = ImageIO.read(new File("FRAME.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
// super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
}
Canvas
#SuppressWarnings("serial")
public class Canvas extends JPanel {
private static final int CANVAS_WIDTH = 227;
private static final int CANVAS_HEIGHT = 240;
BufferedImage image;
BufferedImage imgBuffer;
Graphics2D g2d;
public Canvas() {
this.initComponent();
}
private void initComponent() {
Timer timer = new Timer();
this.setBounds(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
imgBuffer = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
g2d = imgBuffer.createGraphics();
try {
image = ImageIO.read(new File("tile.png"));
} catch (IOException e) {
e.printStackTrace();
}
timer.schedule(new TimerTask(){
#Override
public void run() {
drawComponent();
repaint();
}
}, 0, 100);
}
public void drawComponent() {
/* will do tile drawing */
g2d.drawImage(image, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imgBuffer, 0, 0, this);
}
}
My drawing class: for example I just want to draw a simple line
public class DrawNot1 extends JPanel {
private BasicStroke BS = new BasicStroke(2);
private int x;
private int y;
public DrawNot1(int x, int y){
setSize(100, 100);
this.x = x;
this.y = y;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(BS);
g2d.drawLine(x, y, x, y+10);
}
my JFrame class:
public class Main extends JFrame{
private int x;
private int y;
public Main() {
initUI();
}
public void initUI() {
setSize(600, 500);
setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new DrawNot1(20, 20));
add(new JButton("button1"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Main ex = new Main();
ex.setVisible(true);
}
});
}
}
I want to show my drawing next to the button but that doesn't appear the only component that appear is the button, my drawing does not.
My ultimate goal is when I press the button my drawing is appear near the button.
JFrame uses a BorderLayout by default, adding two components to the default (CENTRE) position means that only the last one added will be shown.
Try adding the button to the SOUTH position instead
add(new JButton("button1"), BorderLayout.SOUTH);
You may also find the overriding the getPreferredSize method of DrawDot1 and providing a suitable value will also result in a better output
I have a 3x3 check board-like image rendered on a JPanel which is added onto a JFrame. Then I have 9 more JPanels (1 on top of each square) and on click something needs to be drawn on the corresponding square. My problem is that it only works for the top-left square. The rest of the drawings seem to be drawn below the checkboard image. So if I comment out the part that loads the checkboard image,and click as if they were there then the drawings appear correctly. I get the same result with a layered pane. Absolute positioning is used and the coordinates seem to be correct since if I remove the checkboard image then the drawings appear where they should and the drawings do not occupy more than a square.
My code is structured as follows:
'main' class creates the frame and adds an instance of another class which extends JPanel and which also draws the checkboard image using paintComponent(Graphics g).
'main' class has also 9 instances added of a class that extends JPanel and draws something on a mouse click using paintComponent(Graphics g). Each instance is placed on top of a square
Please note that because I was going to do it with just Rectangles I named the second class Rectangles but it is rectangualar JPanels not java Rectangle instances
Code:
public class Main3
{
private JFrame frame=new JFrame("");
private Rectangles rect00=new Rectangles(0,0,129,129);
private Rectangles rect01=new Rectangles(136,0,129,129);
private Rectangles rect02=new Rectangles(268,0,129,129);
private Rectangles rect10=new Rectangles(0,136,129,129);
private Rectangles rect11=new Rectangles(134,136,129,129);
private Rectangles rect12=new Rectangles(269,137,129,129);
private Rectangles rect20=new Rectangles(0,270,129,129);
private Rectangles rect21=new Rectangles(136,269,129,129);
private Rectangles rect22=new Rectangles(269,270,129,129);
public void Display()
{
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(null);
frame.setSize(600,400);
sub inter=new sub();
inter.setLayout(null);
inter.setBounds(0,0,600,400);
inter.setSize(600,400);
rect00.setBounds(rect00.getX(),rect00.getY(),rect00.getWidth(),rect00.getHeight());
rect01.setBounds(rect01.getX(),rect01.getY(),rect01.getWidth(),rect01.getHeight());
rect02.setBounds(rect02.getX(),rect02.getY(),rect02.getWidth(),rect02.getHeight());
rect10.setBounds(rect10.getX(),rect10.getY(),rect10.getWidth(),rect10.getHeight());
rect11.setBounds(rect11.getX(),rect11.getY(),rect11.getWidth(),rect11.getHeight());
rect12.setBounds(rect12.getX(),rect12.getY(),rect12.getWidth(),rect12.getHeight());
rect20.setBounds(rect20.getX(),rect20.getY(),rect20.getWidth(),rect20.getHeight());
rect21.setBounds(rect21.getX(),rect21.getY(),rect21.getWidth(),rect21.getHeight());
rect22.setBounds(rect22.getX(),rect22.getY(),rect22.getWidth(),rect22.getHeight());
rect00.setOpaque(false);
rect01.setOpaque(false);
rect02.setOpaque(false);
rect10.setOpaque(false);
rect11.setOpaque(false);
rect12.setOpaque(false);
rect20.setOpaque(false);
rect21.setOpaque(false);
rect22.setOpaque(false);
inter.add(rect00);
inter.add(rect01);
inter.add(rect02);
inter.add(rect10);
inter.add(rect11);
inter.add(rect12);
inter.add(rect20);
inter.add(rect21);
inter.add(rect22);
frame.add(inter);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String args[])
{
new main().Display();
}
private class sub extends JPanel
{
private BufferedImage image;
public sub ()
{
try
{
image=ImageIO.read(new File("image.jpg"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(600,400));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}
This is the other class
public class Rectangles extends JPanel implements MouseListener
{
private int Posx;
private int Posy;
private int width;
private int height;
private boolean selected=false;
public Rectangles(int Posx,int Posy,int width,int height)
{
this.Posx=Posx;
this.Posy=Posy;
this.width=width;
this.height=height;
this.addMouseListener(this);
}
#Override
protected void paintComponent(Graphics g)
{
if(selected==true)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
g2.setColor(new Color(250, 235, 215));
g2.drawRect(Posx,Posy,width,height);
Graphics2D g3=(Graphics2D)g;
g2.setColor(new Color(0,0,0));
g3.setStroke(new BasicStroke(20));
g3.drawLine(Posx,Posy,Posx+width,Posy+height);
g3.drawLine(Posx+width,Posy,Posx,Posy+height);
}
}
public int getX()
{
return Posx;
}
public int getY()
{
return Posy;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public void setSelected()
{
selected=true;
}
#Override
public void mouseClicked(MouseEvent arg0)
{
}
#Override
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
#Override
public void mousePressed(MouseEvent arg0)
{
}
#Override
public void mouseReleased(MouseEvent arg0)
{
selected=true;
repaint();
}
}
1) You dont honor the components paint chain.
As per java docs for paintComponent(Graphics g):
Further, if you do not invoker super's implementation you must honour
the opaque property, that is if this component is opaque, you must
completely fill in the background in a non-opaque color. If you do not
honor the opaque property you will likely see visual artifacts.
2) super.paintComponent would in most cases be the first call in the method.
3) But there is more, your cast to Graphics2D twice, that should not be done:
Graphics2D g2 = (Graphics2D) g;
...
Graphics2D g3=(Graphics2D)g;
omit the g3 its not needed you already have casted to a Graphics2D object
4) Another problem lies here in sub class. You do this in your main code:
inter.add(rect00);
inter.add(rect01);
...
but in inter which is your variable name for the instance of sub class you only have:
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
Thus it will only draw a single image no matter how many rectangles you add!
Also dont do
g2.drawLine(Posx, Posy, Posx + width, Posy + height); rather
g2.drawLine(0, 0, Posx + width, Posy + height); as the JPanel has been added at co-ordinates x and y on its container, when you draw on the JPanel we want to start at the top left i.e 0,0, changing the value would move the image further down on its conatiner
See fixed code here:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
private JFrame frame = new JFrame("");
private Rectangles rect00 = new Rectangles(0, 0, 129, 129);
private Rectangles rect01 = new Rectangles(136, 0, 129, 129);
private Rectangles rect02 = new Rectangles(268, 0, 129, 129);
private Rectangles rect10 = new Rectangles(0, 136, 129, 129);
private Rectangles rect11 = new Rectangles(134, 136, 129, 129);
private Rectangles rect12 = new Rectangles(269, 137, 129, 129);
private Rectangles rect20 = new Rectangles(0, 270, 129, 129);
private Rectangles rect21 = new Rectangles(136, 269, 129, 129);
private Rectangles rect22 = new Rectangles(269, 270, 129, 129);
public void Display() {
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(null);
frame.setSize(600, 400);
sub inter = new sub();
inter.setLayout(null);
inter.setBounds(0, 0, 600, 400);
inter.setSize(600, 400);
rect00.setBounds(rect00.getX(), rect00.getY(), rect00.getWidth(), rect00.getHeight());
rect01.setBounds(rect01.getX(), rect01.getY(), rect01.getWidth(), rect01.getHeight());
rect02.setBounds(rect02.getX(), rect02.getY(), rect02.getWidth(), rect02.getHeight());
rect10.setBounds(rect10.getX(), rect10.getY(), rect10.getWidth(), rect10.getHeight());
rect11.setBounds(rect11.getX(), rect11.getY(), rect11.getWidth(), rect11.getHeight());
rect12.setBounds(rect12.getX(), rect12.getY(), rect12.getWidth(), rect12.getHeight());
rect20.setBounds(rect20.getX(), rect20.getY(), rect20.getWidth(), rect20.getHeight());
rect21.setBounds(rect21.getX(), rect21.getY(), rect21.getWidth(), rect21.getHeight());
rect22.setBounds(rect22.getX(), rect22.getY(), rect22.getWidth(), rect22.getHeight());
rect00.setOpaque(false);
rect01.setOpaque(false);
rect02.setOpaque(false);
rect10.setOpaque(false);
rect11.setOpaque(false);
rect12.setOpaque(false);
rect20.setOpaque(false);
rect21.setOpaque(false);
rect22.setOpaque(false);
inter.addPanel(rect00);
inter.addPanel(rect01);
inter.addPanel(rect02);
inter.addPanel(rect10);
inter.addPanel(rect11);
inter.addPanel(rect12);
inter.addPanel(rect20);
inter.addPanel(rect21);
inter.addPanel(rect22);
frame.add(inter);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String args[]) {
new Test().Display();
}
private class sub extends JPanel {
private BufferedImage image;
private ArrayList<Rectangles> rects = new ArrayList<>();
public sub() {
try {
image = ImageIO.read(new File("c:/image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return (new Dimension(600, 400));
}
void addPanel(Rectangles r) {
rects.add(r);
add(r);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Rectangles r : rects) {
g.drawImage(image, r.getX(), r.getY(), null);
}
}
}
}
class Rectangles extends JPanel implements MouseListener {
private int Posx;
private int Posy;
private int width;
private int height;
private boolean selected = false;
public Rectangles(int Posx, int Posy, int width, int height) {
this.Posx = Posx;
this.Posy = Posy;
this.width = width;
this.height = height;
this.addMouseListener(this);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (selected == true) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(250, 235, 215));
g2.drawRect(0,0, width, height);
g2.setColor(new Color(0, 0, 0));
g2.setStroke(new BasicStroke(20));
g2.drawLine(0,0, width,height);
g2.drawLine(getWidth(),0, 0, height);
}
}
public int getX() {
return Posx;
}
public int getY() {
return Posy;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setSelected() {
selected = true;
}
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent arg0) {
}
#Override
public void mouseReleased(MouseEvent arg0) {
selected = true;
repaint();
}
}
A few other pointers:
Dont use Absolute/Null layout. A GridLayout or GridBagLayout would suit your needs fine. (see here for more.)
Dont do JFrame#setSize(...); rather use Correct LayoutManager and call pack() on JFrame before setting it visible.
Dont call setSize on your Rectangles instances, simply override getPreferredSize like you did with sub panel??
No need for implementing MouseListener, just use MouseAdapter thus giving you the freedom to choose which methods to override and not just override all.
Have a read on Concurrency in Swing especailly Event-Dispatch-Thread
Pretty much like the title says - I've got a JPanel, and I'd like to have it at 90% opacity if inactive, and full opacity if moused over.
I've got the separate components working - the mouse over and out is OK and I can change the opacity from overriding the paintComponent method, but I suppose I'm having trouble with the connection between the MouseAdapter and the paintComponent.
It seems there's probably a really easy way to do this - any tips?
(BTW sorry left the code at home so no example for now :( unless it's not quickly solvable then I'll throw some code up tomorrow.)
If you are overriding the paintComponent method, you should be able to add an opacity variable to the JPanel extension. Modify this in the MouseAdapter (using mutators). Then refer to this variable in the overridden paintComponent method to determine how to paint.
If you override the paintComponent method already, I'd suggest you make the MouseAdapter an anonymous inner class of your Panel and let it manipulate a private boolean flag.
public class FadingPanel extends JPanel
{
private boolean active;
public FadingPanel()
{
createMouseAdapter();
}
private void createMouseAdapter()
{
this.addMouseListener(new MouseAdapter()
{
// your mouseadapter code here toggling the active flag
}
}
#Override
public boolean paintComponent(Graphics g)
{
if(active)
{
super.paintComponent(g);
}
else
{
// your semitransparent painting code here
}
}
}
Shoot, I don't have the Haase and Guy book with me, and I don't remember the way they recommend to code for translucent components, but I guess overriding either paint or paintComponent work except that overriding paint will show the effect on child components from the get-go. For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OpaqueJPanels {
private static void createAndShowUI() {
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
mainPanel.add(new OpaqueJPanel(false, "Override paintComponent"));
mainPanel.add(new OpaqueJPanel(true, "Override paint"));
JFrame frame = new JFrame("Opaque JPanels");
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class OpaqueJPanel extends JPanel {
private static final int OJP_WIDTH = 250;
private static final int OJP_HEIGHT = OJP_WIDTH;
private static final Composite TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.4f);
private static final Composite NON_TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 1.0f);
private boolean overridePaint;
private boolean transluscent = true;
public OpaqueJPanel(boolean overridePaint, String title) {
add(new JButton("Button"));
setBorder(BorderFactory.createTitledBorder(title));
addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
transluscent = false;
getParent().repaint();
}
#Override
public void mouseExited(MouseEvent e) {
if (!OpaqueJPanel.this.contains(e.getPoint())) {
transluscent = true;
getParent().repaint();
}
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(OJP_WIDTH, OJP_HEIGHT);
}
#Override
public void paint(Graphics g) {
if (!overridePaint) {
super.paint(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paint(g);
}
#Override
protected void paintComponent(Graphics g) {
if (overridePaint) {
super.paintComponent(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paintComponent(g);
}
}
I also found that it was better if I repainted the JPanel's parent component rather than the JPanel itself.