#Running a program by extending a Class with Main From Another Package

1 messages Β· Page 1 of 1 (latest)

wraith dome
#

Several years ago, the association for computing machinery had a library for teaching java associated with the Art and Science of Java textbook. The library is not functional anymore as it was written around JApplet.

I want to create a similar library to used for teaching middle school students. One of the benefits of the library is some of the main class structure could be hidden away.

Here's what I have

  1. A Class called Main (PSVM + InvokeLater for Swing GUI)
  2. A Class called CanvasGUI (implements runnable, also has PSVM + InvokeLater)
  3. A Class named DrawBoxes which Subclasses CanvasGUI.

I think I understand the general classing but the interaction with Netbeans and Maven has my blocked.

So my Main Class and CanvasGUI can be the maven target as it has a MainClass but DrawBoxes cannot as it does not.

In the MainClass. I can call both CanvasGUI and DrawBoxes as runnable.

Help: I am looking for a way to get a simplified entry where a student can just create a class and overwrite the run function to get started.

For Example.
public class DrawBoxes extends CanvasGUI{

// Class Functions
public void run() {
    // Set Properties like Size
    //Draw Object

}

Right now netbeans will not recognize DrawBoxes from the run menu since the PSVM is in CanvasGUI not DrawBoxes.

Is it possible for Netbeans to recognize this is a subclass of something with a PSVM so it can be run from the GUI and context menu? When i'm done I hope to package everything in a jar so that students can rapidly create programs with just a run and not have to edit pom.xml or any other file in the IDE

Thanks

upbeat terraceBOT
#

<@&987246527741304832> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question πŸ€–. Maybe it helps! In any case, a human is on the way πŸ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Several years ago, the association for computing machinery had a library for teaching java associated with the Art and Science of Java textbook. The library is not functional anymore as it was written around JApplet. I want to create a similar library to

. This library included a class called GraphicsProgram which students would subclass to create graphical programs. The main method in this class handled setup and initialization of the graphics window, allowing students to focus on implementing their program logic in the run method.

NetBeans IDE simplifies the process of subclassing from a class with a main method like GraphicsProgram. When creating a new Java class in NetBeans, you can select the superclass you want to extend from. By selecting GraphicsProgram as the superclass, NetBeans automatically generates the necessary code structure for subclassing, including overriding the run method.

This feature of NetBeans saves time and effort for developers as they don't have to manually write out the subclassing boilerplate code. It allows them to quickly get started on implementing their program logic without having to worry about setting up the graphics window.

Overall, NetBeans IDE provides a convenient way for developers to subclass from classes with a main method like GraphicsProgram, simplifying the run process and allowing them to focus on their program logic. This feature is particularly useful in educational settings where students are learning Java programming concepts and want to quickly create graphical programs without getting bogged down in setup details.

wraith dome
#

Running a program by extending a Class with Main From Another Package

reef briar
#

so

#

ideally we can make it work with a simplified entrypoint too

#
void main() {

}
#

so you don't need to explain classes yet

#
import module gui;

void main() {
   Gui.init();

   Gui.draw(0, 0);
}
#

doing something like this would be a little easier

#

when i was in school we used a graphics library i should go and find

#

can you give more details?

#

maybe share what you've written so far?

#

more about the curriculum you are planning?

reef briar
#

found the lib i used in college but no examples yet

upbeat terraceBOT
#

@wraith dome

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure πŸ‘

wraith dome
#

So I think there are two problems.

  1. They managed to let GraphicsProgram be a runnable file when I did this in college almost 2 decades ago. I am sure it was just using ANT now and I'm not sure if there is a part of Maven that I need to set correctly. My subclass cannot be run from the context menu because it does not have it's own main. I would really like to have that functionality again if possible.

  2. Here is the part of the original abstract class Program that extended JApplet. I'm trying to understand how it grabs the main class. I think then if this is the entry for the maincode exec in Maven but it is able to search and run for a specific.java class and get the name and run I might be able to recreate a simplified project entry where students just subclass program and can run.

upbeat terraceBOT
reef briar
#

I think then if this is the entry for the maincode exec in Maven
If at all possible you should avoid maven at this stage

#

have them use java 22 and java Main.java will work

wraith dome
upbeat terraceBOT
reef briar
#
import acm.gui.*;
import acm.io.*;
import acm.util.*;
#

missing some imports here

wraith dome
#

I think that would be possible but I am hoping to recreate what was GUI only in netbeans back in the day

#

You didn't need them for that demo project. That was the first example on page 23 of the draft book pdf

#

But i think it does explain where I am hoping to go. A subclass without main that only overrides the run function from the super class that will create the object and set visible for the swing frame

#

What i think its happening is that the static main in program is searching for suitable files and then calling their run functions on startup. Do you think something like this is possible if it is a simple Maven project that only has the one java subclass in it in Netbeans?

reef briar
#

What i think its happening is that the static main in program is searching for suitable files and then calling their run functions on startup.
Ehhh maybe?

#

A subclass without main that only overrides the run function from the super class that will create the object and set visible for the swing frame
Well, without the full code I can't exactly try to update it from applets

#

but

#

gotta say that a subclass sucks a little

#

classes are confusing as hell

#
void run() {
    System.out.println("Running");
}


void main() {
   Program.start(this);
}
#

i can make something like this work i think

#

1 sec

wraith dome
#

I think that works if your class is runnable and that means that the very first program I would have to explain to people what implement, thread and so in

reef briar
#

no

wraith dome
reef briar
#

you would just need a method named run

wraith dome
#

But to your point, I am specifically lookign to use this framework to explain classes at the middle school level. I ran some summer programs using this and older Lego NXT mindstorms kits.

#

I would like to recreate these graphical classes because then they can learn subclassing by making different compound shapes for screensavers, games, storyboarding etc

reef briar
#

I understand. I'm trying to take explaining classes off the curriculum (until way later), which wasn't an option when that book was written

wraith dome
#

So definitely going to learn to subclass and just to defend, the goal is to prepare students to better understand First Technology Challenge and First Robotic Challenge concepts which both rely on Java

reef briar
#

classes and subclassing are super confusing and not valuable for students

#

at least not at the stage where they are still struggling with for loops

wraith dome
#

I hear you and I greatly appreciate the insight. This is a hard problem for sure and I don't have the background ofa team of 8 CS professors. Thanks for the link to the northeastern class too

reef briar
#

so, to lay out the concept here

#

you can give them this file, say "don't touch it"

#
final class Program {
    private Program() {}

    static void start(Object o) {
        try {
            var method = o.getClass()
                    .getDeclaredMethod("run");
            method.invoke(o);
        } catch (NoSuchMethodException e) {
            System.err.println("Did you define a run method?");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
#

and maybe don't look at it

#
void run() {
    System.out.println("Running");
}


void main() {
    Program.start(this);
}
#

then java Main.java will automatically work

#

(i'm still looking for a download link for the libraries you mentioned)

wraith dome
#

I believe I understand that and I see how a custom class that extends that could replace Program.

#

Do you want .jar or uncompiled files

#

I have both from 15 years ago πŸ˜„

reef briar
#

uncompiled - gotta remove applets

wraith dome
#

That's exactly what I'm trying to do

#

So I have recreated the basic graphic functionality in a Maven project. Executable jars working on the very basic demos I have created

#

If you want, I would be happy to zip up the Netbeans project I have going

#

What I love so far is that I can understand your solution above and I currently don't understand the main loop in the Program class in the ACM files

reef briar
#

you aren't doing this tommorow right?

#

like we got the summer to figure it out?

wraith dome
#

Yeah. I have time to figure out how to do this. I greatly appreciate the help.

#

I might reach out to the other members of that ACM library. Maybe some are not yet retired/emeritus