Transparent JFrame with non-transparent content












0















My goal is to have a transparent JFrame with a non-transparent JPanel that is constantly drawing a square at a random place



private static final int alpha = 255;

public static void main(String args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);

frame.setUndecorated(true);
frame.setBackground(new Color(255, 255, 255, alpha));

CustomPanel panel = new CustomPanel();

panel.setBackground(new Color(255, 255, 255, 0));

new Timer().schedule(new TimerTask() {

@Override
public void run() {

panel.revalidate();
panel.repaint();

}

}, 0, 1000);

frame.add(panel);

frame.setVisible(true);

}

public static class CustomPanel extends JPanel {

private static final long serialVersionUID = 1L;

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(new Color(0, 255, 0));
g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);
}

}


Currently, the frame is rendered with a white background and the square is drawn correctly.



enter image description here



However, if I change the value of variable alpha to, for example, 31, the clearRect call will only clear the panel with 31 alpha, and the past frame will still be visible



enter image description here



Left: 1 Iteration
Right: 4 Iterations



As you can see, the frame will be drawn 4 times and the previous instance will still be visible.



How can I draw the transparent frame without the old frame being visible?



My OS is Ubuntu 18.04










share|improve this question





























    0















    My goal is to have a transparent JFrame with a non-transparent JPanel that is constantly drawing a square at a random place



    private static final int alpha = 255;

    public static void main(String args) {

    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);

    frame.setUndecorated(true);
    frame.setBackground(new Color(255, 255, 255, alpha));

    CustomPanel panel = new CustomPanel();

    panel.setBackground(new Color(255, 255, 255, 0));

    new Timer().schedule(new TimerTask() {

    @Override
    public void run() {

    panel.revalidate();
    panel.repaint();

    }

    }, 0, 1000);

    frame.add(panel);

    frame.setVisible(true);

    }

    public static class CustomPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.clearRect(0, 0, getWidth(), getHeight());
    g.setColor(new Color(0, 255, 0));
    g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);
    }

    }


    Currently, the frame is rendered with a white background and the square is drawn correctly.



    enter image description here



    However, if I change the value of variable alpha to, for example, 31, the clearRect call will only clear the panel with 31 alpha, and the past frame will still be visible



    enter image description here



    Left: 1 Iteration
    Right: 4 Iterations



    As you can see, the frame will be drawn 4 times and the previous instance will still be visible.



    How can I draw the transparent frame without the old frame being visible?



    My OS is Ubuntu 18.04










    share|improve this question



























      0












      0








      0








      My goal is to have a transparent JFrame with a non-transparent JPanel that is constantly drawing a square at a random place



      private static final int alpha = 255;

      public static void main(String args) {

      JFrame frame = new JFrame();

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 400);
      frame.setLocationRelativeTo(null);

      frame.setUndecorated(true);
      frame.setBackground(new Color(255, 255, 255, alpha));

      CustomPanel panel = new CustomPanel();

      panel.setBackground(new Color(255, 255, 255, 0));

      new Timer().schedule(new TimerTask() {

      @Override
      public void run() {

      panel.revalidate();
      panel.repaint();

      }

      }, 0, 1000);

      frame.add(panel);

      frame.setVisible(true);

      }

      public static class CustomPanel extends JPanel {

      private static final long serialVersionUID = 1L;

      @Override
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.clearRect(0, 0, getWidth(), getHeight());
      g.setColor(new Color(0, 255, 0));
      g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);
      }

      }


      Currently, the frame is rendered with a white background and the square is drawn correctly.



      enter image description here



      However, if I change the value of variable alpha to, for example, 31, the clearRect call will only clear the panel with 31 alpha, and the past frame will still be visible



      enter image description here



      Left: 1 Iteration
      Right: 4 Iterations



      As you can see, the frame will be drawn 4 times and the previous instance will still be visible.



      How can I draw the transparent frame without the old frame being visible?



      My OS is Ubuntu 18.04










      share|improve this question
















      My goal is to have a transparent JFrame with a non-transparent JPanel that is constantly drawing a square at a random place



      private static final int alpha = 255;

      public static void main(String args) {

      JFrame frame = new JFrame();

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 400);
      frame.setLocationRelativeTo(null);

      frame.setUndecorated(true);
      frame.setBackground(new Color(255, 255, 255, alpha));

      CustomPanel panel = new CustomPanel();

      panel.setBackground(new Color(255, 255, 255, 0));

      new Timer().schedule(new TimerTask() {

      @Override
      public void run() {

      panel.revalidate();
      panel.repaint();

      }

      }, 0, 1000);

      frame.add(panel);

      frame.setVisible(true);

      }

      public static class CustomPanel extends JPanel {

      private static final long serialVersionUID = 1L;

      @Override
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.clearRect(0, 0, getWidth(), getHeight());
      g.setColor(new Color(0, 255, 0));
      g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);
      }

      }


      Currently, the frame is rendered with a white background and the square is drawn correctly.



      enter image description here



      However, if I change the value of variable alpha to, for example, 31, the clearRect call will only clear the panel with 31 alpha, and the past frame will still be visible



      enter image description here



      Left: 1 Iteration
      Right: 4 Iterations



      As you can see, the frame will be drawn 4 times and the previous instance will still be visible.



      How can I draw the transparent frame without the old frame being visible?



      My OS is Ubuntu 18.04







      java swing jframe transparency






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:19







      nathanthesnooper

















      asked Nov 26 '18 at 1:28









      nathanthesnoopernathanthesnooper

      84111




      84111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          For a transparent panel don't use a transparent color. Swing does not paint backgrounds with transparency correctly. See Background With Transparency for more information.



          However, for full transparency there is a simple solution. Just make the panel transparent:



          //panel.setBackground(new Color(255, 255, 255, 0));
          panel.setOpaque( false );


          Then in the painting code you would use:



          super.paintComponent(g);
          g.setColor(new Color(0, 255, 0));
          g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);


          Note however you should NOT be using random values in a painting method. You can't control when or how often Swing will repaint a component.



          Instead you need a property of the class to be the Color for the Rectangle. Then you need a method like setSquareColor(...) when you want to change the Color of the square.






          share|improve this answer


























          • After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

            – nathanthesnooper
            Nov 26 '18 at 1:55













          • @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

            – camickr
            Nov 26 '18 at 4:20











          • 1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

            – nathanthesnooper
            Nov 26 '18 at 5:15











          • @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

            – camickr
            Nov 26 '18 at 16:23













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473692%2ftransparent-jframe-with-non-transparent-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          For a transparent panel don't use a transparent color. Swing does not paint backgrounds with transparency correctly. See Background With Transparency for more information.



          However, for full transparency there is a simple solution. Just make the panel transparent:



          //panel.setBackground(new Color(255, 255, 255, 0));
          panel.setOpaque( false );


          Then in the painting code you would use:



          super.paintComponent(g);
          g.setColor(new Color(0, 255, 0));
          g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);


          Note however you should NOT be using random values in a painting method. You can't control when or how often Swing will repaint a component.



          Instead you need a property of the class to be the Color for the Rectangle. Then you need a method like setSquareColor(...) when you want to change the Color of the square.






          share|improve this answer


























          • After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

            – nathanthesnooper
            Nov 26 '18 at 1:55













          • @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

            – camickr
            Nov 26 '18 at 4:20











          • 1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

            – nathanthesnooper
            Nov 26 '18 at 5:15











          • @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

            – camickr
            Nov 26 '18 at 16:23


















          0














          For a transparent panel don't use a transparent color. Swing does not paint backgrounds with transparency correctly. See Background With Transparency for more information.



          However, for full transparency there is a simple solution. Just make the panel transparent:



          //panel.setBackground(new Color(255, 255, 255, 0));
          panel.setOpaque( false );


          Then in the painting code you would use:



          super.paintComponent(g);
          g.setColor(new Color(0, 255, 0));
          g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);


          Note however you should NOT be using random values in a painting method. You can't control when or how often Swing will repaint a component.



          Instead you need a property of the class to be the Color for the Rectangle. Then you need a method like setSquareColor(...) when you want to change the Color of the square.






          share|improve this answer


























          • After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

            – nathanthesnooper
            Nov 26 '18 at 1:55













          • @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

            – camickr
            Nov 26 '18 at 4:20











          • 1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

            – nathanthesnooper
            Nov 26 '18 at 5:15











          • @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

            – camickr
            Nov 26 '18 at 16:23
















          0












          0








          0







          For a transparent panel don't use a transparent color. Swing does not paint backgrounds with transparency correctly. See Background With Transparency for more information.



          However, for full transparency there is a simple solution. Just make the panel transparent:



          //panel.setBackground(new Color(255, 255, 255, 0));
          panel.setOpaque( false );


          Then in the painting code you would use:



          super.paintComponent(g);
          g.setColor(new Color(0, 255, 0));
          g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);


          Note however you should NOT be using random values in a painting method. You can't control when or how often Swing will repaint a component.



          Instead you need a property of the class to be the Color for the Rectangle. Then you need a method like setSquareColor(...) when you want to change the Color of the square.






          share|improve this answer















          For a transparent panel don't use a transparent color. Swing does not paint backgrounds with transparency correctly. See Background With Transparency for more information.



          However, for full transparency there is a simple solution. Just make the panel transparent:



          //panel.setBackground(new Color(255, 255, 255, 0));
          panel.setOpaque( false );


          Then in the painting code you would use:



          super.paintComponent(g);
          g.setColor(new Color(0, 255, 0));
          g.fillRect((int)(Math.random() * 380), (int)(Math.random() * 320), 20, 20);


          Note however you should NOT be using random values in a painting method. You can't control when or how often Swing will repaint a component.



          Instead you need a property of the class to be the Color for the Rectangle. Then you need a method like setSquareColor(...) when you want to change the Color of the square.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 1:43

























          answered Nov 26 '18 at 1:32









          camickrcamickr

          276k16127239




          276k16127239













          • After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

            – nathanthesnooper
            Nov 26 '18 at 1:55













          • @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

            – camickr
            Nov 26 '18 at 4:20











          • 1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

            – nathanthesnooper
            Nov 26 '18 at 5:15











          • @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

            – camickr
            Nov 26 '18 at 16:23





















          • After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

            – nathanthesnooper
            Nov 26 '18 at 1:55













          • @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

            – camickr
            Nov 26 '18 at 4:20











          • 1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

            – nathanthesnooper
            Nov 26 '18 at 5:15











          • @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

            – camickr
            Nov 26 '18 at 16:23



















          After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

          – nathanthesnooper
          Nov 26 '18 at 1:55







          After applying your changes, there is no difference. The problem is not within the panel. The problem is that when my JFrame is transparent, the paint method draws over the old frame, since the clear method only clears a small amount of alpha. EDIT: I get the same issue with this pastebin.com/HPEVcfNe

          – nathanthesnooper
          Nov 26 '18 at 1:55















          @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

          – camickr
          Nov 26 '18 at 4:20





          @nathanthesnooper, 1) The alpha color suggestion would also apply to the frame. Instead you should be using the setOpacity(...) method of the JFrame 2) You should be using a Swing Timer not an AWT Timer. Swing components should be updated on the Event Dispatch Thread. 3) Your code doesn't compile there is no class or import statements.

          – camickr
          Nov 26 '18 at 4:20













          1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

          – nathanthesnooper
          Nov 26 '18 at 5:15





          1) setOpacity also effects the content, and I want the content to have full opacity. 2) I have already fixed this and it does not seem to make a difference. 3) I ommited the class declaration and the import statements as they do not contribute to the question

          – nathanthesnooper
          Nov 26 '18 at 5:15













          @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

          – camickr
          Nov 26 '18 at 16:23







          @nathanthesnooper, I ommited the class declaration and the import statements as they do not contribute to the question - yes they are relevant so we can easily copy/paste/compile/test to verify your code. See: Minimal, Complete, and Verifiable example for what is expected in this forum when posting code. setOpacity also effects the content - not with the code I have on my machine (JDK 8 on Windows 7) it doesn't. If you post a proper "MCVE" maybe people using your environment will test it and people not using your environment can verity if it works or not.

          – camickr
          Nov 26 '18 at 16:23






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473692%2ftransparent-jframe-with-non-transparent-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'