Mouse over Color in Java - java

I need to make a game in Java for a project.
What I'm trying to do is a game where you have to go through a maze without touching the walls.
Is there a way to get the color of the pixel the mouse is over?

You probably want java.awt.Robot.getPixelColor(x, y). It'll be faster than grabbing an image.

This post answers your question precisely including a complete working code example. Basically, you can create an image from canvas on which you draw and call getRGB() on a pixel on the image.

You want to implement the MouseMotionListener interface and do what Artelius mentioned, namely, get the coordinates of the mouse position and calculate the color of the pixel.

Related

Rotating Asteroids

I'm trying to make asteroids that spin in my 2D game.
Using the graphics class I am drawing an image I created onto the screen (the asteroid) and moving it across the screen.
My next step was to make it rotate however I am lost at to how to do this.
I was able to make it rotate when I was simply drawing a polygon by changing the vertices of the asteroid however there are no vertices when drawing images, only (x,y) and (length,width).
How can I rotate an image? Is there any inbuilt functionality that does it?
I'm not asking for someone to tell me exactly how to do this, I'm just looking for a push in the right direction as I am a bit lost.
Either use a AffineTransform or use Graphics#rotate. In either case, you should make a copy of the Graphics context first (Graphics#create) first, which will preserve the state of the original context. Just make sure you dispose of the copy when you're done (Graphics#dispose)
Something like this perhaps

Java: set screen pixel color directly?

I have searched for an answer for this question, but all attempts have proven no useful info.
I am trying to figure out a way to draw a pixel directly to the screen, without using a fullscreen canvas of sorts.
Is there any way to do this? If so, what is the best (And/Or most efficient) way to do so?
Thanks in advance.
EDIT: For those who are confused, I am trying to draw directly to the screen, without a fullscreen canvas. I want to set a pixel color on the screen if you will, sort of like drawing to the video hardware.

How to paint the portion of the image inside its border line via using libgdx?

I am creating an image painting app in libgdx where user would touch the portion of the image. e.g If the user touch the wheels of a car that should turn into the selected color.
I don't really know how to specify the custom area around the touch. That can be a wheel, a door which are not always rectangle and circle. I just need an idea to do it. Whether libgdx pixelmap can achive this. I have to paint the image inside the border line of the touch region. I hope my problem is clearly stated.
I woule really appreciate for your time for giving the answers. Thanks in advance
I think you want to look at flood fill algorithms. I don't think there is any built-in support for this in libGDX, but you should be able to implement your own flood fill on a libGDX pixmap.

How to make transparent part of a sprite in a Java 2D Game?

I'm making a 2D tile based game and I need to make part of a sprite transparent.
I assume I should choose a determinate color and tell Java "Please Java, don't draw any pixel of this color.", but I don't know how to do it.
I already googled it but I only get results about "alpha".
Also how can I "set priority" to my sprites so that the ones with high priority are always drawn in the front?
Thanks.
Use png files for the sprites. Java will draw transparent parts of png files transparently.
As for the sprite ordering, this is difficult to advise without any knowledge of your game's code structure. You could give each sprite a Z-index, sort and draw in order.
This KineticModel creates multiple, translucent instances of GradientImage as part of a simulation.

Android color picker - updating color array

I'm trying to create a color picker for Android that looks like a minimalistic version of Gimp's. So, it has a hue slider and a rectangle with saturation/value variants of a color chosen in hue slider.
Question: what is the best way to create the rectangle?
Right now, I'm creating an 200x200 array of pixels, but it takes ~5sec to create and display rectangle with that array. And I need colors in rectangle to change whenever I change the value in hue slider...
Rectangle is bitmap, btw. Can I use color matrices on that and how? Any examples?
Thanks in advance!
You can create the rectangle with saturation/value variants that change according to the selected hue, by drawing the rectangle with LinearGradients.
You can incorporate the code here: http://code.google.com/p/android-color-picker/ into your application. Seems that this is what you want.
OpenIntents has a very nice color picker you can use. It can be installed as an independent app and launched with Intents.
Code: http://code.google.com/p/openintents/source/browse/#svn/trunk/ColorPicker
Screenshots/download: http://www.openintents.org/en/colorpicker
Intent specification: http://www.openintents.org/en/node/670
One possibility is to pre-create the rectangles on your developer PC for each slider position, embed them as resources, and then swap in the right one when the slider changes. This may make for a portly application, but it will be nice and quick.
I have not dealt with the 2D graphics API much, so I don't know if there are other possibilities (e.g., color matrices).
Can this be applied to an image color picker as well?
Use case:
Select a particular pixel on an image.
The pixel selected generates a color on a rectangle shape.
Perhaps generating color codes for the pixel selected?

Categories

Resources