Netbeans:get the X and Y co ordinate - netbeans-7

I have an image in the jlabel. I want to get the X and Y co-ordinate when ever it is clicked on. I used the following code:
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
f = jLabel2.getMousePosition();
m = new Point(f).toString();
}
And I got the output:
java.awt.Point[x=165,y=105]
But I don't know how take the x and y separately.

The point class two public fields which are x and y. You can access them through your instance variable.
You do the following. int x1 = m.x; int y1 = m.y.
if you need extra information and some useful methods, read the Java Api.
http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

Related

How to do multiple variable assignments in one line in Kotlin like C,C++?

I had to swap 2 numbers in one line expression using no other variable except x and y.
So I wrote the following .c program to swapp two numbers with the given conditions and it works like charm.
int main() {
int x =5, y =2;
x = y-x+(y=x);
printf("X=%d, y=%d", x, y);
return 0;
}
But when i try to do the same in kotlin it gives me an error that
Assignments are not expressions, and only expressions are allowed in
this context,
I can resolve this issue by introducing a third variable just like this. But I'm not allowed to have any other variable except x and y which are already given. So is there any other way I can do this in one line using any kotlin property?
Below is the kotlin program
fun main() {
var x = 5
var y = 10
x = y-x+(y=x)
println("X = $x, Y = $y")
}
While I have two suggestions below, I want to start with a recommendation against either of them, at least in this simple example.
It's usually a lot more clear to optimise code for developers to read in the following ways:
create an extra variable with a descriptive name
prefer val over var to avoid accidental mutations
and try to make the code 'linear', so the operations can be read from top-to-bottom without jumping between functions
avoid code that needs an IDE to see what the type-hints are
And I'll trust that the compiler will make make the code performant.
fun main() {
val x = 5
val y = 10
val newX = y
val newY = x
println("X = $newX, Y = $newY")
}
Local function
You could use a local function to perform the swap, as the function will still be able to access the original values.
fun main() {
var x = 5
var y = 10
fun swap(originalX: Int, originalY: Int) {
y = originalX
x = originalY
}
swap(x, y)
println("X = $x, Y = $y")
}
Scope function
This could be code-golfed into one line
use to to create a Pair<Int, Int>,
and a scope function to use the result.
fun main() {
var x = 5
var y = 10
(x to y).apply { x = second; y = first }
println("X = $x, Y = $y")
}
One line? Yes. More difficult to read? I think so.

How can a module defined in a .ml file reference itself

I am trying to define a Point module that defines a type to represent 2d points.
I would also like to include a submodule Point.Set so that Point.Set.t is
a type meaning 'a set of Points'. That seems logical and convenient, but I am not able to figure out how to make the 'circular' reference that this involves.
I tried this:
file: point.ml (implicitly defines a 'Point' module)
type t = {x: int; y:int}
let compare {x=x1;y=y1} {x=x2;y=y2} = ...implementation omitted for brevity...
module Set = Stdlib.Set.Make(Point)
(* ^^^^^ Internal path Mylib__Point is dangling *)
When I dune build the Mylib project/library this is in. I get an error:
Internal path Mylib__Point is dangling.
The compiled interface for module Mylib__Point was not found.
I am not entirely sure what the error really means, but I gather it probably has something to do with the fact that we are trying to reference the Point module from within itself. And maybe that is not allowed?
I can work around this by instead defining a separate 'pointSet.ml' file and in there have include Set.Make(Point). Now I have a module called PointSet. That is okay, but I still would find it a bit more 'aesthetically pleasing' if Point.Set could be a submodule of Point instead. Is there a way to make this work?
If you don't mind a little bit of boilerplate, I think this solution may suit you:
point.ml
module Point = struct
type t = { x : int; y : int }
let compare { x = x1; y = _y1 } { x = x2; y = _y2 } = x1 - x2
end
module Set : Set.S with type elt = Point.t = Set.Make (Point)
include Point
You'll have access to Point.Set and since point.ml includes the module Point at the end of the file, you won't have to do Point.Point.compare ... in other files.
[EDIT]
I previously made the modules mutually recursive but in this case it's useless. If you need them to be mutually recursive you'll have to explicit their signatures:
point.ml
module rec Point : sig
type t
val compare : t -> t -> int
end = struct
type t = { x : int; y : int }
let compare { x = x1; y = _y1 } { x = x2; y = _y2 } = x1 - x2
end
and Set : (Stdlib.Set.S with type elt = Point.t) = Stdlib.Set.Make (Point)
include Point
As far as I know a module doesn't have a name for itself. You can make a module (a struct) just for the purpose of supplying it to the functor Set.Make:
type t = { x : int; y : int }
let compare a b = compare a b
module Set =
Set.Make(struct type nonrec t = t let compare = compare end)

game maker random cave generation

I want to make a cave explorer game in game maker 8.0.
I've made a block object and an generator But I'm stuck. Here is my code for the generator
var r;
r = random_range(0, 1);
repeat(room_width/16) {
repeat(room_height/16) {
if (r == 1) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
now i always get a blank frame
You need to use irandom(1) so you get an integer. You also should put it inside the loop so it generates a new value each time.
In the second statement, you are generating a random real value and storing it in r. What you actually require is choosing one of the two values. I recommend that you use the function choose(...) for this. Here goes the corrected statement:
r = choose(0,1); //Choose either 0 or 1 and store it in r
Also, move the above statement to the inner loop. (Because you want to decide whether you want to place a block at the said (x,y) location at every spot, right?)
Also, I recommend that you substitute sprite_width and sprite_height instead of using the value 16 directly, so that any changes you make to the sprite will adjust the resulting layout of the blocks accordingly.
Here is the code with corrections:
var r;
repeat(room_width/sprite_width) {
repeat(room_height/sprite_height) {
r = choose(0, 1);
if (r == 1)
instance_create(x, y, obj_block);
y += sprite_height;
}
x += sprite_width;
}
That should work. I hope that helps!
Looks like you are only creating a instance if r==1. Shouldn't you create a instance every time?
Variable assignment r = random_range(0, 1); is outside the loop. Therefore performed only once before starting the loop.
random_range(0, 1) returns a random real number between 0 and 1 (not integer!). But you have if (r == 1) - the probability of getting 1 is a very small.
as example:
repeat(room_width/16) {
repeat(room_height/16) {
if (irandom(1)) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
Here's a possible, maybe even better solution:
length = room_width/16;
height = room_height/16;
for(xx = 0; xx < length; xx+=1)
{
for(yy = 0; yy < height; yy+=1)
{
if choose(0, 1) = 1 {
instance_create(xx*16, yy*16, obj_block); }
}
}
if you want random caves, you should probably delete random sections of those blocks,
not just single ones.
For bonus points, you could use a seed value for the random cave generation. You can also have a pathway random generation that will have a guaranteed path to the finish with random openings and fake paths that generate randomly from that path. Then you can fill in the extra spaces with other random pieces.
But in regards to your code, you must redefine the random number each time you are placing a block, which is why all of them are the same. It should be called inside of the loops, and should be an integer instead of a decimal value.
Problem is on the first line, you need to put r = something in the for cycle

How to initialize a variable twice?

I'm trying to initialize a variable twice in 2 different functions without the second initialization having effect on the first.
float X;
void setup()
{
size(400, 400);
background(255);
}
void draw()
{
Rect1();
Rect2();
}
void Rect1()
{
fill(255,0, 0);
rect(X, 20, 40, 40);
X=20;
}
void Rect2()
{
fill(0, 255, 0);
rect(X, 200, 40, 40);
X=50;
}
You code seems to be "demonstration" code, but it doesn't really do anything significant other than show that you probably need to first sit down and learn a bit about Processing before you continue - hit up processing.org and run through some of the tutorials. It's worth it.
The code you gave can be written much more sensibly, but instead I'll answer the question; Processing uses a variation on Java's scoping rules: a local variable trumps an object instance variable, which trumps a global variable:
int x = 0;
int y = 50;
class Thing {
int y = 10;
Thing() {
// x is global, y is global and object instance
println("x (in Thing): "+x);
println("y (in Thing): "+y);
declareAndCheck();
}
void declareAndCheck() {
// now we make some local variables: they win.
int x = 40;
int y = 100;
println("x (local declared): "+x);
println("y (local declared): "+y);
}
}
void setup() {
println("x (global): "+x);
println("y (global): "+y);
Thing t = new Thing();
// and global is still global
println("x (global, again): "+x);
println("y (global, again): "+y);
}
This will generate the following output:
x (global): 0
y (global): 50
x (in Thing): 0
y (in Thing): 10
x (local declared): 40
y (local declared): 100
x (global, again): 0
y (global, again): 50
Why? First we see this:
x (global): 0
y (global): 50
because x and y are global variables with values 0 and 50. Simple enough. then we see:
x (in Thing): 0
y (in Thing): 10
because in the "Thing" object, we have an object instance variable 'y', which wins the naming conflict between it, and the global variable.
Then we enter declareAndCheck, where we see:
x (local declared): 40
y (local declared): 100
because we now have a global x, and a local x, and local always wins, and we have an instance y, and a local y, and again local always wins.
Finally we print x and y in global context again, and because there is no conflict, we see:
x (global, again): 0
y (global, again): 50
And if that did not make sense to you, run through those tutorials, they teach you programming in Processing =)

Dynamic grid using FlashDevelop & Actionscript 2.0

I'm new to actionscript. What I'm tryin to do is simulate traffic flow near a 2 lane intersection, following Wolfram's rule 184. To begin with, I'm trying to create a grid (8x8 of which the intersection is between the middle two rows and the middle two columns, like a plus sign) whose cells have the following attributes:
color = white;
car = false;
when clicked:
color = red;
car = true (a car is present);
So, after the user clicks cells to position the cars initially and presses the start button, the simulation will begin.
Here's my code so far (apologies for incorrect formatting):
class Main
{
private var parent:MovieClip;
public static function main(mc:MovieClip)
{
var app = new Main(mc);
}
public function Main(mc:MovieClip)
{
this.parent = mc;
//grid settings
var Cell:MovieClip = mc.createEmptyMovieClip("cell", mc.getNextHighestDepth());
var x:Number = 0;
var y:Number = 0;
var color:Number = 0xffffff;
var car:Boolean = false;
for (y = 0; y < 3 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8) - 5, (Stage.height / 8) - 5, color, 100);
}
}
for (y = 3*Stage.height/8; y < 5 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 0; x < Stage.width; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
for (y = 5*Stage.height/8; y < Stage.height; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
Cell.onMouseDown()
{
Cell.color = UI.RED;
Cell.car = true;
}
}
}
I know there's quite a few things gone wrong here. First of all, the cell color doesn't change on mouse down. Do i need to make movie clip for each cell in the for loops? I think it would be easier to make a grid of objects with given attributes, but i don't know how to do that. Would really appreciate if someone helps me out.
From what I can tell, issue with your current approach is that using drawRect() literally draws pixels on to the stage, which means you'll have no reference to those shapes in future frames. right now, you've got one MovieClip that has been drawn many times. What you need is a lot of MovieClips so you have a reference to each cell that you can update/edit every frame.
Your best bet is to do the following (I'll just provide pseudo because I'm a bit shaky on AS2 syntax):
A) Create an array to hold all of the Cells. Call it:
var Cells:Array = new Array();
B) During each step of the loops in your constructor, do 4 things.
1) Create a new MovieClip `var tempCell:MovieClip = new MovieClip();
2) Draw a rectangle on to each MovieClip: A tutorial for the graphics API in AS2 http://www.actionscript.org/resources/articles/727/1/Drawing-shapes-with-AS2/Page1.html
3) Add an event listenerto each MovieClip that points to a common event handler. This listener listens for mouse clicks on that MovieClip (or MOUSE_DOWN)
4) and use Cells.push(tempClip) to add that new MovieClip to your array so you now have one object that contains a reference to all of your cells.
C) Create an click event handler that redraws the cell that has been clicked. Try MouseEvent.target
You have another option to using the graphics API to draw rectangles, and that is to simply add and remove stock graphics from your Flash library. You'll have to draw these graphics in Flash and then 'Export for Actionscript' to call them up.
Hope this points you in the right direction!
J