Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can you think of a better way to do this? (I'm desperate!)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Can you think of a better way to do this? (I'm desperate!)

    Hi, I'm making a gui with swing for a physics project. I'm really in trouble with this, I was supposed to hand it in last week (I have an extension for monday), because the coding part is taking so long since my strategy to do this is probably very bad. Please, any input / help is very appreciated. It looks like this so far:

    -link removed- (I tried using the "insert image" feature but it was too small to see)

    So there three buttons on the bottom called Big, Between, and Small. When the user clicks the "Big" button, the side panels look how they are in the picture. Depending what the user clicks on the side panels, the center panel changes to a screen where theres information on that subject, videos, etc.

    I thought I could tackle this my own way (having limited knowledge of swing, no openGL, etc). But it is getting really complicated. What I'm doing is, each section of the interface (left, center, right, top) are their own JLabels, with an ImageIcon as setIcon(). Lets say for the center panel, where there are three buttons (big, between, and small) I have four different pictures, in which one picture is with no buttons selected, and then one picture for each button being selected.

    Then on top of the JLabel, I have a mouse listener to determine when the user clicks, and where. I have two "types" of switch statementss. I use one 'type' of switch statements for all the positions of the buttons to determine where the use clicks, and then depending on where they click the corresponding code. The other 'type' of switch statement determines what state the program is in(for example, if state = 2, then use the switch statement for for the buttons that belong to "big" (universe, galaxy, stars, future tech, etc).

    Using this method, I currently have over 120 pictures and over 30 switch statements, with around 10+ cases for each of them.

    --------

    How would you do this? My method is not going to work. Basically I'm doing this to make an "attractive" gui (I realize my gui is ugly lol).

    Note I have my buttons as part of the background image (which is why I have to have a picture for every possible combination) because they have transparency on the edges. I don't believe if I create imageIcons for the buttons I will keep the transparency of them.

    I understand the right way to do this would be to use open GL, but I don't know it and I don't really have the time to do so.

    Any input at all is greatly appreciated, Thanks!

    edit: if you want too see what I mean by having an image for each possible combination, an early test of my program is here:

    note in this version I had an arrow over the button in which the mouse was hovering over, so even though theres only three buttons, I had 14 pictures for all the different combinations. (E.g. click the "The Very Big" Button and then hover your mouse of the "everything inbetween" and "the very small" buttons. You can see the Very Big button's image changes a bit.)
    Attached Images Attached Images
    Last edited by jps; June 1st, 2013 at 11:22 PM. Reason: link removed


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can you think of a better way to do this? (I'm desperate!)

    I have read your post over again and still do not quite understand what you are doing. Can you explain more, perhaps more about the flow, what is going on and what pictures are changing.
    Do you need 14 pictures? If so use them. If you are using completely new pictures to draw a border around the old picture, then you have too many pictures. If they are very different images, then it would seem they are individually necessary to get the desired display.
    If I understand correctly, on the part about the mouse listener being used to detect button clicks by determining position, you can draw an image and draw buttons over top of that image that still work as buttons, detect being clicked and messaging registered action listeners. Perhaps drawing buttons on different backgrounds would solve some of the multiple image issues.

    Then again I am not quite sure I understand what is going on in the code from the description. Perhaps post some of the code used for the images and such, (the stuff that makes you say its getting complex), perhaps the code can shed some light

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can you think of a better way to do this? (I'm desperate!)

    Hi, Ill try to explain what I'm doing and how I'm doing it better. And sorry if posting my .jar is not allowed (if thats why the link was removed). I'm not very good at explaining things...

    My program gives information on the physics behind various subjects. I have it divided into three sections, Big, Small, and everything in between. Each of these sections has sub sections, for example as you see in the picture, Big has sub sections Universe, Galaxy, Stars, Future Tech, etc. The three main sections are in the middle, and when you click them, the corresponding sub sections appear on the side panels. Lets say you click the "Galaxy" button. The center panel changes to an interface with more buttons to pages about various things about galaxies.

    When you click a button, it becomes Bright, signifying it is the button that is active.

    now in terms of the actual code, I'll try to explain whats happening. For the main menu, I have three buttons: big, between, and small. I use an image for each button being pressed (See picture below)

    MainMenu2.jpg

    I am no longer using a mouselistener to find out where the user clicked like I was when I wrote this. Now, I have invisible JButtons on the Positions the buttons are on the screen.

    Now with the side panels: I have multiple sets of buttons for each of the sections big, between and small. For big, the buttons may be "universe" "galaxy" stars, but for small they buttons on the side panels may be "electrostatics" "chemical reactions" "Light". So I have buttons that go to different pages, but they are all in the same positions as each other. So to work with this, I have an integer variable called State, which ranges from 1 - 46 depending on what set of buttons are displayed on the screen. When a button is clicked, I use a switch statement to determine the state, and if lets say the state = 5, then the button that was in the range of the mouse click was "Universe". If the state was 7, the button that was clicked was "Electrostatics".

    So in the end of all this, I have over 120 images, each for the different combinations of buttons being clicked (like in the picture above). I hope this makes it easier to understand.

    Thanks

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can you think of a better way to do this? (I'm desperate!)

    I would use a common background image and let buttons draw themselves (differently depending on their own state, selected or not etc) on top of the (same) background. (instead of multiple large images and invisible buttons)

    Then an organized filtering should remove the need for 46 different states. I get the feeling a few states and a couple of buttons would take care of it.

    What would I do with such a program as the end user? What would I expect? Is this like a slide show of facts with images? I don't quite understand how it is intended to be used, I am just wondering if this time is being spent "fine-tuning" a "bad-approach" so to speak

  5. The Following User Says Thank You to jps For This Useful Post:

    Benner (June 2nd, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can you think of a better way to do this? (I'm desperate!)

    It's just a project for physics class, where we need to demonstrate the physics involved in various things. We had present this information in a "special" form, so i chose to do this. It's kind of like a powerpoint I guess, but with multiple "routes" instead of just going to the next slide everytime. It just displays information, videos, equations, etc.

    Lots of the time was being spent trying to figure out which switch statement was for what, what variable was for what, what icon[] index was what image, etc. But you just made it a lot more simpler!

    I never thought of just painting the buttons onto the screen... thats really the "common sense" thing to do i guess, but it never even occurred to me. You and the person who suggested I use invisible buttons helped me so much. Thank you so much!

Similar Threads

  1. Please help desperate beginner with Finding Volume project.
    By phuzed in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 13th, 2013, 12:18 AM
  2. In desperate need of help. Making an exponents table.
    By jp93129 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 15th, 2012, 10:49 AM
  3. Desperate Need of help
    By mszyndlar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2011, 03:32 AM
  4. programming hw desperate help!
    By midnightdream13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2011, 07:58 AM
  5. What is wrong with this code, please desperate
    By macnasty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:26 AM