Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

I created my first Java program (wuCrypto)

Hey! I am super excited :)
I created my first Java program. I used Atom to write the code and learned on my own how to:
  • Create window GUI (JFrame)

  • Add content to the GUI (JPanel)

  • Add labels & buttons to the panel (JButton / JLabel)

  • Fetch data from a URL (HttpURLConnection, BufferedReader, Inputstreams...)

  • Parse JSON from the URL (Gson)

  • Void / static / public / private meanings & usage

  • Create an ActionListener with actionPerformed() to trigger data refresh (replaced by the auto refresher later)

  • Refresh data from API every 30 seconds (TimerTask / Timer)

  • Update the GUI with new data (JLabel setText() )

  • Package / compile a Java program from command line (javac, jar -cvmf), which was hard due to dependency/packaging issues with Gson (Google's java parser)

  • Use Launch4j to compile the .jar file as an .exe to make it easy to share the program with everyone


Here is an image of the program with some code behind it :)
[Image: 3qEfmp9.png] \

All in all, it took me about 10 hours total to learn everything & get Version 1 released. I will now be using my program to learn how to use NetBeans IDE. I think using an IDE will make life a lot easier, and it will be good for compiling / error catching. I'm sure my program has some possible errors, as I did not introduce anything for if the data API is unavailable or does not return the expected results. This may cause the program to crash or freeze. There's also a freeze that occurs if you do not have internet access. In Version 2 I hope to fix these things. From there I will be making a miniature panel that allows the user to input their own cryptocurrency identifiers (ex: Bitcoin's identifier is BTC) to track them. They can input the currency code or select from a list I will include by default. They will also be able to remove tracking for a specific currency by clicking a small X on the corner of the box when they hover over it (I think that's what I will do at least).

There is also a bug where if the crypto's prices get larger (more decimals), it pushes 'USDT' (Tether) on to the next line, which is hidden from view. The next version will auto adjust the elements so no element is ever hidden & the sizing remains the same.

I will also be updating the GUI to have a toolbar on the top that allows them to access these settings, etc. The next version will not have the red bar and will be a better looking toolbar.

If you are interested in using this, please note:
  • The public API I use only allows 100,000 API calls per month, for each IP Address

  • This means if you have this open on 2 computers at once on the same internet, the program will fail half way through the month

  • Unfortunately I do not have a way to resolve this currently

  • You should only open this program on one computer in your household/internet at a time to avoid the data rate limiting

  • The program auto refreshes every 30 seconds, allowing for around 89,000 requests per month

  • Version 2 will allow the user to choose how fast they can refresh, and show how many API calls they have remaining for the month

  • Version 2 will also submit data to my own server to provide historical crypto data to users of wuCrypto

  • This will allow users to view old crypto prices, even before they owned the software I created

  • This will also allow me to provide free price updates to users, even if they have used their 100,000 original requests - which allows users to receive updates faster because everyone is 'acting as one'... aka if User A requests the Bitcoin price, I can tell User B the new price even if User B has 0 requests left



You can download the file here:
https://wubur.com/cryptocurrency-develop...Crypto.exe

The following page also has the same data as my software wuCrypto & can be refreshed manually:
https://wubur.com/cryptocurrency-development/

If you want to make sure it's safe, I encourage you to scan it on your own. Here is an online scanner results, proving it is safe:
https://www.virustotal.com/#/url/bd928d5.../detection

I used Launch4j (unsigned) to generate the .exe from the Java .jar file, which may lead to some (about 1 out of 100) scans warning users because it's unsigned and an auto installer.

The only 'outgoing' request it makes is to fetch the cryptocurrency data, which is done here at an IP-address tracked public API (limiting each IP to 100,000 monthly requests):
https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LTC,XRP,TRX,USDT&tsyms=USD
Good first program. A few things you could look into. Swing (JFrame's, JPanel's etc.) has been deprecated for a while now, have a look at JavaFX for a more up to date approach to Java UI's. Are you making the web request and doing the JSON deserialization on the UI thread? If so, try to learn multi-threading and run computationally expensive tasks on a background thread to avoid freezing the UI.
>launch4j to make sharing easy

But jars are so easy to run if you set up the MANIFEST.MF

Nitpicking aside, nice job. Very good for a first-time project.
If you don't mind me asking, what resources did you use to learn? I'm not very good with Java tbh.
(Feb 26, 2019 03:45 PM)Khamul Wrote:  Good first program.  A few things you could look into.  Swing (JFrame's, JPanel's etc.) has been deprecated for a while now, have a look at JavaFX for a more up to date approach to Java UI's.  Are you making the web request and doing the JSON deserialization on the UI thread?  If so, try to learn multi-threading and run computationally expensive tasks on a background thread to avoid freezing the UI.
Hmm, this is good to know. I did some research & used Stackoverflow answers and recommendations to learn about JFrame/JPanel, and then I used the manual to learn how to manipulate size, add content, etc. I will update Version 2 with JavaFX I suppose to learn the more modern standard.

I have the following:
Code
 
public static void main(String[] args) throws Exception{
       //creating and showing this application's GUI.
       frame objectFrame = new frame();
       objectFrame.createAndShowGUI();
       objectFrame.refreshStats(); // reload every 10 seconds
   }

The createAndShowGUI() creates the JFrame window & then calls 'createGUIContent()' which pulls the data initially from 'setValues()', puts them in a JPanel and adds it to the frame. At that point the createGUIContent() method is finished. Then the refreshStats() is called, which just pulls the data from setValues() and uses setText() to update them in the panel. I have some private strings to hold the values of the currencies as the program runs. So to answer your question, I am not entirely sure. All of this is one class, and I have one child class called runTimer that is responsible for the 30 second refresh. refreshStats() creates an instance of runTimer every 30 seconds.

Optimization and threads is something I still have to learn, but I appreciate you bringing it up. I'm just not 100% sure of the answer yet, but I am assuming I have not done the right thing for the sake of getting it working. All in all the CPU/Memory usage is extremely low even after many hours of running, so I assume I do not have any giant memory leaks or ridiculous processes, and this is what I used (from task manager) to check efficiency.

Here is the full code:
https://pastebin.com/b6qxs5kQ

(Feb 26, 2019 07:09 PM)GenericSkid Wrote:  >launch4j to make sharing easy
But jars are so easy to run if you set up the MANIFEST.MF
Nitpicking aside, nice job. Very good for a first-time project.
Yeah, that's true. One benefit of this though is that it checks the JRE & brings the user to the official download page if they don't have the minimum required version. I'm not sure how user friendly jars are for people not familiar with Java / dependencies.

(Feb 26, 2019 11:06 PM)6Itan6imulli6 Wrote:  If you don't mind me asking, what resources did you use to learn? I'm not very good with Java tbh.
I mainly used the Java official manual, Stackoverflow, and forum posts scattered around the internet to learn everything.
I have learned PHP, MySQL, Javascript, and Node.js this way too.
My learning process is as follows:
  1. What do i want to make?

  2. Cryptocurrency price checker

  3. How do I pull data from a URL? HttpURLConnection is what I chose after trying 2 other methods

  4. How do I show this data (System.out.println - console)

  5. How do I parse this data? (I tried org.json, then settled for Gson)

  6. How do I access different objects of this parsed json string? (getAsJsonObject)

  7. How do I create a GUI? (I went with JFrame)

  8. How do I add content to this frame? (JPanel)

  9. How do I add content to a panel? (JButtons, JLabels)

  10. How do I create an interval / run a function every X seconds (Timer/Timertask)

  11. How do I update values of a JLabel? (setText)

  12. How do I compile?

  13. etc etc



Basically, once you know one language you can ask the right questions for other languages.
For that reason I could create this program in Java, Javascript/HTML, PHP/HTML, etc. If you know what you need, you can ask the right questions for a new language.
Of course, I did have some preliminary knowledge of Java. I know about ints, strings, private & public, I knew what a void method was, I knew I needed the main/initial method to start the program, and I knew I could use console/cmd to test all of the data before getting to the GUI. All in all, good questions yield a lot of answers on the internet. From there it's just down to figuring out what answers are the best and then understanding mistakes. I learned in this thread on HF that JPanel/JFrame is old & I should be using something else. So the second thing to do is to always share work when you're learning because even if a program works, it doesn't mean it's the right way to do it.
(Feb 28, 2019 07:08 AM)phlex Wrote:  Hmm, this is good to know. I did some research & used Stackoverflow answers and recommendations to learn about JFrame/JPanel, and then I used the manual to learn how to manipulate size, add content, etc. I will update Version 2 with JavaFX I suppose to learn the more modern standard.

I have the following:
Code
 
public static void main(String[] args) throws Exception{
       //creating and showing this application's GUI.
       frame objectFrame = new frame();
       objectFrame.createAndShowGUI();
       objectFrame.refreshStats(); // reload every 10 seconds
   }

The createAndShowGUI() creates the JFrame window & then calls 'createGUIContent()' which pulls the data initially from 'setValues()', puts them in a JPanel and adds it to the frame. At that point the createGUIContent() method is finished. Then the refreshStats() is called, which just pulls the data from setValues() and uses setText() to update them in the panel. I have some private strings to hold the values of the currencies as the program runs. So to answer your question, I am not entirely sure. All of this is one class, and I have one child class called runTimer that is responsible for the 30 second refresh. refreshStats() creates an instance of runTimer every 30 seconds.

Optimization and threads is something I still have to learn, but I appreciate you bringing it up. I'm just not 100% sure of the answer yet, but I am assuming I have not done the right thing for the sake of getting it working. All in all the CPU/Memory usage is extremely low even after many hours of running, so I assume I do not have any giant memory leaks or ridiculous processes, and this is what I used (from task manager) to check efficiency.

Here is the full code:
https://pastebin.com/b6qxs5kQ

Yeah, that's true. One benefit of this though is that it checks the JRE & brings the user to the official download page if they don't have the minimum required version. I'm not sure how user friendly jars are for people not familiar with Java / dependencies.

I mainly used the Java official manual, Stackoverflow, and forum posts scattered around the internet to learn everything.
I have learned PHP, MySQL, Javascript, and Node.js this way too.
My learning process is as follows:
  1. What do i want to make?

  2. Cryptocurrency price checker

  3. How do I pull data from a URL? HttpURLConnection is what I chose after trying 2 other methods

  4. How do I show this data (System.out.println - console)

  5. How do I parse this data? (I tried org.json, then settled for Gson)

  6. How do I access different objects of this parsed json string? (getAsJsonObject)

  7. How do I create a GUI? (I went with JFrame)

  8. How do I add content to this frame? (JPanel)

  9. How do I add content to a panel? (JButtons, JLabels)

  10. How do I create an interval / run a function every X seconds (Timer/Timertask)

  11. How do I update values of a JLabel? (setText)

  12. How do I compile?

  13. etc etc



Basically, once you know one language you can ask the right questions for other languages.
For that reason I could create this program in Java, Javascript/HTML, PHP/HTML, etc. If you know what you need, you can ask the right questions for a new language.
Of course, I did have some preliminary knowledge of Java. I know about ints, strings, private & public, I knew what a void method was, I knew I needed the main/initial method to start the program, and I knew I could use console/cmd to test all of the data before getting to the GUI. All in all, good questions yield a lot of answers on the internet. From there it's just down to figuring out what answers are the best and then understanding mistakes. I learned in this thread on HF that JPanel/JFrame is old & I should be using something else. So the second thing to do is to always share work when you're learning because even if a program works, it doesn't mean it's the right way to do it.

Thank you for the share buddy.
Very good for your first project. Keep going and tackle problem which seem to be "too big for you".
That will improve your coding skills.
(Mar 1, 2019 02:38 PM)Agorist Wrote:  Very good for your first project. Keep going and tackle problem which seem to be "too big for you".
That will improve your coding skills.

Yes, exactly
I will make V2 of wuCrypto with the following features, to learn them from scratch:
  • JavaFX (Have to learn it)

  • Custom window header/top

  • Splash screen on opening / initial loading

  • Custom tool bar with drop down options

  • Settings, saved locally

  • Uninstall protocol

  • Send POST data to my own server, to store crypto history from all users



After I do that I would like to convert my Electron app which I built a couple months ago (Taski) to Java, and then I will create my first 2D game in Java. I have the game planned out and it will be a pretty basic game - you control a circle/ball that is constantly falling down & have to collect certain items that appear from the bottom of the screen, and avoid others. The ball will have an animated streak/trail effect, I will use boxed item collision physics & pixel based collision, sound effects, a points system, and incorporate a few different things that alter velocity of the ball you control. It will be a great learning experience in my opinion.
If that is too hard I will start with a basic side scroller with run/jump/attack controls for interacting with stationary and some moving items/enemies. I think the sidescroller might be easier, but maybe not. It will require similar components, so I can probably go with either or.

From there I want to challenge myself by making a simple 2D game that incorporates enemy AI - logic to control the enemy more than just where they move - based on player actions.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by ImThumperJan 6, 2021 11:34 PM 2 163 Jan 6, 2021 11:34 PM
Last Post: StarZ
[.NET] by Genius At PlayFeb 10, 2015 11:03 AM 6 232 Feb 10, 2015 11:03 AM
Last Post: Genius At Play
[Java] by ziloApr 23, 2014 02:00 PM 9 687 Apr 23, 2014 02:00 PM
Last Post: zilo