#Java classes and constructors

1 messages · Page 1 of 1 (latest)

raw totem
#

I can't grasp the concept of them at all. For my project I got some basic code to already use and don't no how to utilise the parameters given to make an object.

pseudo drumBOT
#

<@&987246399047479336> please have a look, thanks.

white pilot
#

Can you please share the code and describe what exactly you dont understand? Then we will be able to work things out

raw totem
#

Hi, that's the code above. I don't understand how I can use those parameters to create a draggable circle.

#

the parameters where I've created that new instance of the object

white pilot
#

You can enter that constructor declaration and see what parameters it expects

raw totem
#

it expects int,int,int

white pilot
#

They should have descriptive names, then you will know what values are set with these numbers

raw totem
#

Like what does the constructor actually do. The code was already given, but I have to figure out utilising it myself

white pilot
#

Ah...

raw totem
#

I've created a draggable class and done the code that makes the circle draggable

#

but is the constructor for setting the values of its size and location?

#

thats the other piece of code I was given

#

for the draggable class

white pilot
#

Constructor creates an object, new instance of the class

#

Can you paste the constructor code?

raw totem
#

the code from the first image?

#

I could screenshot all the code i've written completely

#

and explain what I'm trying to do if that helps

pseudo drumBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

white pilot
#

Exactly

#

Paste it as formatted block

raw totem
#


void setup() {
  size(1000, 1000);
  obj = new Draggable(width/2,height/2,50);
  
  
}

void draw() {  //repeat this code 60 times a second – animate things on screen
  background(64);
  obj.update();
}
pseudo drumBOT
raw totem
#

{
float circleX;  //draggable circle position
float circleY;
float circleDiameter;

{
  obj.circleX = width/2;  //set circle to be in centre of screen
  obj.circleY = height/2;
  obj.circleDiameter = 50;
}
void update()


{
if ( dist(mouseX, mouseY, circleX, circleY) < circleDiameter/2) { //mouse is inside the circle
    if (mousePressed) {         //mouse is inside the circle and clicked
      fill(64, 256, 64);        //colour it bright green and move the circle to mouse position
      circleX = mouseX;    //change circle position to mouse position
      circleY = mouseY;
    } else {  //mouse is inside the circle but not clicked
      fill(128, 256, 128);  //highlight the circle light green but don't move it
    }
  } else {  //mouse is outside the circle, color it gray
    fill(128);
  }
  ellipse(circleX, circleY, circleDiameter, circleDiameter);
   }
}
pseudo drumBOT
white pilot
#

And also draggable class

raw totem
#

My aim is to create that object and put it onto the screen using the constructor I think

#

so theres a draggable circle

#

the obj = new part was given by the processor in one of his videos, but he wants us to do the rest

white pilot
#

Ok I'm blind I think, there is no constructor inside that class that takes 3 int parameters

raw totem
#

I've watched his lectures back and loads of youtube vids but it just doesn't click for me

#

yeh thats what i'm confused about

#

I think I

#

i

white pilot
#

So the obj = new Draggable() is part written by ur teacher?

vale locust
#

does the constructor in your IDE underline red ?

raw totem
#

yeh exactly

white pilot
#

So maybe you have to add constructor by urself?

raw totem
#

I think so, I'm just confused how

white pilot
#

This code does not compile right? It shows any errors in IDe as fa asked?

raw totem
#

sorry for explaining this poorly

#

it has a red line under it

white pilot
#

no problemo, dont worry at all

#

Okay.. thats because Draggable class does not have an constructor like this

#

So what you need is to create that constructor by urself in Draggable class

vale locust
#

hover your mouse over the red line ( do not click )

#

most of those red lines actually say WHY its red

white pilot
#

It should say that no constructor is found for given arguments (int, int, int) or something like that

raw totem
#

I noticed that it said those values are not already done but I'm confused

#

what are those parameters actually doing

#

those 3 numbers

white pilot
#

Okay, from the beggining

raw totem
#

width/2, height/2, 50

white pilot
#

Class can have some fields, as you see in Draggable class

#

You have circleX, circleY and circleDiamter

#

Correct?

raw totem
#

Yes

white pilot
#

There are properties of your class

#

You can create various objects (instances of class) with different values for these fields

raw totem
#

so the location and diameter of the circle are set within the class?

white pilot
#

Here comes the constructor in your case

#

Constructor is kinda "special" method for a class which constructs its instance

raw totem
#

ohhhhhhh

white pilot
#

And if you want to create a new Draggable object with these values set to some values

#

You need to use specific constructor

raw totem
#

so I create the object within the class but then specific instances of it within the main code?

#

like a enemy would be a class

#

but if you want 50 of that enemy

white pilot
#

You need to provide a constructor inside the class

raw totem
#

you create 50 instances?

white pilot
#

But call the constructor somehwere in the code

white pilot
raw totem
#

so can the parameters be used to set specific values for each instance of the class

white pilot
#

Exactly

raw totem
#

like if you want a class for all the soldiers

white pilot
#
    public Draggable(float circleX, float circleY, float circleDiameter) {
        this.circleX = circleX;
        this.circleY = circleY;
        this.circleDiameter = circleDiameter;
    }
pseudo drumBOT
white pilot
#

That would be your constructor in this case

raw totem
#

I could set their health different with each instance

white pilot
#

Let's break it up for you

raw totem
#

I see

white pilot
#

constructor is build from it's access parameter and a name of the class itself

#

public Draggable

#

then you implement parameters that you want to use to set values of the instance of this class

#

In this case I named them the same as the class field names

raw totem
#

I assume that code would go within the class?

white pilot
#

Within the Draggable class

#

Class contains set of contructors inside of the class itself

raw totem
#

I see, I see

white pilot
#

If you paste it inside the class (or write by yourself) you will see that error in the main class will dissapear

raw totem
white pilot
#

no no

#

look again

#

the bracket is in the wrong place

raw totem
#

ohhhhhhh

#

there we go

white pilot
#

access modifier class name arguments in parenthesis {
method body
}

raw totem
#

wait so

#

by using a constructor

#

do i get rid of that

#

like does that need removing

#

or where does that go

white pilot
#

You get rid of that

raw totem
#

It workeddd

#

woohoo

#

first time using a constructor, that makes so much more sense now

white pilot
#

I would tell you some more, but that's probably the thing for your lectures

#

That would cause too much chaos for you now 😄

raw totem
#

That's the issue

#

the lectures ended and the project needs to be in within the next few days now

#

Long story short loads happened that made me unable to attend the lectures, so I've had to cram everything in at the end

white pilot
#

I see

#

Well, feel free to ask more questions in here then

#

You can also ping me in PM, but I will answer only if I have some spare time

raw totem
#

what exactly is drawing the object onto my screen? also thank you for the pm thing

vale locust
#

( just do no push to many screenshots of code in here, but actual code )

raw totem
#

I'll do that in future for sure

#

is it the parameters that are drawing the object?

white pilot
#

Ok, so that's kinda specific for the project you are doing - from the code I'm not really sure how it should be drawn

vale locust
#

mobile has issues with images, and most of use are alot more confortable actualy seeing the code ( and c&p parts when needed )

raw totem
#

like its being draw doop

#

but idk which code is setting its location and actually drawing it there

#

cause i ran it and its been drawn

white pilot
#

What IDE are you using?

vale locust
white pilot
#

Exactly

raw totem
#

im using processing

white pilot
#

Because from the code you sent, there are no imports for any classes or methods for drawing anything to be honest

raw totem
#

i do have a void draw followed by obj.update

#

would the obj.update be drawing it?

white pilot
#

Right, but what I mean is that the processing gives you some methods from it's backend to perform some actions

vale locust
#
Renderers

Processing has three built-in screen renderers. The default renderer is for drawing two-dimensional shapes. P2D is a faster, but less accurate renderer for drawing two-dimensional shapes. P3D is for three-dimensional geometry; it can also control the camera, lighting, and materials. The P2D and P3D renderers are accelerated if your computer has an OpenGL compatible graphics card. The smooth() function affects the amount of antialiasing for each renderer. Check the reference for smooth() for more information.
pseudo drumBOT
white pilot
#

Exactly

vale locust
white pilot
#

So let me break it for you also

vale locust
#

setup / draw etc is all backend

white pilot
#
    void update() {
        if (dist(mouseX, mouseY, circleX, circleY) < circleDiameter / 2) { //mouse is inside the circle
            if (mousePressed) {         //mouse is inside the circle and clicked
                fill(64, 256, 64);        //colour it bright green and move the circle to mouse position
                circleX = mouseX;    //change circle position to mouse position
                circleY = mouseY;
            } else {  //mouse is inside the circle but not clicked
                fill(128, 256, 128);  //highlight the circle light green but don't move it
            }
        } else {  //mouse is outside the circle, color it gray
            fill(128);
        }
        ellipse(circleX, circleY, circleDiameter, circleDiameter);
    }
pseudo drumBOT
white pilot
#

This code makes it draw

#

the ellipse method call on the end possibly does it

#

If you hover your mouse over the drawn circle does it change the colour?

#

Or if you click it?

raw totem
#

Yeh, it goes green

white pilot
#

Ok so look

#

This code I pasted above checks for your distance between the circle itself and your mouse

#

First if statement checks if the mouse is inside the circle (hovering above it)

#

Then it's checking if you are pressing your mouse - if yes, it colours it to green by:
fill(64, 256, 64);

#

These 3 numbers are int values of the color

#

Then if you also move your mouse it changes the circleX and circleY values which are the coordinates of your circle - it updates it to your mouse coordinates

#
                circleX = mouseX;    //change circle position to mouse position
                circleY = mouseY;
#

This means that you change circle coordinates to your mouse coordinates (so basically that means you are dragging it)

raw totem
#

ohhhhhh that makes sense now

white pilot
#

The first else statements means that if you are not pressing the circle, the color is light green and you cannot move it because you dont press the mouse button

#

And the second else statement is a case when your mouse is outside the circle - then its gray, right?

#

Then the last line of the update method just invokes method that Processing provides - ellipse will probably draw a circle with given coordinations and diameters

#

In your case you have the circleDiameter used there twice, that means that vertically and horizontally it will have the same diameter (which makes it a circle, not an ellipse) 😄

#

And now getting back to your main class

#

Processing probably expects some methods to be implemented to work - in your case setup() methods setups the "playground" for you to work on with a given size, and there you can for example create your instances of the classes

#

And draw() method is also used by processing to show the things on the screen, as you see in the comment - it repeats it's body 60 times a second

#

I hope it's understandable

#

Feel free to tell if there's something unclear

raw totem
#

yeh I'm a little confused rn

#

i fixed it

white pilot
#

Whats your confusion?

raw totem
#

The code wasn't running, but it was just a case of bracket placement

#

I'm trying to figue out colission now

dense spindle
#

Are you the person with extremelly weird code for checking if the mouse is pressed ?

vale locust
#

he is, and it the site that he's using that has the code in its backend

dense spindle
#

he is using processing

vale locust
#

yep

dense spindle
#

it's not a website

vale locust
#

my bad then, but the weird bits he's using are from there