Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Making a JPanel opaque, but showing paintComponent contents?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Making a JPanel opaque, but showing paintComponent contents?

    In my Independent Study class, I'm working on making a 2d scrolling platform game with my classmate. We're just starting out on this project and we think it'll be a huge help with learning to use the paint methods and basic game design, but we're already running into a bit of an issue. We've created a JFrame and set the background to a static, non-changing image, and we're trying to create a foreground JPanel which will contain the scrolling "platforms" which will be what our character will jump on. In order to do this, I believe we will need to make everything on the jpanel transparent, apart from the paitnComponent contents (which will be the platforms, I think). If we don't do this, wouldn't the jpanel show up as a white box and block the background image, or am I mistaken on that? Anyway, the code is below. Is there any way to have a transparent jpanel, but have the paintComponent contents still show up? Currently, all that I can see when I run the program is the background, with no black rectangle at the top.

    P.S. sorry if the formatting is a little whacked out. Transferring the code from my school computer to my home computer on google docs was a terrible idea.

     
        import java.awt.*;
        import javax.swing.*;
        import javax.imageio.*;
        import java.io.*;
     
        class BackgroundTesting extends JFrame {
            BackgroundTesting() {                                                       //Main JFrame, contains the BG and
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                         //JPanel "JPan"
                setResizable(false);
                setSize(900, 447);
                setLocationRelativeTo(null);
     
                JPanel panel = new JPan();
                add(panel);
     
                setVisible(true);
            }
     
     
            public void paint(Graphics g) {
                Image img;
                try {
                    img = ImageIO.read(new File("/Users/java/Desktop/BG.png"));         //Sets image "BG" to the
                g.drawImage(img, 0, 0, null);                                           //Background of the JFrame
     
                } catch(Exception e) {}
     
     
            }
        }
     
        class JPan extends JPanel {
            JPan() {
                setOpaque(false);
                setSize(900, 447);
                setVisible(true);
            }
     
            public void paintComponent(Graphics g) {                                    //Will contain the "scrolling"
                g.fillRect(50, 50, 25, 25);                                             //Platforms
            }
        }
     
        class Frame {
            public static void main(String[] args) {
                BackgroundTesting bgTest = new BackgroundTesting();
            }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a JPanel opaque, but showing paintComponent contents?

    Please read the Custom Painting tutorial for a better understanding of Swing's painting mechanism. Your better understanding should lead you to correct some errors in your code that are either causing or contributing to the results you're seeing. After that, a Google search should help you find basic Java tutorials for side scroller games to get you started on a firmer foundation.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Making a JPanel opaque, but showing paintComponent contents?

    Quote Originally Posted by NuclearCherry View Post
    In my Independent Study class, I'm working on making a 2d scrolling platform game with my classmate. We're just starting out on this project and we think it'll be a huge help with learning to use the paint methods and basic game design, but we're already running into a bit of an issue.
    In your code there are at least 2 things that are bad.

    1) Painting directly into JFrame is (generally) a bad choice. JFrame is complex, it contains a JRootPane which in turn contains the well known "content pane". Painting directly into JFrame, you are painting "behind" these components.

    2) Don't load images (in general any "resource") into a paint/paintComponent! These methods can be called thousand of times.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Graphics in Jpanel are not showing! Help please ! T^T
    By Toad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2013, 11:09 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. [SOLVED] JPanel not showing in JFrame
    By Tjstretch in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2011, 12:27 AM
  4. Showing JPanel error
    By Fermen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 12:12 PM
  5. Making a little Lua script 'generator', JFrame showing up blank.
    By scribbleno1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 2nd, 2010, 07:00 PM