Ignore transparent pixels on sprite - haxe

So I need the sprite to get bigger when I hover over the sprite with the mouse. But the problem is that the sprite has an unusual shape, which leaves a lot of transparent space. And the mouse reacts to this transparent space.
override public function create()
{
image = new FlxSprite(100, 100).loadGraphic("assets/images/freeplay.png");
add(image);
}
override public function update(elapsed:Float)
{
image.scale.set(1, 1);
if (FlxG.mouse.overlaps(image))
{
image.scale.set(1.1, 1.1);
}
super.update(elapsed);
}
How can I make the sprite not react on a transparent background?

FlxSprite has pixelsOverlapPoint for one-off things.
FlxExtendedSprite has enableMouseClicks for more complex setups.

Related

Something for a child window analogous to `this`

I am a newcomer to programming. I am writing a dialog based application which has a static control on it. Using
Using
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (this == GetCapture())
{
CClientDC aDC(this);
aDC.SetPixel(point, RGB(255,0,0));
}
}
I can create results like
However what I want is that the locus of mouse is only drawn within the static window. I can't find the reference to this in MSDN and I don't know why the following method fails.
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pMYSTATIC = GetDlgItem (IDC_MYSTATIC); //IDC_MYSTATIC is the ID of the static control
if (pMYSTATIC == GetCapture())
{
CClientDC aDC(pMYSTATIC);
aDC.SetPixel(point, RGB(255,0,0));
}
}
How can I get what I want? Are there any methods to get something for the static window analogous to this? I will appreciate any help with these.
OK, try this:
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rect;
// Get static control's rectangle
GetDlgItem(IDC_MYSTATIC)->GetWindowRect(&rect);
// Convert it to client coordinates
ScreenToClient(&rect);
// Check if mouse pointer is inside the static window, if so draw the pixel
if (rect.PtInRect(point))
{
CClientDC dc(this);
dc.SetPixel(point.x, point.y, RGB(255,0,0));
}
}
This code may need some fixes too, eg shrink the rectangle (to its client-only area), before checking whether to draw the pixel.
Please note that you don't need to check GetCapture(); if your dialog hasn't captured the mouse it won't be receiving this message anyway.
Also, all these functions are wrappers of Windows SDK ones, eg the ClientDC() class, basically wraps GetDC()/ReleaseDC().

Layouting problems with JavaFX Region

I want to write a new class that extends Region containing a StackPane inside. But I'm getting into trouble when I add insets to it like padding or a border. Here is a simplified example of the class:
public class CustomPane extends Region
{
private ToggleButton testControl = new ToggleButton("just a test control");
private StackPane rootPane = new StackPane(testControl);
public CustomPane()
{
getChildren().add(rootPane);
setStyle("-fx-border-color: #257165; -fx-border-width: 10;");
}
}
And that is how the result looks like:
If I try to move the StackPane by calling
rootPane.setLayoutX(10);
rootPane.setLayoutY(10);
then the Region just grows:
But really I wanted it to look like this:
(The third image was created by extending StackPane instead of Region, which already manages the layouting stuff correctly. Unfortunately I have to extend Region since I want to keep getChildren() protected.)
Okay, I tried to handle the layout calculation but I didn't come up with it. Could experts give me some advise?
StackPane uses the insets for layouting the (managed) children. Region doesn't do this by default. Therefore you need to override the layoutChildren with something that uses these insets, e.g.:
#Override
protected void layoutChildren() {
Insets insets = getInsets();
double top = insets.getTop(),
left = insets.getLeft(),
width = getWidth() - left - insets.getRight(),
height = getHeight() - top - insets.getBottom();
// layout all managed children (there's only rootPane in this case)
layoutInArea(rootPane,
left, top, // offset of layout area
width, height, // available size for content
0,
HPos.LEFT,
VPos.TOP);
}

How to create a circular bufferedimage rather than creating a rectangular one in Java using Graphics

I need to create a bufferedimage
(circular type) but I am able to create only rectangular type. Then I want to create a oval inside the bufferedimage with some configurations and finally i want to draw a rectangular icon inside the circular shape which should be inserted on the circular shape and not on the rectangular bufferedimage.
Now i am able to do the following
BufferedImage img = "SomeImage.png";
BufferedImage bfImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bfImage.createGraphics();
graphics.fillOval(0, 0, 200, 200);
graphics.drawImage(img, 0, 0, null);
(This creates a circular oval inside the bfImage object)
Now i need to draw "img" which is rectangular in shape of say 100*100 size.
This i can draw using
When i am doing this my final image is getting drawn in the Rectangular BfImage, which i dont want.I want the image "img" to be drawn in the circular oval and it should not come outside the boundaries of the circular oval shape.
In short instead of drawing my final image on the rectangular bfImage can i have a circular bfImage on which i can directly draw my image.
Any logic to do this in Java2D using graphics.
I've never encountered a "circular image" in the sense of a two dimensional array that is not in rectangular form. If all you are concerned about is that the pixels outside the circle are not visible, just set the alpha to 0 for those pixels. An easy way to do this is to fill the entire rectangular image with ARGB(0,0,0,0) first and then draw whatever else you want.
Also, not that if you intent to persist this buffer as an image file, you must make sure that you export/save to a format like PNG or TIFF that supports transparency.
As #justinzane says, you can't have a truly circular image. All BufferedImages will be rectangular.
But: You can achieve the effect you are after, by using the AlphaComposite class, and the rule AlphaComposite.SrcIn. I've added a fully running example, plus a screen shot below, the compose method is the important part.
public class AlphaCompositeTest {
private static BufferedImage compose(final BufferedImage source, int w, int h) {
BufferedImage destination = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = destination.createGraphics();
try {
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setColor(Color.BLACK); // The color here doesn't really matter
graphics.fillOval(0, 0, destination.getWidth(), destination.getHeight());
if (source != null) {
graphics.setComposite(AlphaComposite.SrcIn); // Only paint inside the oval from now on
graphics.drawImage(source, 0, 0, null);
}
}
finally {
graphics.dispose();
}
return destination;
}
public static void main(String[] args) throws IOException {
final BufferedImage original = ImageIO.read(new File("lena.png"));
final BufferedImage template = compose(null, original.getWidth(), original.getHeight());
final BufferedImage composed = compose(original, original.getWidth(), original.getHeight());
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.add(new JLabel("+", new ImageIcon(template), SwingConstants.LEFT), BorderLayout.WEST);
panel.add(new JLabel("=", new ImageIcon(original), SwingConstants.LEFT), BorderLayout.CENTER);
panel.add(new JLabel(new ImageIcon(composed)), BorderLayout.EAST);
frame.add(new JScrollPane(panel));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
See for example the Compositing tutorial for more info and examples.

How to save canvas as an image in j2me?

I want to save the current state of the canvas as an image as I will use it in the next pointerevent. If I use repaint it will clear the canvas and I can't get the previous state of canvas. So, I want to save it as image and then iterate it over and over so that finally I can make out what I want.
The final question is how to save the canvas as image ?.
Or Is there any possibility of converting the Graphics object to a byte array ?
You can't save the canvas as an image.
You have to create an image first, and then you can paint onto that image.
Basically that means that your midlet will be doing more work, since you first have to draw onto your image, and then you have to draw that image onto the canvas. But it's the only way you can do what you want.
Create an Image with same size (width and height) of the screen. When you want to save the canvas state call Canvas.paint passing the image Graphics.
class MyCanvas extends Canvas {
private Image lastScreen;
protected void sizeChanged(int w, int h) {
if (lastScreen == null || w != lastScreen.getWidth()
|| h != lastScreen.getHeight) {
lastScreen = Image.createImage(w, h);
}
}
protected void paint(Graphics g) {
// paint the whole screen
}
protected void pointerReleased(int x, int y) {
paint(lastScreen.getGraphics());
}
}

How to trigger BorderLayoutContainer center ContentPanel to resize when other regions collapse/expand?

I would like the BorderLayoutContainer Center region Content Panel to fill to the available space when the other regions collapse/expend.
Right now, the center region moves to the collapsed space but does not resize.
I've figured this out from a bunch of digging thru the api, source, and hints in the Sencha forums.
My setup is a center BorderLayoutContainer ContentPanel with a MPV View Composite VerticalLayoutContainer surrounding two Toolbars and a Grid.
The issue is the toolbars and grid would not resize to fill the space when the center ContentPanel resized.
My solution is for the View VerticalLayoutContainer Composite to implement the HasLayout interface and to add a ResizeHandler() to the surrounding ContentPanel.
centerContentPanel.addResizeHandler( new ResizeHandler() {
#Override
public void onResize(ResizeEvent event) {
Widget w = centerContentPanel.getWidget();
if (w instanceof HasLayout) {
((HasLayout) w).forceLayout();
}
}
});
I know this answer is very late ... but try putting your BorderLayoutContainer inside a ViewPort. Here's the JavaDoc for the ViewPort widget: GXT ViewPort.

Resources