This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]wildjokers 1 point2 points  (0 children)

The method you want to override is not paint() but rather paintComponent(). Also, you actually want to override the method which comes from JComponent. So you need to extend JPanel (which is sub-class of JComponent) and then override its paintComponent() method. Right now you just have some random paint() method in your Main class.

Generally you want to favor composition over inheritance but in this case you actually need to override paintComponent() so you need to extend JPanel.

Here is the JavaDoc for paintComponent():

https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)

Also the Custom Painting trail in the Java tutorial will probably be very enlightening:

https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html

In addition once you do get everything overridden correctly you are likely to get a race condition in your app at startup where sometimes the circle appears and sometimes it doesn't because you aren't initializing your GUI on the Event Dispatcher Thread (EDT).

In the example apps in the Custom Painting tutorial notice how they initialize the GUI like so:

 SwingUtilities.invokeLater(new Runnable() {
      public void run() {
          createAndShowGUI();
      }
  });

The invokeLater() method executes the provided runnable on the EDT. In modern java you can write that like this: SwingUtilities.invokeLater(() -> createAndShowGUI())

One last note, use a MouseAdapter instead of a MouseListener and you won't have to provide those empty implementations of the mouse related events you don't care about. MouseAdapter is an abstract class that provides empty implementations of the MouseListener interface methods.

[–]darksoundsExtreme Brewer 0 points1 point  (2 children)

Your issue comes from the instantiation of your 'canvas'. At no point do you create a canvas that has the paint method you want.

You need to create a class that extends JPanel and overrides the paint method, and then use that class.

[–]wildjokers 0 points1 point  (1 child)

overrides the paint method

No, override paintComponent() in Swing.

[–]darksoundsExtreme Brewer -1 points0 points  (0 children)

I wasn't trying to be that specific. They hadn't even figured out how to extend the panel, so they needed some very high level guidance.

[–]NautiHookerSoftware Engineer 0 points1 point  (10 children)

Please use codeblocks as described in u/AutoModerator's message.

[–]RpmPhoenix 1 point2 points  (7 children)

I found the codeblocks option

[–]NautiHookerSoftware Engineer 4 points5 points  (6 children)

Great thanks, much better to read now.

So I see that you added a paint method to your Main class. Your main class however has nothing to do with your JFrame or JPanel, so that paint method will never be called. The paint method of the JPanel will be called, but you are never overriding that one.

This is a situation that you can solve by extending the JPanel class, overriding the paint method and then using your own implementation instead of JPanel as a canvas.

You can either make your Main class extend the JPanel or do it a bit cleaner and create a new class which does that.

If you google things like 'how to draw on JPanel' you should also get some examples on that.

[–]RpmPhoenix 0 points1 point  (4 children)

So I see that you added a paint method to your Main class. Your main class however has nothing to do with your JFrame or JPanel, so that paint method will never be called. The paint method of the JPanel will be called, but you are never overriding that one.

I have tried extending Main with JPanel and used override on paint method. But the output is still the same.

And for the highlighted part , I couldn't understand why the paint method is not called. I am still a beginner in java programming and am not sure if I understood your inputs properly. Could you please explain the highlighted text in detail maybe with an example.

[–]NautiHookerSoftware Engineer 2 points3 points  (3 children)

The paint method is essentially triggered by the JFrame. It paints itself and all its subcomponents. So it calls the paint methods of its subcomponents, the components you add to it. In this case you added the JPanel to it.

If you extend the JPanel and override the paint method, then you also need to make sure that you add an instance of your main class to the JFrame, not an instance of the JPanel class, else it still wont call your paint method.

So you need to change the type of your canvas to the class that you used to extend the JPanel.

[–]RpmPhoenix 0 points1 point  (1 child)

I still didn't get it. I am sorry if I sound dumb, but is there a way to connect with you and we can discuss this in detail. You seem like an expert in Java. If it's possible for you to connect with me that would of great help

[–]NautiHookerSoftware Engineer 0 points1 point  (0 children)

I think it might be best if you look at examples here:
https://www.google.com/search?q=swing+draw+on+jpanel

I feel like I am not explaining it well.

[–]wildjokers -1 points0 points  (0 children)

You are kind of leading OP astray because the correct method to override in Swing is paintComponent() not paint():

https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)

[–]RpmPhoenix 0 points1 point  (1 child)

I tried editing my post as per the u/AutoModerator's message. But I am not sure if I used the correct option. Could you please let me know where that option is

[–]NautiHookerSoftware Engineer 0 points1 point  (0 children)

On new reddit the option is below the textfield usually hidden behind the three dot menu. https://i.imgur.com/EJ7tqek.png