• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Call JFrame being partially overwritten by frame that called it

 
Fernando Hood
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a JFRAME which has button which calls another frame.

In the paint component method of the panel. If I remove the super.paintcomponent()
my new panel show bits of info from the first frame. If I leave it in the problem goes away
but I only see the last point drawn. Any ideas?
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably shouldn't have multiple frames in the first place. Please explain what you want to do.
 
Stephan van Hulst
Saloon Keeper
Posts: 11441
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fernando Hood wrote:If I leave it in the problem goes away but I only see the last point drawn.


It looks like you're using a JPanel as a canvas. This is fine, but you need to be aware of the following rules:

  • If you don't draw every pixel of the JPanel, you must override isOpaque() to return false.
  • If you don't override isOpaque() to return false, you must either draw every pixel of the JPanel, or you must call super.paintComponent(g).

  • Now, what do you mean by "I only see the last point drawn"? Can you show us your paintComponent() method?
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    but I only see the last point drawn.


    Correct. Swing components always clear  their background before doing the custom painting.

    If you want to dynamically paint multiple objects then you need to either:

    1. keep a List of objects to paint
    2. paint to a BufferedImage

    See: Custom Painting Approaches for examples of both approaches and decide which approach is best for your requirements.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am currently at work. Will post the code when i get home.  I have jframe with panel which has number of components tables, buttons and guages.
    The guages are update from data coming in the serial port.

    I have button that when I click on it a real time graph should popup. The button opens a frame in which a panel is added.

    The graph is drawn on the panel. A dot is drawn every second. I want to see all the dots not the last one.

    When I remove the super.paintcomponent() i see all the previous dots. However, pieces of the calling frame shows on the graph panel.frame.
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Will post the code when i get home.


    No reason to post your code.

    Your code is wrong and needs to be rewritten.

    You have been given two approaches to use.

    You need to read the suggestions in the link and then update your code to implement one of the approaches.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How do I implement the "isopaque override" technique? This seems much easier than the other appoach.
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    How do I implement the "isopaque override" technique?


    The easiest way is to just use setOpaque( false ).

    But it still won't help.

    Read the "Additional Paint Properties" section found in: Painting in AWT and Swing.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am using the buffer method and now there is nothing on the frame.
    Every example I look at seems to confuse me more as they constantly introduce other concepts.

     
    Paul Clapham
    Marshal
    Posts: 25143
    64
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    So none of your stuff appears in the pane? There's a good reason for that.

    Notice how the paintComponent() method has a Graphics parameter? So when the method is called, it gives you a Graphics object to draw on. The API documentation will tell you that.

    However none of your code in that method actually draws on that Graphics object. It all draws on some other Graphics object which comes from who knows where. That's what you need to fix.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am watching examples and trying to follow. I am now getting a black frame.



     
    Paul Clapham
    Marshal
    Posts: 25143
    64
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I'm guessing you mean this line of code to draw on the JPanel?

    No. You can't draw on a JPanel. You have to override the paintComponent(Graphics) method... but I thought you already knew that. You have an example which sort of does the right thing, but it needs to be fixed to use the right Graphics object, as I just said in my previous post.
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I am using the buffer method


    You are missing the concept of using a BufferedImage.

    The sole purpose of the paintComponent() method is to paint the BufferedImage.

    Other events in your code will paint ON the BufferedImage.

    I simplified the example from my link.

    The code below will simply use a Timer to paint a new "dot" to the BufferedImage every second:


    Understand how the code works and modify your code so you are able to paint your dots based on your requirements.

    Once that works, then you can modify the "createEmptyImage()" logic to paint your default graph lines onto the BufferedImage.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am learning but a lot of the stuff is not clearly explained. Many online examples lead you down the wrong path.
    There is also a lot of stuff hidden behind the scenes that is not intuitive.
    I see why many are flocking to simpler languages.I wll have crack at your code.
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    but a lot of the stuff is not clearly explained.


    Don't blame your lack of understanding on the language! Spend more time reading the tutorials and learning instead of posting unnecessary comments in the forum.

    You did NOT ask a single question about the link I provided you earlier. We can't guess what you find confusing.

    The code you posted above looked nothing like the code from the examples I gave you. Learning by example is a good way to learn. Most people would start with the working example and then customize the example to do what they want.

    Your original question complained about not being able to draw multiple dots. Yet the code you posted above had all kinds of complex code related to drawing a graph.

    Learn first how to simplify the problem and solve one problem at a time.

    The fewer the lines of code you look at, the easier it is to understand.
     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I followed your code closely and I see the dots. My task now is to piggy back on your code to achieve my objective.

    I saw that for you to draw the eclipse you did the following




    To paint my empty graph as suggested below I need to create the g2 object from the buffered image then
    use it to do the drawing? Should the casting part be doing so that scope of g2 is available to painting the empty graph?



    I am slowing making progress thanks for you assistance.
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Should the casting part be doing so that scope of g2 is available to painting the empty graph?



    Any time you want to paint to the BufferedImage you need access to the Graphics2D of that image. So you have two options:

    1. When you create the BufferedImage you also update in instance variable in your class for the Graphics2D object, the same way you do for the "image" variable
    2. You just access the Graphics2D object in each method that wants to paint to the BufferedImage

    Using the second approach you would do:

     
    Fernando Hood
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I want to draw a vertical  line on the graph and make it move from left to fright depending on the arrow keys.

    In this case I don't want to see the previous line. I want to see all that was painted before. Perhaps
    a mouse click would be better and I simply show the details (x,y ) at that point.

    Trying to  mimick 6:40 in this video.

    webpage
     
    Rob Camick
    Rancher
    Posts: 3131
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I want to draw a vertical  line on the graph and make it move from left to fright depending on the arrow keys.


    And now you are changing the requirement.

    Your first requirement was to add dots, so you could use either approach.

    Now you want to dynamically change the drawing, which means you can't use a BufferedImage.

    You need to use the other approach to dynamically recreate the drawing. So you need to use the `Draw On Component` example from the link I gave you. You will need to:

    1. keep the dots you want to paint in an ArrayList
    2. have another property that will change the location of the vertical line, something like changeLinePosition(). You would pass a positive/negative value to change the location.

     
    Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!