I'm currently using Processing 3.0.1 on MacOSX, and am having trouble with setting a custom image for the cursor. Specifically, the cursor flickers between my custom image and the default image.
The documentation says there are issues when running fullscreen, but the problem persists in a window as well.
Relevant code:
PImage cursorGreen;
void setup(){
cursorGreen = loadImage("img/cursor.png");
cursorGreen.resize(16,16);
}
void draw(){
cursor(cursorGreen);
}
EDIT: Restarted my computer and it seems to be working fine. Maybe a memory problem or something...
Your code works fine for me on Windows 10 using Processing 3.0.
One thing that jumps out to me is: why are you constantly setting the cursor in the draw function, which is called 60 times per second? Try just setting it once instead:
void setup(){
PImage cursorGreen = loadImage("img/cursor.png");
cursorGreen.resize(16,16);
cursor(cursorGreen);
}
void draw(){
//whatever
}
Related
I need help with my code in processing. It is actually a short and easy code, but I am a beginner in programming, so for me seems everything difficult... :(
My goal is...
To click on an image.
After the mouse click, the image should disappear and show up in a new random position.
Then I should be able to click on the image in the new random position, and it should do the same again: disappear and show up in a new random position. and so on.
I have written some code (see below), but it does not work properly. I would really appreciate it if someone could help me to find out, what is wrong with my code. Thank you very much in advance! :)
Here is my code:
PImage pic;
// Declare variables for the picture
float pic_x;
float pic_y;
float pic_r = 100;
float pic_x_new = random(0, 400);
float pic_y_new = random(0, 400);
boolean mouseOverPic;
void setup(){
size(500,500);
background(0,100,0);
//loading the picture
pic = loadImage("pic.png");
image(pic, pic_x, pic_y, pic_r, pic_r);
}
void draw(){
mouseOverPic = mouseX <= pic.width
&& mouseX >= pic_x
&& mouseY <= pic.height
&& mouseY >= pic_y;
if (mousePressed && mouseOverPic) {
background(100);
image(pic, pic_x_new, pic_y_new, pic_r, pic_r);
}
}
Can you please try to be more specific than saying your code does not work properly? Have you tried debugging your code to narrow your problem down? Which line of code behaves differently from what you expected?
The code you have doesn't make a ton of sense, because you're only ever drawing the image when it's being clicked. That doesn't sound like what you want to do. And your collision detection code is not correct. Try running through your code with some example values to see exactly what it's doing. I've written a tutorial on collision detection in Processing available here.
To fix this, you really need to break your problem down into smaller pieces and take those pieces on one at a time. For example:
Can you create a simple example program that just shows a hard-coded rectangle that changes color if the mouse is inside it?
Can you then make it so the rectangle displays in a random location every time the program is run?
Then can you make it so the rectangle changes location when you click it?
If you get stuck on a specific step, please narrow your problem down and post a MCVE in a new question. Good luck.
I belive you test against pic_x, but uses pic_x_new to draw the image (same with y). You should use the same var to place and test the image.
Another approach would be to make a function to test mouse against the image passing the new values as parameters.
I'm currently working on a new app, and I want to add customized animations to it (not talking about activity transitions).
To show you exactly what I mean, take a look at this video at 2:30-2:33:
https://www.youtube.com/watch?v=XBMdjX5bbvk&nohtml5=False
You see the chest jumping to the screen and opens smoothly with beautiful animation?
I'd really like to know how can it be added to an Android app, is it a frame animation?
I mean, I can make this animation, in 2D, I just want to know how to add it (I'm using Android Studio) without causing memory overflow.
Thanks!
For you question:
You see the chest jumping to the screen and opens smoothly with
beautiful animation? I'd really like to know how can it be added to an
Android app, is it a frame animation?
I don't think it is a frame animation. I guess this has been implemented using OpenGL. You can find the official tutorial here.
If you want to make simple 2d animations, you can use the AnimationDrawable api provided by android. You basically need frames for the sequence of animations and then you can create the animation using the following code:
// you would need an `ImageView` object as a placeholder for the animation
ImageView mMascotView = findViewById(...);
// prepare the animation object ..
AnimationDrawable mMascotAnimation = new AnimationDrawable();
final int frameTime = 250; // time in milliseconds
// adding the frames to the animation object. You can specify different
// times for each of these in milliseconds
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame1),frameTime);
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame2),frameTime);
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame3),frameTime);
// make it loop infinitely ..
mMascotAnimation.setOneShot(false);
// set the background of the `ImageView` as the `AnimationDrawable`object ..
mMascotView.setBackground(mMascotAnimation);
// start the animation ..
mMascotAnimation.start();
Note: You should not call the AnimationDrawable.start()inside the onCreate() method of the activity. The views are not ready yet. You should use the callback on onWindowFocusChanged() method and start the animation there:
#Override
public void onWindowFocusChanged (boolean hasFocus)
{
//Start animation here
if(hasFocus) {
mMascotAnimation.start();
}
}
I am working on a small 2d game engine for android-ndk using opengl.
I am facing difficulty on how to change levels, eg. from menu to game screen.
Because the texture ids are not working when loading new textures for game screen using glGenTextures, glGenTextures keeps returning duplicate ids.
// class to bind ndk with java
Renderer.java
void onSurfaceChanged(gl, width, height){
nativeSurfaceChanged();
}
void onDrawFrame(){
nativeDrawFrame();
}
// c++ code
Game.cpp
Screen *screen;
void SetScreen(Screen *scrn){
screen = scrn;
// load textures and create openGL objects (mesh and textures)
screen->Initialize();
}
void Update(){
screen->Update();
screen->Render();
}
NDKActivity.cpp
Game *game;
void nativeSurfaceChanged(){
// initlizes stuff like audio engine, rendering engine
game->Initialize();
// set current screen to main menu
game->SetScreen(new MainMenu());
}
void nativeDrawFrame(){
game->Update();
}
MainMenu.cpp
void Update(){
// if some button is clicked
game->SetScreen(new GameScreen());
}
Now when menu is initialized, everything works fine. But on loading GameScreen textue ids get all mixed up
I basically ported it from windows app where I was using GLUT for creating OpenGL context and there it was working fine.
And please let me know if more info is needed
glGenTextures keeps returning duplicate ids
That should not happen, unless you're deleting the texture with that ID somewhere (else) or end up in a different GL context.
I am facing difficulty on how to change levels, eg. from menu to game screen.
It's as easy as drawing something different. Have two functions draw_menu and draw_level; maybe a draw_loadingscreen and while you're in the loading screen or menu you can replace all the stuff the level uses.
Or you could use a modern streaming approach, where you don't discriminate between menu, loading screen and level and instead upon drawing each frame collect the resources required to draw it, load what's not yet loaded into the GL context and then draw.
Deleting GL resources can be mostly offloaded into a background garbage collection routine. I.e. every frame for every resource of your game you increment a counter; in the frame setup, when you actually are about to draw it you reset the counter to zero. Once the counter hits a certain threshold you delete it. If when loading resources into GL you hit an out-of-memory condition you can work yourself through the resources with the highest counter value downward, deleting them from the GL context.
so as it turns out I was listening for change screen method in OnTouch() method which apparently runs on different thread than opengl and thus every thing was falling apart. So I had to add a callback which would change screen in onDrawFrame and it fixed the issue.
thanks for helping :)
I recently asked a question about translucent components causing odd artifacts from seemingly not updating properly. The answer I received caused the artifacts to go away, but at the cost of translucency.
The solution was to- for every translucent component- also call the setOpaque(false) function. This way, Swing knew that it needed to redraw the background behind those components.
However, this came at the cost of the translucency that I was trying to achieve. It caused the components to become transparent instead.
The premise is this: I am designing the GUI for a chat client, and a feature request was to have a background. I successfully got the background working by following a code snippet for extending the JPanel class, but then I wanted the components to allow the background to show. After setting their translucency, remnants of updated components were being displayed where they shouldn't have been. I came here and got my problem solved, but now I've got a new problem. So here we are.
So, here is what I've surmised:
-Calling the setOpaque(false) function for each desired component and NOT setting a translucent color does not achieve what I want.
-Setting a translucent color and NOT calling setOpaque(false) allows the translucent background to show, but causes artifacts, putting me back at square one.
So I need some middle ground between transparent with no artifacts, and translucent with artifacts. Namely, I want a translucent background (not completely transparent) that has no artifacts.
It seems like I'm going to need to override the JFrame to cause it to repaint all its components, regardless of the opacity. Unless there's something I'm missing.. which is why I'm here!
Thanks!
(Here's a link to the original question, with a picture for reference: Java Swing - Translucent Components causing Artifacts)
One option would be to override the components and draw the background yourself:
class TranslucentLabel extends JLabel {
public TranslucentLabel(String text) {
super(text);
setOpaque(false);
}
#Override
public void paintComponent(Graphics g) {
g.setColor(new Color(255, 0, 0, 64));
Insets insets = getInsets();
g.fillRect(insets.left, insets.top,
getWidth() - insets.left - insets.right,
getHeight() - insets.top - insets.bottom);
super.paintComponent(g);
}
}
EDIT: Alternatively you could draw the translucent background colour for the child components directly onto the panel, then you would not have to override components:
class YourPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g.create();
// Draw your background image here to g2d.
g2d.setColor(new Color(255, 0, 0, 64));
Area area = new Area();
for (Component component : getComponents()) {
area.add(new Area(component.getBounds()));
}
g2d.fill(area);
g2d.dispose();
}
}
There is a disadvantage to this approach. If there is a genuinely transparent part of a component (such as a rounded border), then its entire background will be coloured.
I have a very simple application that has one screen and one button. The main screen has a verticalFieldManager with a BitmapField inside it, and one button beneath the bitmap. I want to be able to overlay another image on top of this when the user clicks a button. The overlay image is a PNG with transparent background, and this is important for design, so I can't use popupscreen or a new screen because the backgrounds are always white by default, and I've heard alpha doesn't really do the trick.
I guess what I'm asking is if anyone knows a simple way to...
A) take a standard verticalFieldManager and overlay a PNG on top of the inner contents
B) overlay a PNG over the screen, no matter the contents
The basic functionality of this app was intended to be - show an image. on click, show another overlaid on top. on click again, remove the popup image.
I haven't found anything that addresses something like this online, but I have read of people doing similar things that utilize popupscreen and new screens in a way I don't need to do.
Hopefully this makes sense.
Thanks
Have you tried something like this in your custom class that overrides a screen?
EncodedImage _overlayImage;
boolean _overlay = false;
// this is to catch the click, you might do it different
public void fieldChanged(Field field, int context)
{
if (field == _imgChangeButton) {
// get the overlay image here, however you want
_overlayImage = getEncodedImageResource(blah);
_overlay = true;
invalidate();
}
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (_overlay) {
graphics.drawImage(...);
}
}
If the PNG has transparency in it the bb drawImage stuff should deal with it ok. In the drawImage call obviously you can mess around with finding the existing image and setting the x,y right on top of it.
In general I would say do a lot of testing with paint() and messing with the graphics directly before you try to do anything with another screen. You can do a lot overriding paint() for the screen... you get the whole graphics.