1

Sorry for the very noob question, but I'm having a tremendously difficult time understanding Swing graphics and how to just, well, draw something for goodness sake.

First question is just a snippet question.

When we do this standard code in particular...

  public static void main(String[] args) {
        Jframe frame= new JFrame();  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new MyCustomJPanel());
        frame.setSize(500,500);
        frame.setVisible(true);
  }

...we set the frame pane as a new JPanel object (of class MyCustomJPanel in this case), but we don't directly name that new MyCustomJPanel object...why don't we name it? And without naming it, how can we possibly ever reference that object again?

Second question...as I mentioned, I've been studying swing graphics now for a bit but I simply can't get my mind wrapped around it. Try as I might, I simply cannot conjure up a way to draw a line at runtime on my JPanel in my JFrame. Is there no way to do this? It seems unless the line is hard-coded at compile time in my JPanel's overridden paint(g) method, runtime addition simply can't be done (without resorting to BufferedDrawings or other more advanced topics)...as there's no way to dynamically throw new code into paint(g).

Thanks, and sorry for the noobness. Trying to (re)learn OOP and Java and having a roaringly difficult time of it.

c0der
  • 18,467
  • 6
  • 33
  • 65
quietmedic
  • 13
  • 2
  • 3
    Read the section from the Swng tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html). The working examples show you how to structure your code do do the painting. The examples show how to paint a square at the location where you click. If you need more help then ask specific question about the example found in the the tutorial. You can also check out [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) for examples that allow you to dynamically paint Rectangles at run time. – camickr Dec 24 '19 at 00:54
  • 1
    *And without naming it, how can we possibly ever reference that object again?* - you can't. So if you need a reference the create a reference the same way you do with any other object. You know how to do this with the JFrame. It is no different with your custom panel. However, the main point is that you should NOT have all the code in the main() method. – camickr Dec 24 '19 at 00:58
  • 1
    Please don't ask multiple questions in one thread. It makes answers harder to find, and being a repository for good answers to clear, specific, **single** questions it what SO is all about. – Andrew Thompson Dec 24 '19 at 01:04
  • Thanks, and sorry for the multiple questions, I'll take a look at those. Part of the problem is I'm trying to use IntelliJ's GUI builder, and simply don't get how to associate the form it created with code. I already have a separate working project with a jpanel in a jframe, but I can't translate it to work with this form. There is something I am simply not getting about how an IntelliJ forum is implemented and associated with code, and I simply can't figure out how to work my code into it. Jetbrains documentation is not helpful and avoids addressing the crucial link I don't get. – quietmedic Dec 24 '19 at 01:45
  • 2
    Don't use the GUI builder. You are spending time learning the IDE and not Java/Swing and the code won't be maintainable if you ever move to another IDE. – camickr Dec 24 '19 at 02:06
  • To draw a line you need two points: a starting point (x,y) and an end point. First step: you need to be able to define these two points at run time. Drawing is the second step. See [here](https://stackoverflow.com/a/46577656/3992939) an example of drawing lines at run time. – c0der Dec 24 '19 at 06:23
  • @c0der: I appreciate it, though this I know... I already have a working program and I made with a Jframe and Jpanel that I hard-coded (not drag and drop). However, using IntelliJ's GUI designer, I now have a form, holding a frame, holding an obligate jpanel, containing a button and another jpanel, and I've no idea how to draw on a jpanel within a jpanel within a jframe within a .form, and how to associate my code with Intelli's GUI form code. I'm nearly totally lost in the details. .......@camickr, what then would you suggest for GUI creation, what is industry standard? Thanks all.. – quietmedic Dec 25 '19 at 21:18
  • I think that code ([mcve] ) would have been clearer and shorter than the attempt to describe it. To best learn gui code it manually rather than using a GUI builder. – c0der Dec 26 '19 at 05:14
  • @quietmedic there is no industry standard for GUI creation. Most common, would probably be one of the popular html/javascript frameworks. But it depends on what you are doing and what you are coding to. If it is Java, then either Swing or JavaFX is viable. If you know neither and are finding Swing difficult, then I suggest you try JavaFX instead, it works a bit differently and may suit your coding style better, though if you find Swing difficult, you will probably find the same with JavaFX. Code it first by hand, then, when comfortable, try SceneBuilder GUI builder. – jewelsea Dec 27 '19 at 14:50
  • Thanks! The intelliJ builder seems so...useful but I *cannot* get a handle of the how to work with the generated code. My code works fine when hand-coded. – quietmedic Dec 31 '19 at 21:23

1 Answers1

0

Can't help all that much without seeing the whole code, but I've heard only bad things about IntelliJ's GUI builder. I use Eclipse's Window builder, but there is also Netbeans. Both are popular for Swing, and there is also JavaFX but that's a whole different story.

For your code, when creating your JFrame class, did you write it like:

public class MainFrame extends JFrame {

public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setAlwaysOnTop(false);
                    frame.setMinimumSize(new Dimension(545, 693));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */

    public mainFrame() {


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 545, 693);


        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

//Continue adding components here...
}

Your class extends JFrame, your main method instantiates the frame, and your MainFrame class is where you add your components.

I'm not very good at explaining stuff but hope this helps.

ADSquared
  • 207
  • 3
  • 12
  • Thanks! My code doesn't quite look like that (no error checking, less robust and more elementary) but I'll pick yours apart a bit to learn better. Thanks! – quietmedic Dec 31 '19 at 21:24
  • No problem bud. If the answer is what your were looking for, please accept it so it gets closed. – ADSquared Jan 01 '20 at 04:17