Drawing a text field without the use of JTextField etc - java

I'm making a launcher for a game I'm developing and I use Graphics2D to render buttons, gui etc. Now I've extended the class that does this (Launcher.java) with the Canvas. This means I can't use JTextFields (plus it would look ugly anyway). So my question is, how would I make a text field without the use of JTextField, this means rendering it, getting key input display the input.
Thanks.

You state: I'm making a launcher for a game I'm developing and I use Graphics2D to render buttons, gui etc.
You seem to be re-inventing the wheel. Why not instead use or create a Look and Feel that works well.
You state: Now I've extended the class that does this (Launcher.java) with the Canvas.
By committing to AWT components you are unnecessarily limiting what you can do. Why not just stick with Swing components?
You state: This means I can't use JTextFields
You can if you stick with Swing.
You state: (plus it would look ugly anyway).
Without images, it's hard to discuss this point.
You state: So my question is, how would I make a text field without the use of JTextField, this means rendering it, getting key input display the input.
Again, why commit to re-invent the wheel? Use Swing components including JTextFields, avoid AWT, and if you still feel the GUI is ugly, consider posting images and a small compilable and runnable program that we can mess with and that shows us directly your problem.

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.

How to create a "Pick Whip" (Adobe After Effects) in Swing

In Adobe After Effects there is something called a "pick whip". It allows you to click on a dot on one object and drag a line from that dot to another element, making a connection between the two. I would like to duplicate this feature in my Java program using swing. I honestly have no idea where to start.
If I've done a bad job explaining what I mean, please comment so I can improve it. If there is some way outside of Swing to do this, I'm willing to try it.
Here are some examples of what I am trying to achieve:
LinePanel illustrates how to animate the line as it is rendered, but you'll have to paint on the glass pane or among JLayeredPane instances to see the line above existing components.

Java: How to do GUI animations?

In all my time so far working with Java and its Swing GUI framework, I've never quite figured out (or even attempted to try) how to make the interface animate components.
Say I wanted the screen to slide left into the next screen or have a JLabel "fly" to a new location. Perhaps you want a menu to smoothly open in an animated fashion. How does this work?
Do you have to use SwingWorker? Even if that's the case... how can you control the painting of components if the layout manager is already doing that?
Have a look at the book Filthy Rich Clients, you will find some really good answers there.
I think that there no reason for use SwingWorker, SwingWorker is designated for running long Backgroung Task(s) on output would be on Event dispatch Thread,
For animations in Swing is there javax.swing.Timer, examples here
Take a look at Trident library. You can use it to interpolate various properties in your class.

Debugging incorrect AWT display

I'm trying to modify an existing application using AWT for dialogs, etc. I don't know all the code of the application, so I'm not sure how are the objects stacked.
I tried adding a new JButton to a JPanel with no layout. I set location and size, validated, repainted... and nothing is displayed. Now I'm a bit lost - how do I figure out why this happened? Is there some way to poke around a live application window and see the AWT objects tree? How do I approach this kind of problems?
Disclaimer: I know next to nothing about AWT.
Logging! Making log calls whenever you're manipulating anything on the AWT stack. Most (all?) AWT objects also have decent toString methods, so it's possible to get and print objects in a component and get meaningful debugging information (i.e. for(Component c : frame.getComponents()) { System.err.println(c.toString()); } ).

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