#Java classes and constructors
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Can you please share the code and describe what exactly you dont understand? Then we will be able to work things out
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
You can enter that constructor declaration and see what parameters it expects
it expects int,int,int
They should have descriptive names, then you will know what values are set with these numbers
Like what does the constructor actually do. The code was already given, but I have to figure out utilising it myself
Ah...
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
Constructor creates an object, new instance of the class
Can you paste the constructor code?
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
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.
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();
}
Detected code, here are some useful tools:
Draggable obj;
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();
}
{
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);
}
}
Detected code, here are some useful tools:
And also draggable class
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
Ok I'm blind I think, there is no constructor inside that class that takes 3 int parameters
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
So the obj = new Draggable() is part written by ur teacher?
does the constructor in your IDE underline red ?
yeh exactly
So maybe you have to add constructor by urself?
I think so, I'm just confused how
This code does not compile right? It shows any errors in IDe as fa asked?
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
hover your mouse over the red line ( do not click )
most of those red lines actually say WHY its red
It should say that no constructor is found for given arguments (int, int, int) or something like that
I noticed that it said those values are not already done but I'm confused
what are those parameters actually doing
those 3 numbers
Okay, from the beggining
width/2, height/2, 50
Class can have some fields, as you see in Draggable class
You have circleX, circleY and circleDiamter
Correct?
Yes
There are properties of your class
You can create various objects (instances of class) with different values for these fields
so the location and diameter of the circle are set within the class?
Here comes the constructor in your case
Constructor is kinda "special" method for a class which constructs its instance
ohhhhhhh
And if you want to create a new Draggable object with these values set to some values
You need to use specific constructor
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
You need to provide a constructor inside the class
you create 50 instances?
But call the constructor somehwere in the code
Exactly
so can the parameters be used to set specific values for each instance of the class
Exactly
like if you want a class for all the soldiers
public Draggable(float circleX, float circleY, float circleDiameter) {
this.circleX = circleX;
this.circleY = circleY;
this.circleDiameter = circleDiameter;
}
Detected code, here are some useful tools:
That would be your constructor in this case
I could set their health different with each instance
Let's break it up for you
I see
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
I assume that code would go within the class?
Within the Draggable class
Class contains set of contructors inside of the class itself
I see, I see
If you paste it inside the class (or write by yourself) you will see that error in the main class will dissapear
access modifier class name arguments in parenthesis {
method body
}
wait so
by using a constructor
do i get rid of that
like does that need removing
or where does that go
You get rid of that
It workeddd
woohoo
first time using a constructor, that makes so much more sense now
I would tell you some more, but that's probably the thing for your lectures
That would cause too much chaos for you now 😄
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
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
what exactly is drawing the object onto my screen? also thank you for the pm thing
( just do no push to many screenshots of code in here, but actual code )
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
mobile has issues with images, and most of use are alot more confortable actualy seeing the code ( and c&p parts when needed )
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
What IDE are you using?
your working on a school specific site, it might do some things in the backend that are not shown)
Exactly
im using processing
Because from the code you sent, there are no imports for any classes or methods for drawing anything to be honest
Right, but what I mean is that the processing gives you some methods from it's backend to perform some actions
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.
Detected code, here are some useful tools:
Exactly
The Processing Environment includes a text editor, a compiler, and a display window. It enables the creation of software within a carefully designed set of constraints.
So let me break it for you also
setup / draw etc is all backend
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);
}
Detected code, here are some useful tools:
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?
Yeh, it goes green
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)
ohhhhhh that makes sense now
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
Whats your confusion?
The code wasn't running, but it was just a case of bracket placement
I'm trying to figue out colission now
Are you the person with extremelly weird code for checking if the mouse is pressed ?
he is, and it the site that he's using that has the code in its backend
he is using processing
yep
it's not a website
my bad then, but the weird bits he's using are from there