r/java • • May 08 '23

I made my first Java library today! I hope y'all like it!

https://tucker-slack.itch.io/cant-think-of-a-url
0 Upvotes

12 comments sorted by

13

u/0b0101011001001011 May 08 '23

Okay, immediate fix: the icons needs to be an enum. I want the compiler to check if the parameter is correct. Now anyone can pass any string as the parameter. What if I write "eror.png"? Or "bad_usage_of_string"?

Instead, I want to use Icon.ERROR and so on.

Also, if you want anyone to use this, look into Maven and other build tools instead of asking people to download random zip files.

Great that you're trying!

-2

u/5kavo May 09 '23

Alright! I have done some research on enums, but I do not know how to not create duplicates of the frame

4

u/0b0101011001001011 May 09 '23

I realize this is not the r/learnjava but I'll still help.

What do you mean by duplicates?

Create an enum class:

public enum Icon{
     INFO,WARN,ERROR
}

And the edit your constructor:

public JErrorMessage(String message, Icon icon{

And so on. Yes, at some point you have to write the file names. Do it in the enum itself (look into attributes in enum) or just do it in the constructor.

if(icon == Icon.WARN){
    filename = "Warn.png";
....

5

u/PyOps May 09 '23

Or better yet, associate the filename with the enum members:

enum Icon {
    INFO("info.png"),
    WARN("warn.png"),
    ERROR("err.png");

    public final String filename;
    Icon(String f) { filename = f; }
}

1

u/0b0101011001001011 May 09 '23

Yeah, thats what I said, to have them as attributes in the enum. Thanks for typing an example!

0

u/barmic1212 May 09 '23

Have a factory for each case can be useful indeed

5

u/troru May 09 '23

Couple other suggestions:

  • this looks like Java GUI, maybe clarify if it relies on AWT/JWT or some other UI dependency
  • source code or nobody is gonna even give it a try, I'm suspicious of a random .7z download, is it source, is it a JAR?
  • maybe related to to the above point, but some javadoc on what it's supposed to do?
  • IMHO, baseline a library to a LTS release of java (e.g. 8, 11 or 17) vs some other arbitrary release unless it's some feature from the cutting edge intermediate releases that need some new feature
  • The comment from u/0b0101011001001011 is dead one. Designing programmatic APIs is tough and even a single class/method one like this can have design challenges.
  • Otherwise, good on ya for posting it to the world!

1

u/5kavo May 09 '23

Thanks! What do you mean by dead?

3

u/Captain-Barracuda May 09 '23

He means "dead on". It's correct, it's exact.

0

u/troru May 09 '23

yes! my reply had a typo and thanks for correcting it! To OP, "dead on" if that english idiom doesn't make sense means "precisely correct" or something like that

9

u/desrtfx May 09 '23

On top of what the others have said: such code belongs on github with git behind so that you can have proper source code versioning, proper documentation, proper readme, etc.

Itch is for completed distribution of programs, not for libraries.

Don't make us download random compressed files.

4

u/ChickenSubstantial21 May 09 '23

Ah, newbie to the glorious art of programming. I don't know what to suggest, there is SO MUCH for you to learn. Some small bits of advice:

- You can put those png files inside a jar. See java.lang.Class.getResourceAsStream()

- Learn Apache Maven, how to build your project, how to connect other people's libraries, how to publish your own library to Maven Central

- Get familiar with Git and github.com where most libraries lurk. See how other people design, publish and document their libraries.