Texturing backgrounds in Java Swing everywhere, not just in panels - java

I want to make a GUI with Java swing/JPanel with a textured background. I found a few tutorials on the topic that texture within the boundaries of the control its being applied to, like this:
That's easy enough. I can't figure out how to apply textures, (not necessarily the same texture) elsewhere, like on the tabs title bar, the background, scroll bars... basically everywhere that's still yellow.
How do I do this? Does this require making my own 'Look and Feel'?

Components have a variety of functions named paint___ that control the drawing of various parts. I overrode these and tiled Graphics.drawImage() in the parts responsible for the background.

Related

OpenGL work with non-OpenGL drawings?

Can graphics rendered using OpenGL work with graphics rendered not using OpenGL?
I am starting to learn OpenGL, but I am still shy when it comes to actually coding everything in OpenGL, I feel more comfortable drawing them out with JPanel or Canvas. I'm assuming that it wouldn't cause much issue code wise, but displaying it all at the same time could cause issues? Or am I stuck with one or the other?
Integrating OpenGL graphics with another non-OpenGL image or rendering boils down to compositing images. You can take a 2D image and load it as a texture in OpenGL, such that you can then use that texture to paint a surface in OpenGL, or as is suggested by your question, paint a background. Alternatively, you can use framebuffers in OpenGL to render an OpenGL scene to a texture, when can then be converted to a 2D bitmap and combined with another image.
There are limitations to this approach of course. Once an OpenGL scene has been moved to a 2D image, generally you lose all depth (it's possible to preserve depth in an additional channel in the image if you want to do that, but it would involve additional work).
In addition, since presumably you want one image to not simply overwrite the other, you're going to have to include an alpha (transparency) channel in one of your images, so that when you combine them, areas which haven't been drawn will end up showing the underlying image.
However, I would suggest you undertake the effort to simply find one rendering API that serves all your needs. The extra work you do to combine rendering output from two APIs is probably going to be wasted effort in the long run. It's one thing to embed an OpenGL control into an enclosing application that renders many of it's controls using a more conventional API like AWT. On the other hand, it's highly unusual to try to composite output from both OpenGL and another rendering API into the same output area.
Perhaps if you could provide a more concrete example of what kinds of rendering you're talking about, people could offer more helpful advice.
You're stuck with one or the other. You can't put them together.

How I can I pan and zoom JComponents?

I am am trying to build a user interface for graphical programming. I would like to use JUNG to handle the graph aspects of the drawing. Rather than draw all of my widgets using Graphics2D, I would like to use Swing widgets as often as possible to take advantage of Swing's event architecture.
I would like to be able to pan, zoom, rotate the canvas the graph is drawn on, but still retain the ability to manipulate the Swing widgets as long as they are large enough to see. I also want the widgets to be automatically laid out based on the JUNG layout algorithm I have chosen.
How do I combine Graphics2D drawings and Swing components?
I understand that JComponents take care of drawing themselves using their paint() methods. What I am having trouble understanding is how to create Swing objects that can be affected by Graphics2D transforms and still retain all of their event handling. I can't just show an image of the component. The component must be live.
I've tried subclassing a widget and adding a AffineTransform to the graphics object that is passed to the parent's paint method. It rotates the widget, but the clipping appears to be off.
Any suggestions? For the time being, I'd like to avoid adding another library if I can.
As an example, JDigit extends JButton and overrides paintComponent() to scale to the size of its enclosing Container. For speed, the component uses pre-rendered glyphs, but deriveFont() is only slightly slower. Similar results can be obtained by implementing the Icon interface. This example scales a grid of buttons to a few, pre-determined sizes, and this example forgoes components entirely, scaling the graphics context and all contents.
JXLayer project has implementation to transform the whole UI to different scales.
You may want to check out this demo
Based on my research, using Piccolo2D provides the ability to pan and zoom Swing elements. Using JUNG's graph layout algorithms to layout the graph and Piccolo2D PSwing nodes, I can generate a graph of Swing components that are usable, can be laid out by algorithm, and are zoom and panable.

Suggestion for implementing a drawing program - UML designer

This program will have an infinite canvas (ie as long as the user scrolls, it becomes bigger) with a tiled background image, and you can drag and drop blocks and draw arrows between blocks. Obviously I won't use a layout manager for placing blocks and lines, since they will be absolutely positioned (any link on this, possibily with a snapping feature?). The problem arises with blocks and lines. Basically I'll have two options:
Using a simple layout for each building block. This is the simplest and clearest approach, but does it scale well when you have hundreds of objects? This may not be uncommon, just imagine a database with 50 tables and dozens of relationships
Drawing everything with primitives (rectangles, bitmaps, etc). This seems too complicated (especially things like text padding and alignment) but may be more scalable if you have a large number of objects. Also there won't be any event handler
Please give me some hints based on your experience. I have never drawn with Java before - well I did something rather basic with PHP and on Android. Here is a simple preview
DISCLAIMER
You are not forced to answer this. I am looking for someone who did something like this before, what's the use of writing I can check an open source project? Do you know how difficult it is to understand someone else's code? I'm talking about implementations details here... Moreover, there is no guarantee that he's right. This project is just for study and will be funny, I don't want to sell it or anything and I don't need your authorization to start it.
Measuring and drawing text isn't such a pain, since java has built in classes for doing that. you may want to take a look at the 2D Text Tutorial for more information. In fact, I did some text drawing computations with a different graphics engine which is much more primitive, and in the end it was rather easy (at least for the single-line drawing, for going multiline see the previous link).
For the infinite canvas problem, that's also something I always wanted to be able to do. A quick search here at stackoverflow gives this which sounds nice, althought I'm not sure I like it. What you can do, is use the way GIMP has a scroll area that can extend as you move - catch the click of the middle mouse button for marking the initial intention to move the viewport. Then, when the mouse is dragged (while the button is clicked) move the viewport of the jscrollpane by the offset between the initial click and the current position. If we moved outside the bounds of the canvas, then you should simply enlarge the canvas.
In case you are still afraid of some of the manual drawing, you can actually have a JPanel as your canvas, with a fixed layout. Then you can override it's paint method for drawing the connectors, while having child components (such as buttongs and text areas) for other interaction (and each component may override it's own paint method in case it wants to have a custom-painted rect).
In my last drawing test in java, I made an application for drawing bezier curves (which are basically curves made of several control points). It was a JPanel with overidden paint method that drew the curve itself, and buttons with custom painting placed on the location of the control points. Clicking on the control point actually was clicking on a button, so it was easy to detect the matching control point (since each button had one control point associated with it). This is bad in terms of efficiency (manual hit detection may be faster) but it was easy in terms of programming.
Anyway, This idea can be extended by having one child JPanel for each class rectangle - this will provide easy click detection and custom painting, while the parent will draw the connectors.
So in short - go for nested JPanels with custom drawing, so that you can also place "on-canvas" widgets (and use real swing widgets such as text labels to do some ready drawing) while also having custom drawing (by overriding the paint method of the panels). Note that the con of this method is that some swing look-and-feel's may interfere with your drawing, so may need to mess a bit with that (as far as I remember, the metal and nimbus look-and-feel's were ok, and they are both cross-platform).

How to get the image center, strech, fill, tilt in J2ME?

How to get these effects in a J2ME project?
I need a similar effect like CSS background like we write:
background:#fff url(img.png) repeat-x;
I need this kind of thing in J2ME. Can I do it?
I"ve never used J2ME, but I assume it supports basic Graphics functionality. Therefore you should be able to used the concepts found in the paintComponent() method of Background Panel to do your custom painting.
The graphical stuff you can do out of the box in J2ME is anything found in the javax.microedition.lcdui.Graphics. If you want an image repeated across the screen, you'll need to paint it repeatedly in a loop, in your paint() method. You can't rotate an image through small angle; you can rotate through 90 degree angles using javax.microedition.lcdui.game.Sprite.
There's no method for scaling in the API, however you could copy/paste a function like this; beware, this is likely to eat up a lot of memory.

Floating Rectangle on Screen

I know how to draw a rectangle onto a JPanel, but how can I paint a rectangle to the screen so that the rectangle appears to be floating? More specifically, a non-filled rectangle. My thought is to use a transparent JFrame with a rectangle drawn on it; however, this makes all of the content in the JFrame transparent.
My Solution
So I think there are many ways of going about this, some more complex than others, some more practical than others.
I tried the splash screen. The problem with that is you need to pass VM parameters "-splash " when you run. I created a manifest file to automate this/put the parameters into eclipse; but then the code is dependent on the .gif file and I can't change the size/position of the rectangle easily. Similar problems occur while faking it via screen screenshot. Good suggestions though, I learned some pretty cool stuff.
So, back to what I did. I used a JFrame, got the content pane and set the background to red (what ever color you want), then set the frame undecorated which removes the titlebar and border of the window. This created a floating rectangle which I could easily change the size and location of (frame.setSize, .setLocation). I have yet to make this a non filled rectangle, I tried internal frames and layeredpanes, but no success.
JFrame is a heavyweight component, and those were always opaque for the longest time. However, since Java 6u10, there is an inofficial API for transparent windows in the class com.sun.awt.AWTUtilities, which will most likely become official in Java 7. In earlier versions, the only way to simulate this kind of thing was to fake it via screenshots taken with java.awt.Robot
You would probably have to have parts of the window transparent while the actual drawn rectangle is not. I doubt there is a platform-agnostic solution for this so you would need to resort to JNI for this. Depending on what you need to do it might also be nice to make the window invisible for clicks which would need other tricks as well.
https://github.com/twall/jna/
That project offers a library and has examples of a clock and info balloons that are semi-transparent and transcend even what you're trying to do. The demos work well on Windows but I couldn't speak to how cross platform they are.
You might want to look at JLayeredPane. The demo on this page shows partially what you want, however when painting your rectangle you'll need to set your paint to transparent:
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(ac);
g2d.drawImage(image, x, y, this);

Categories

Resources