How to rotate this arm upwards? - java

I am learning Java with the program named Processing.
However, I cannot understand why my code do not work appropriately.
I made a arm with this problem which should be rotated to upside or downside when I hold the mouse button, but it just works when I hold the left button on my mouse, and it does not work when I hold the right button.
The problem is I do not know the problem of my code as follows.
float angle=0;
float angleDirection=1;
float speed=0.005;
void setup(){
size(800,600);
}
void draw(){
background(255,255,255);
stroke(20,20,255);
translate(400,300);
rotate(angle);
strokeWeight(18);
line(0,0,140,0);
pushMatrix();
translate(140,0);
rotate(angle*2.0);
strokeWeight(14);
line(0,0,100,0);
translate(100,0);
rotate(angle*2.5);
strokeWeight(10);
line(0,0,60,0);
popMatrix();
rotate(-angle*2.0);
strokeWeight(18);
line(0,0,-140,0);
translate(-140,0);
rotate(-angle*2.0);
strokeWeight(14);
line(0,0,-100,0);
translate(-100,0);
rotate(-angle*2.5);
strokeWeight(10);
line(0,0,-60,0);
if(mousePressed){
if(mouseButton==LEFT){
angle=angle+speed*angleDirection;
if((angle>QUARTER_PI)||(angle<0)){
angle=QUARTER_PI;
}
if(mouseButton==RIGHT){
angle=angle+speed*angleDirection;
if((angle>QUARTER_PI)||(angle<0)){
angleDirection=-angleDirection;
angle=QUARTER_PI;
}
}
}
}
}

You're almost there, but accidentally made a logical error: the right button condition is nested inside the left button condition. Since the mouseButton can only be either left or right (but not both), the right button condition will never trigger. Simply move it outside of the left button condition:
if (mousePressed) {
if (mouseButton==LEFT) {
angle=angle+speed*angleDirection;
if ((angle>QUARTER_PI)||(angle<0)) {
angle=QUARTER_PI;
}
}
if (mouseButton==RIGHT) {
angle=angle+speed*angleDirection;
if ((angle>QUARTER_PI)||(angle<0)) {
angleDirection=-angleDirection;
angle=QUARTER_PI;
}
}
}
(Edit > Auto Format (Ctrl+T / CMD + T) will make it easier to spot {} issues)
Have fun learning!

Related

How can I get first data while using MouseAdapter?

I want to make code that When I pressed left upper area, the rectangle chosen and resize it using drag and drop (right down is fixed and use left up point to resize).
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
start = e.getPoint();
if((up.a<start.x&&start.x<up.a+10&&up.b<start.y&&start.y<up.b+10)) {//When I pressed Rectangle's left up area
Label.setText("up");
if(tempshape==null) {
tempshape=shapes.get(now);
}
ud=1;
}
tempshape is a global variable and I want to get only first data so
I claimed tempshape as:
shape tempshape=null;
if(ud==1) {
else if(nowshape=="Rectangle") {
shapes.get(now).setA(end.x);
shapes.get(now).setB(end.y);
shapes.get(now).setC(tempshape.c+end.x);
shapes.get(now).setD(tempshape.d+end.y);
}
} ]
But tempshape is still update it's value while I dragging so I get trash data from tempshape.
I want to know:
Why tempshape is updating while dragging? (I only Write tempshape's update code in pressed.)
How can I solve this problem?

Processing: How to make Sphere that rotates when mouseClicked?

In my program, I want a sphere to represent a globe. I want the user to press down the mouse1 button and rotate the sphere. When the user is not pressing the mouse1 down, the sphere should just be staying still regardless of how the mouse is moving. In simple terms, I want a sphere to rotate only when the mouse is pressed. Here is my code for the sphere rotating.
float lockXRotation;
float lockYRotation;
void setup() {
size(1000,700,P3D); //reference
}
void draw() {
mouseAction();
fill(200, 0, 160);
if(mousePressed == false){
rotateX(lockXRotation);
rotateY(lockYRotation);
}
sphere(100);
}
void mouseAction () {
translate(500, 350, 0); //reference starts here'
if(mousePressed){
rotateX(mouseY * -0.01);
rotateY(mouseX * -0.01); //ends here
lockXRotation = 0;//what should i put so it will stay still?
lockYRotation = 0;
}
}
I have tried mouseX * -0.01 for lockXRotation but it offsets by the distance of the x to the origin. Is there a function to find the rotation an object is in?
Okay, so you want the sphere's location to lock when the user presses the mouse.
I took a look at your code and ran it on Processing, I believe that your lockXRotation is not doing what it is suppose to.
If you look at lines 12 - 15, where it says if (mousePressed == false), then it should rotateX(lockXRotation), those lines don't belong in the draw function, but if you put them in the mouseAction function, that might solve the problem.

Create Java 2D gravity?

I'm creating java game (I'm a beginner with this for now) and I'd like to start with some kind of platform game.
I'd like to know how I can make the player jump (I know how to move him up and down), but I don't know how how to make him go back down after going up.
Here is my code:
public void keyPress() {
if (listener.arrowUp) {
Jump();
}
}
private void Jump() {
if(player.get(1).getPosY() > maxJump) {
player.get(1).moveY(-10);
} else if(player.get(1).getPosY() == maxJump) {
player.get(1).moveY(85);
}
}
So.. the player moves -10px upwards as long as i press 'w' and when he hits maxJump (which is 375 and players position at the start is 465) he "teleports" back to 465 instead of sliding back down like he does when going up.. It's really hard to explain this without a video, but i hope somebody understands and can help me with this.
This question gives a basic answer to yours. Now in your jump function, you have to set the vertical_speed only once and only call fall() every frame and add some moveY.
Here are two alternatives:
Option 1. Simulating some very simple physics. You add forces to your player, so pressing the up arrow will add a force that makes the player move upwards. Gravity is constantly applied and thus it will gradually cancel out the force you applied when the up arrow was pressed. Perhaps you only want to use forces in the vertical direction you do something like this:
// Each frame
if(notOnTheGround){
verticalVelocity -= GRAVITATIONAL_CONSTANT;
}
// When user jumps
vertivalVelocity += JUMP_FORCE;
Option 2. An alternative is to kind of animate the jumps using projectile motion.
// Each frame
if(isJumping){
verticalVelocity = initialVelocity*sin(angle) - g*animationTime;
animationTime += TIME_STEP;
}
// When user jumps
isJumping = true;
animationTime = 0;
initialVelocity = ... // calculate initial velocity

Panning on GUI map Java

This is my first post so if any other information is needed please let me know.
I'm making a game in a Java GUI and just have two hopefully quick questions about which. I can currently paint a map to the screen and pan around the map with the following code:
private class MoveMap implements MouseMotionListener{
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
if(e.getX() > swidth-30){
if(xmod+(columns*30) > swidth){
xmod-=30;
repaint();
}
}
else if(e.getX() < 30){
if(xmod < 0){
xmod+=30;
repaint();
}
}
else if(e.getY() > sheight-30){
if(ymod+(rows*30) > sheight){
ymod-=30;
repaint();
}
}
else if(e.getY() < 30){
if(ymod < 0){
ymod+=30;
repaint();
}
}
else{
}
}
}
The only problem is the mouse must continually move at the edge of the screen to continually pan. My question is if there's a way to have the mouse at the edge of the screen and continually pan while updating the graphics? My second question would be if this could be applied to characters moving as well? I thought maybe a thread would be a possible solution but am not familiar with using them. Thank you!
I can think of two ways to achieve this.
You could...
Start a javax.swing.Timer that would update the x/y position as required and repaint the screen when ever the mouse entered within a given distance of the edge of the screen. You would, obviously stop the timer as the mouse moved back out ;)
You could...
Use a background Thread to monitor the mouse position and when it enters the "trigger" zone, would update the x/y values and trigger a repaint, making sure you re-sync the calls to the EDT ;)

Java Application : mouseDragged Event isn't Executed Often Enough

Is there a way to make the mouseDragged Event be called more Often ( In my Case, Drawing a Color? I need it for Smooth Drawing, because right now, if you move too fast, it doesn't Draw All my Path. Also, I have an 2D Array Storing the Color of the Pixel, so that's also Problematic if I try to solve by problem by another Way, that's why I thought Increasing the mouseDragged Frequency would be the Best thing to Do
Thanks
If you want smooth drawing, you'd likely have to interpolate the data yourself. If you get an event at (3,3) and another at (10,10) you can figure the slope between the two, and iterate through the logical points that the mouse must have been dragged to get from (3,3) to (10,10)
I don't know of a way to force mouseDragged to update faster, and if, for instance the system was under high load, or someone used a touch screen, you might get huge jumps anyhow.
If you are drawing ovals to as color lines, change to lines:
ArrayList<> colors;
mousepressed(Event e) {
startPoint = e.getPoint();
}
mousedragged(Event e) {
colors.add(new Color(startPoint, e.getPoint);
startPoint = e.getPoint();
}
class Color() {
Color(Point start, Point end) {
// ...
}
paint(Graphics g) {
g.drawLine(start, end);
}
}

Categories