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 8 of 8

Thread: Paint only part of screen

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Paint only part of screen

    Hi everyone
    I've trying to make simple 2D game in Java, which involves moving sprites around.

    However, i don't know much about paint, so I ask is there a way for paint to use only part of screen? Like on image here:



    Uploaded with ImageShack.us

    That middle square with red lines is area i would like to use with paint, and around it are JPanels that contain buttons with controls, score, etc.

    Is it possible?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Paint only part of screen

    Well, if you have JPanels surrounding the middle area, why not make the middle area a JPanel and paint to that? That should effectively limit the painting to the JPanel in the middle.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paint only part of screen

    Can you give some example? Right now i have one class, AppCore, that extend JFrame, initialize other panels, and paint is also done here.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Paint only part of screen

    Ok, well I can only provide so much because I would assume you are wanting to use BoarderLayout for this and I have no experience with JAVA Provided Layouts (I made my own, its easier).

    Basically, in your main you can create a JFrame where you can add 6 or whatever JPanels to it. All of those JPanels can be normal JPanels, except for 1, which will be a JPanel class you make.

    Instead of having a class that extends JFrame, have a class that extends JPanel. That class will be your middle JPanel in your JFrame. From there, you can override the paintComponent(Graphics g) method in your JPanel class and essentially do the exact same thing you would with your extended JFrame.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paint only part of screen

    Thank you for your advice. I will try to rewrite code, if i encounter problems, i will post them here.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paint only part of screen

    I tnink that i could get it working, but i have encountered problem.

    So, middle panel is PaintPanel (where drawing should be).

    On the left side, there is some, let say ControlPanel with some buttons, but they must be placed on specific place so i used setLayout(null), and setBounds, to place them where i want them. But ControlPanel doesnt show up, because of null layout (if i comment that line, ControlPanel shows up but buttons are aragned in line, like in FlowLayout).
    Any thoughts, how to get it done?

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Paint only part of screen

    Quote Originally Posted by bonus_clip View Post
    I tnink that i could get it working, but i have encountered problem.

    So, middle panel is PaintPanel (where drawing should be).

    On the left side, there is some, let say ControlPanel with some buttons, but they must be placed on specific place so i used setLayout(null), and setBounds, to place them where i want them. But ControlPanel doesnt show up, because of null layout (if i comment that line, ControlPanel shows up but buttons are aragned in line, like in FlowLayout).
    Any thoughts, how to get it done?
    Without code to look at, there isn't much help that can be given.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paint only part of screen

    Oh sorry. Well here is constructor for left panel:

    setLayout(null);	
    ii = new ImageIcon("green arrow.png");
    label1 = new JLabel(ii);
    JLabel label2 = new JLabel(ii);
    label1.addMouseListener(new MouseList());
    label2.addMouseListener(new MouseList());
    label1.setBounds(50, 75, 70, 70);
    label2.setBounds(50, 425, 70, 70);		
    add(label1);
    add(label2);


    And this is main class, that extends JFrame:
    setLayout(new BorderLayout());
    getContentPane().add(new LeftPointerPanel, BorderLayout.WEST);
    getContentPane().add(new PaintPanel(), BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1024, 768);
    setVisible(true);

    For some strange reasons, if i set LeftPointerPanel to BorderLayut.CENTER, it show up even with null layout!

Similar Threads

  1. Applet paint program frame work.
    By ShaunB in forum Java Swing Tutorials
    Replies: 4
    Last Post: October 31st, 2012, 08:44 AM
  2. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  3. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM
  4. HELP with Java Paint Program Code
    By CheekySpoon in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2010, 12:03 PM
  5. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM