How make drawing restrictions in own gui window? - java

I want make gui window, but i don't know how to restrict drawing. What i mean? When you scrolling window, some ui elements drawing, and some "being cut off". See in image this restrict? I need that: (IMAGE)
I find glScissor, but i don't know how to used it

glScissor could work for exactly this purpose (scrolling).
It uses a per-fragment 'scissor test' to check whether a fragment is in the boundaries of the scissor box specified by glScissor. Nothing can be modified outside of the box, so this is a way to restrict drawing and rendering.
It takes in the position of the bottom left of the box as the first two parameters, and the last two are the width and height respectively. (You may have to call glEnable(GL_SCISSOR_TEST) first, as the scissor test is usually disabled.)
In this case, you can set the scissor box to the dimensions of the GUI window, so that you can cut off individual pixels of the GUI when the user scrolls.

Related

Is there any possibilities to get coordinates of the specific area on the background image?

So i am new to libgdx and i am thinking about making 2D quest. I want to know how to interact with the objects on the backround image. For example, i have this location
Is it possible to get the window's coordinates so that if the player is next to it (I want to check the interaction possibilities by the player's coordinates and the interactive object's coordinates), he can press "E" or do something like that?
I was thinking about putting certain images in the background, but I don't know if that's a good idea. I also saw something about TextureRegion but I don't think it can help me because I still don't know how to get the coordinates of a specific area in the background.
Colour code the objects. From the appearance of your game its retro adventure so you are using a restricted palette. A restricted palette has advantages! So if you have windows with two colours, doors with 2 different and separate colours, objects with 2 different separate colours, then you can check when the player gets near a window, or whichever, by seeing what active colours are present on a radius around the player location.
So e.g. the floor and walls are coloured of course but they never trigger anything. This does limit you to one window per room, or one type of object per room (unless you introduce a new window colour pair to distinguish them). The code would be just sample all the pixels in the area around the player maybe a rectangle would be better.

How to make my 800*600 game fit all screen resolutions?

I have created a game that is 800*600. How can i make it so that it will fit all computer screens? How would this normally be done? Other sites have said too set everything to a certain ratio depending what the screen resolution is. But if each image in the game changes size in comparison to the screen resolution it would mean that the images would not be in the right places and cause other problems. Is it possible to just "Stretch out" my 800*600 game so it fits any size? Thanks
You need to make the game resolution-independent. That means you create your own concept of 'game units' (which might be exactly the same as pixels relative to an 800x600 screen if you want), and then operate exclusively on those game units.
Whenever you draw something to screen you convert co-ordinates and sizes from game units into pixels. You will need to have functions that can convert both ways, because you may also want to translate click-events from screen space to game space.
When you start your game, you need to decide on the screen resolution and aspect ratio. If the aspect ratio is not the same (eg widescreen), you might choose to either stretch the game area or letterbox it. By moving all the translation code to a lower level and operating only within your game units, you save yourself a lot of pain in the long run.
Yes, you can stretch it out. Make the Games' main components on a JPanel that is 800 by 600, and add that JPanel to your JFrame. Then the player can resize your JFrame and the components won't go offscreen, they will change shape and size based on the new size of the JFrame. Remember that the mouse's position in relation to the JFrame will change a bit, but divide the mouses position by the JFrames height/width and multiply it by the JPanel height/width to get the position in relation to the JPanel. With this method you can let the user maximize your game and still have components look normal.

Clicking Past a Transparent Frame (Java)

I am trying to develop an application in Java (Swing) that lets me overlay a grid on the screen, and be able to click things behind the grid -- say, an icon the desktop for example.
Would I approach this problem with a Transparent JFrame or a Transparent JWindow?
Whatever I use, the transparent window/frame needs to
always be on top.
occupy the entire screen.
register every click with a mouse listener.
record the coordinates of the clicks on the screen.
allow me to use the Graphics class to draw a grid on the screen, and other elements, like numbers or images, that should also be click through.
Any direction would be appreciated.
Apologize if I haven't been specific enough, but I haven't found a demo window or frame that can do all these things. There's an example here and another here -- but I don't know how to use WindowUtils in Eclipse. This is my first time in GUI development and I've never used external libraries aside from the base Java classes.
on top: Frame.setAlwaysOnTop() should work for you
full screen: Setting the window size to the display size will make it occupy the entire screen
events and overlay painting: The actual behavior may vary by platform, but typically if you're using an alpha component to draw into your window, and your window is nominally transparent, those areas not painted (or sometimes those painted below a certain alpha threshold) will pass events through to whatever applications, windows, or components are underneath.
If you capture events, you then have to re-introduce them to whatever window is below yours, which is non-trivial. If you don't capture events, you need to install an OS-specific event handler to capture events of interest.
JNA's WindowUtils.setWindowTransparent() should provide the paint/event behavior required, or you can use the AWTUtils equivalent provided in more recent JVM releases.

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).

Raw Java 2D implementation of a scrollbar

How would I go about writing my own scrollbar using standard Java 2D.
I really don't want to use swing, and I've already made up my own component parts for everything else such as buttons etc.
I'm not really looking for code, rather the math involved in the event changes and the drawing.
Why on earth would you want to write your own java GUI toolkit? You already have the choice of Swing and SWT, can you really do better than these two teams?
If you've already written the rest of the toolkit, I don't understand why the scrollbar would stump you. Without knowing anything about your event system, or how your custom components are structured, it's impossible to give much advise. I don't see this being particularly maths intensive - just maintain the height of the scrollable component, and the view it's in, and the scrollbar size should match the proportion of the component that is visible. The position of the scrollbar should match which part of the component is visible (this will have to be scaled). Specifically, what do you want to know?
Java is now open. I'd go look at the source for the Swing and/or SWT as they are already implemented. The math seems fairly straight forward. You have a Bar and a Container. To simplify we will only discuss length (the dimension in which the scrollbar moves). The container is of a certain length. The bar is of a length that is equal to or less than the container. It is useful to define the center and the two endpoints of the scrollbar. You can have the scrollbar start at 0 at the top and 1 at the bottom or 0 at the top and 100 at the bottom with the important part being defining your scrollbar in the same manner. Then you can check the endpoints for collision with the edge to stop the bar from moving. If the mouse is held down with the cursor over the coordinates inside the bar, the bar starts caring about where the cursor is and will paint the scrollbar and whatever the scrollbar is ultimately supposed to be affecting. So, you would take the page to be affected and map it to 0 and 1 * the scale in pixels of the scrollbar. Then you get to worry about the arrows at either end and how big of a jump each click is and dealing with mousedown events etc.etc. Use what is given don't reinvent the wheel.
While not Java2D, this straightforward code snippet might help:
http://processing.org/learning/topics/scrollbar.html

Categories

Resources