Does anyone have a example of slot machine in Java using swing? - java

I want to get some examples about the GUI of an slot machine written in java, I know that it's kind of specific thing, but i want to know if it's possible to do some animations with java swing, or something like that, greetings

It isn't a slot machine simulation, but you may find related features in this example.
Addendum: The example cited uses Unicode glyphs for variety, but another trick is to implement the Icon interface, which works nicely with some components. This example decorates a JButton for use in a game, while this example extends a child of JLabel to render a table.

This won't answer your question exactly (because I don't have a direct example), but I hope it at least tells you that it's certainly possible.
I've done some games using Java and Swing and such. None of them have been too heavy on the animations. Most of the animations I did happened through the Java 2D Graphics libraries, while more UI stuff happened through Swing.
For example, you can take all of the functionality of a JButton, but override the paintComponent(Graphics g) method to make it look however you want. This was really useful in past projects for me.
The most graphics-intensive Swing-related game I ever built was a "Who Wants to Be a Millionaire?" game. I used animations and graphics and sounds and everything, so I can assure you that it is possible, but it certainly isn't as easy as some other libraries might be.
If you decide to pursue this, I've found that Swing Hacks has some great tips on doing weird things with Swing. Although it isn't focused on gaming, I've found enough interesting things that you can do (Drag and Drop, a bunch of weird things) to make it a worthwhile purchase.

Related

Futuristic GUI in Java?

First of all sorry for my english, it's not my mother tongue.
I want to made a program in java, i was thinking that it will look great with a futuristic graphical user interface. When i say "futuristic", i mean something like this:
I know it's the image of an Android application but i'd like to do something like this in a java program.
Usually when i have to do GUIs in java i use Swing components, but now i need to personalize everything, and i'd also like that when i click "Send" for example the circle becomes bigger and other things appears inside it.
So i need to have a complitely personalized and animated GUI. Which is the best way to do it? Swing, Awt, JavaFx or what else?
Thank you in advance.
Consider to use JavaFX. It supports styling via CSS, has APIs for effects, animations etc. and can work with the graphic card.
Swing is currently in maintenance mode and JavaFX is the new proposed (by Oracle) standard Java GUI toolkit for Java.
You could extend JComponent for every different component you want and then override the paint(Graphics g) method. Then you just draw on g object like on any other graphics object.

Java graphic as an object/class

I'm sorry this is probably way too basic to be on here, but it's a subject I've been struggling with for about a month now and I don't know where else to go (as far as I know there is no "noob overflow", lol).
I'm trying to create a class that would:
1. put an image on a window (a JFrame, JPanel or other container)
2. be able to support keyboard and mouse listeners
3. could have multiple instances in the same container
So anyway I've tried all the usual places - Google, YouTube, the official Java site (sorry forgot the URL) and of course here on Stack Overflow - but haven't been able to find anything even remotely similar to what I'm trying to do.
Of course, I've also considered the possiblity that maybe it can't be done at all. There doesn't seem to be any kind of standard "JImage" or "JGraphic" that works like JButton or JLabel, and for whatever reason graphics requires a completely different list of (extremely involved) processes and procedures. As an example, in this post: How to "really" draw images in a Java app - it took me 60+ lines of code and 2 classes to just come close. That project didn't work in the end because for some reason it would only let me create one instance (even it you created 2-4 in the main method, it would only display the last one you told it to add).
But anyway, assuming that I'm not trying to "re-invent the wheel" here and it is actually possible (in Java), does anyone have an idea as to how (or at least know of a better site to study it)? Unfortunately most of the sites I've visited tend to assume you know all the inner workings of images (I know what a pixel is but that's about it - Buffers, Rastars etc. are still beyond me). It would be absolutely outstanding if there were a site that would explain it in layman's terms, if such a site exists. Thanks in advance.
Just use a plain old JLabel.
Regarding your requirements:
put an image on a window (a JFrame, JPanel or other container).
You can give a JLabel an ImageIcon of the image of interest and it will display it. This can then be easily placed in any other container such as a JPanel or JFrame.
be able to support keyboard and mouse listeners
Any component that extends JComponent, such as a JLabel allows for use of MouseListener, MouseMotionListener and can listen for keyboard input via Key Bindings.
could have multiple instances in the same container
You can add as many as you'd like to any container. Just be cognizant and respectful of the layout managers in use.

Writing an AWT widget

I fear I'm just making a ten-times duplicate question, but I've been trying to Google this for quite some time without finding anything, neither in general nor even here on SO, so I'm starting to think it's worth a shot.
I'm wondering, is there a good guide on what is necessary to implement an AWT widget in Java? What needs to be implemented in order to respond to such things as redrawing, resizing, placement, focus behavior and all other such things as widgets may or should do, in a proper manner? All the articles I manage to find describe the process from the perspective of a user of widgets, never from the perspective of the one implementing them.
Also, what would be the primary differences between implementing an AWT widget and implementing a Swing widget? Is there even a difference from the implementer's point of view?
Probably the best way to learn how to write an AWT widget would be to look at the source code for an AWT widget. Here's the source code for the java.awt.Frame class.
I haven't worked with AWT much, but Swing is much better from the GUI developer's point of view. The only difference that I know about from the implementer's point of view is Swing's usage of Listeners.
Again, you can look at the source code to see what's different. Here's the source code for the javax.swing.JFrame class
Not too much difference aside from the fact that AWT widgets are simpler.
You extend one of the 4 classes: Component, Container, Canvas, Panel. The first two are heavyweight, the latter two are lightweight (don't have native window system peers). Override paint(Graphics) (or update()) and getPreferredSize() to make it visible, then look into javadocs for overriding event handling methods.
Sure you know this link, http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html

Java, Swing, Netbeans: how to create an interface similar to the iPhone's SMS screen?

I'm looking to create an interface similar to that of the iPhone's SMS screen. More specifically I'm looking to replicate the "bubbles" coming from each side of the page which contain the messages, as shown here http://www.bidslammer.com/images/iphone_shot1.png .
I do also want to recreate the date and time above the bubbles like you can see in that image. I need to be able to do this by code because its use will be to display the messages that it receives over my socket connection, and show the messages I send over the socket.
I'm really new to Java, and even newer to Swing, so I'm looking for some pointers on how I should go about this.
Can anyone offer my any suggestions about how I would go about doing this? I'm not looking for someone to do the work for me, just a few pointers, perhaps some things I should learn how to use/do and perhaps a helpful tutorial or two.
Google for "swing tutorial" gives a lot of tutorial links, so just pick one. When I was learning Swing I used original Java Swing tutorial.
As for some pointers, I think good idea is to use images to represent the bubbles - that will be the easiest. Inherit some basic component like JLabel or JPanel and override a drawing method - do a custom drawing. First draw the bubble image and then the text over it. This may help with image drawing.
Generally with custom component drawing you use Graphics class, which provides a lot of useful drawing methods.
The sticky notes demo might get you started in the right direction.
Even though that demo is a NetBeans platform module, the sticky note itself is a pure Swing component and should be usable without the platform.

Custom Swing component: questions on approach

I'm trying to build a new java swing component, I realise that I might be able to find one that does what I need on the web, but this is partly an exercise for me to learn ow to do this.
I want to build a swing component that represents a Gantt chart. it would be good (though not essential for people to be able to interact with it (e.g slide the the tasks around to adjust timings)
it feels like the best approach for this is to subclass JComponent, and override PaintComponent() to 'draw a picture' of what the chart should look like, as opposed to doing something like trying to jam everything into a custom JTable.
I've read a couple of books on the subject, and also looked at a few examples (most notably things like JXGraph) - but I'm curious about a few things
When do I have to switch to using UI delegates, and when can I stick to just fiddling around in paintcomponent() to render what I want?
if I want other swing components as sub-elements of my component (e.g I wanted a text box on my gantt chart)
can I no longer use paintComponent()?
can I arbitrarily position them within my Gantt chart, or do I have to use a normal swing layout manager
many thanks in advance.
-Ace
I think that the article i wrote a few years ago for java.net is still correct today. Doing everything in one monolithic class gets you going faster in the beginning, but becomes a mess quite fast. I highly recommend doing the separation between the model (in your main class) and the view (UI delegate). The view is responsible for:
interaction with the user - mouse, keyboard etc.
painting
creating "worker" subcomponents as necessary
In the medium and long run this is the approach that has been validated over and over again in the Flamingo component suite, which you can use as an extra reference point (in addition to how core Swing components are implemented).
Using UI delegates is a good idea if you think that your component should look different for different Look And Feels. Also it is generally a good idea from design point of view to separate you presentation from your component
Even when overrding paintComponent you can still put any sub components on it.
Using null layout you arbitrarey position your components. Alternatively you can use layouts too.
Here is a very good starting point for you.

Categories