Results 1 to 10 of 10
  1. #1
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default Making a screen to draw on

    Okay im having a lot of problems just making a JFrame to draw on so im just going to step back and look for the simplest way to do this. I have my application and id like to create a JFrame in which I can draw images and whatever. However, I do not want to extend my application to JPanel because it is already extended to my Core class. How do i go about doing this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Making a screen to draw on

    You need to have a class that extends a JComponent or subclass like JPanel that can be added to the JFrame.
    Your component class could contain the Core class.

  3. #3
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default Re: Making a screen to draw on

    so i should make my Core class extend to JPanel/JComponent?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Making a screen to draw on

    That would be a solution if the Core class is where the override is for the paintComponent method where you will do the drawing.

  5. #5
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default Re: Making a screen to draw on

    Quote Originally Posted by Norm View Post
    That would be a solution if the Core class is where the override is for the paintComponent method where you will do the drawing.
    Great that works now. Also how would I make it double buffer?

    I tried this and it still flickering

    Java Code:
    public void update(long timeElapsed){
    
    		backbuffer = new BufferedImage(getScreenWidth(), getScreenHeight(), BufferedImage.TYPE_INT_RGB);
    		g2d = backbuffer.createGraphics();
    		g2d.setBackground(Color.BLACK);
    		
    		draw(g2d);
    		g2d.drawImage(Main.getImage(0), 0, 0, frame);   //frame is the JFrame as a variable
    }
    Last edited by sgthale; 09-29-2011 at 02:52 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Making a screen to draw on

    I think Swing components that extend JComponent are already double buffered.

    If you're having problems with flickering, make a small program that compiles and executes to demonstrate your problem. If you searched on the forum you'd probably find several past posts on this topic.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Making a screen to draw on

    There is no need to override an update() method when using Swing. That looks like old AWT code.

    Read Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for Swing examples.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Making a screen to draw on

    The update method shown is not an override. Wrong args.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Making a screen to draw on

    I guess my point was he is trying to make it look like AWT by creating an update(...) method. Presumably somewhere that method gets manually invoked. You should not to that.

    Custom painting is done in the paintComponent() method, which is explained in the tutorial.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Making a screen to draw on

    For double buffering, you can create the image in some method and then later draw it in the paintComponent method.

    I have no idea what the OP is doing with only the small bits of code shown.

Similar Threads

  1. Making more than one reports to screen with list
    By aborgeld in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2011, 11:22 PM
  2. Move from one screen fto other screen
    By jprgmr75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-02-2011, 06:32 PM
  3. Replies: 0
    Last Post: 12-29-2009, 03:10 PM
  4. Replies: 2
    Last Post: 04-03-2009, 05:04 AM
  5. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •