Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

mouse click

hi guys

im trying to create a little programme where when i click on a block, the block moves across from the left to the right and right to left in a loop.

this is my code for the block

Rect w = new Rect(50,50,50,50);
d.setForeground(Color.green);
d.fill(w);

id be grateful if u could help me thanks

p.s im using the java elements pckage
[391 byte] By [tanbir_1987] at [2007-11-11 7:20:31]
# 1 Re: mouse click
one possible way is to loop through all the blocks when the mouse is clicked. Check to see if the mouse cursor is within the coordinates of a block to know which one is clicked. Or add a mouse listener to each block.
Phaelax at 2007-11-11 22:38:44 >
# 2 Re: mouse click
u want it to move when they click how?
just randomly
set coordinates to x y variables
move them as user clicks in mousePressed

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;

public class Tetris extends Frame implements MouseListener, KeyListener
{
protected TetrisBlock t;
protected Graphics g;
protected final static int SIZE = 15;
protected boolean gotBlock = false;
final int left = KeyEvent.VK_LEFT;
final int right = KeyEvent.VK_RIGHT;
int realX;
int realY;
public Tetris ()
{
super ("Tetris"); // Set the frame's name
setSize (550, 550); // Set the frame's size
setVisible (true);
addWindowListener (new WindowCloser ());
addMouseListener (this);
addKeyListener (this);
g = getGraphics ();
getBlock ();

}

public void keyTyped (KeyEvent e)
{

}

public void keyPressed (KeyEvent e)
{
if (e.getKeyCode () == KeyEvent.VK_LEFT)
{
realX += 100;

}
// if (e.getKeyCode () == KeyEvent.VK_RIGHT)
// System.out.println ("hi");
// if (e.getKeyCode () == KeyEvent.VK_DOWN)
// System.out.println ("hi");
// if (e.getKeyCode () == KeyEvent.VK_UP)
// System.out.println ("hi");

}

public void keyReleased (KeyEvent e)
{
}

public void getBlock ()
{
g = getGraphics ();
Color color;
int xL = 0;
int Orientation = 0;
int offSet = 0;
String s = "";
xL = (int) (Math.random () * 4);
if (xL == 0)
{
color = Color.blue;
t = new SquareBlock (color);
drawGrid ();
}
else if (xL == 1)
{

color = Color.red;
t = new LineBlock (color);
Orientation = (int) (Math.random () * 2) + 1;
if (Orientation == 1)
t.Orientation = 0;
else if (Orientation == 2)
t.Orientation = 90;
drawGrid ();
}
else if (xL == 2)
{
color = Color.cyan;
offSet = (int) (Math.random () * 2) + 1;
Orientation = (int) (Math.random () * 2) + 1;
if (Orientation == 1)
Orientation = 0;
else if (Orientation == 2)
Orientation = 90;
if (offSet == 1)
offSet = -1;
else if (offSet == 2)
offSet = 1;
t = new TwoAndTwoBlock (color, Orientation, offSet);
drawGrid ();
}
else if (xL == 3)
{
color = Color.yellow;
offSet = (int) (Math.random () * 3) + 1;
Orientation = (int) (Math.random () * 4);

if (Orientation == 0)
{
Orientation = 0;
}
else if (Orientation == 1)
{
Orientation = 90;
}
else if (Orientation == 2)
{
Orientation = 180;
}
else if (Orientation == 3)
{
Orientation = 270;
}

t = new ThreeAndOneBlock (color, Orientation, offSet);

}
g.clearRect (0, 481, 550, 490);
g.clearRect (482, 0, 550, 550);
g.drawString (t.toString (s), 5, 500);
t.setLocation (5, 500);
t.draw (g);

}

public void paint (Graphics g)
{
Color c;
int Orientation = 0;
g = getGraphics ();
drawGrid ();

}

public static void main (String[] args)
{
new Tetris (); // Create a TetrisBlock frame
} // main method

public void mouseClicked (MouseEvent e)
{
String s = "";
int a = 0;
g = getGraphics ();
int x = e.getX ();
int y = e.getY ();
if (x >= 30 && y >= 30 && x <= 480 && y <= 480)
{
x = (x / SIZE) * SIZE;
y = ((y + 30) / SIZE) * SIZE - 30;
if (x + (t.acrs (a) * SIZE) <= 481)
{
gotBlock = false;
// t.setLocation (x, y);
moveIt (x, y, g);
getBlock ();
}
else
{
g.drawString ("Wont fit!!!", 50, 520);
}

}
}

public void moveIt (int x, int y, Graphics g)
{
int a = 0;
g = getGraphics ();
realY = y;
realX = x;
realY = 0;
int aboveValue = 460;
if (t.type (a) == 0)
aboveValue = 460;
else if (t.type (a) == 1 && t.Orientation == 0)
aboveValue = 470;
else if (t.type (a) == 1 && t.Orientation == 90)
aboveValue = 430;
else if (t.type (a) == 2 && t.Orientation == 90)
aboveValue = 440;
else if (t.type (a) == 3 && (t.Orientation == 90 || t.Orientation == 270))
aboveValue = 440;

realY = 20;

while (realY < aboveValue)
{
for (double delay = 0 ; delay < 100000 ; delay++)
;
realY++;
//x = realX;
//y = realY;
t.setLocation (realX, realY);
t.draw (g);
paint (g);
t.clear (g);
}
realX = (realX / SIZE) * SIZE;
realY = ((realY - 30) / SIZE) * SIZE + 30;
t.setLocation (realX, realY);
t.draw (g);
paint (g);
}

public void mouseEntered (MouseEvent e)
{

}

public void mouseExited (MouseEvent e)
{
}

public void mousePressed (MouseEvent e)
{
}

public void mouseReleased (MouseEvent e)
{
}

public void drawGrid ()
{
g = getGraphics ();
for (int j = 30 ; j < 481 ; j += SIZE)
{
g.drawLine (30, j, 481, j);
}
for (int j = 30 ; j < 481 ; j += SIZE)
{
g.drawLine (j, 30, j, 481);
}

}
}

class WindowCloser extends WindowAdapter
{
public void windowClosing (WindowEvent event)
{
System.exit (0);
}
}
xsouldeath at 2007-11-11 22:39:38 >