Adding bidirectional functionality to a SelectionListener - java

I'm having some trouble with my selection listener, currently I have independently trialed selection of text from TOP to BOTTOM, and BOTTOM to TOP (Mouse movement), however these trials won't work together i.e. the one SelectionListener is bidirectional...
private void setupSelectionListener() {
this.contentValues.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent event) {
StyledText text = (StyledText)event.widget;
int x = event.x;
int y = event.y;
//Mouse Drag Listener here??? - Detects Right
//FOR TOP TO BOTTOM SELECTION
text.setSelection(event.x);
int beginPosition = event.x;
int beginByte = beginPosition / 3;
int endPosition = event.y;
int endByte = endPosition / 3;
setSelection(beginByte, endByte);
//Mouse Drag Listener here??? - Detects Left
//FOR BOTTOM TO TOP SELECTION
text.setSelection(event.y);
int beginPosition = event.y;
int beginByte = beginPosition / 3;
int endPosition = event.x;
int endByte = endPosition / 3;
setSelection(beginByte, endByte);
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// Does nothing...
}
});
So either I need a Mouse Drag Listener like I have stated in the comments, or a conditional statement comparing the event.x and event.y
I have attempted at adding a drag detection listener inside the selection listener, but this disrupts the format of the event coordinate selection.
Any help would be greatly appreciated.

If the source widget on which event happend is Styled Text object then this selection will happen automatically without any listener correct, means if you drag with mouse holding, the contents will automatically selected.

Related

Avoid repaint canvas when scrolling

I followed this tutorial, in order to display some graphical element on a canvas:
Here is where I come:
Scroll listener
vBar.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
System.out.println("Scroll event");
int vSelection = vBar.getSelection();
int destY = -vSelection - origin.y;
canvas.scroll(0, destY, 0, 0, canvas.getSize().x, canvas.getSize().y, false);
origin.y = -vSelection;
}
});
Resize listener
canvas.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(Event e) {
System.out.println("Resize event");
Rectangle client = canvas.getClientArea();
vBar.setThumb(Math.min(theoreticalScreenHeight, client.height));
vBar.setPageIncrement(Math.min(theoreticalScreenHeight, client.height));
vBar.setIncrement(20);
int vPage = canvas.getSize().y - client.height;
int vSelection = vBar.getSelection();
if (vSelection >= vPage) {
if (vPage <= 0)
vSelection = 0;
origin.y = -vSelection;
}
}
});
Paint listener
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
System.out.println("Paint event");
// Costly drawing here
...
// rectangle
for (int productIndex = 0; productIndex < products.size(); productIndex++) {
Product p = products.get(productIndex);
...
e.gc.drawRoundRectangle(rectEdge.x, rectEdge.y, (int) productWidth, productHeight, 30, 30);
Point prevSubRectEdge = rectEdge; // Coordinate of each result rectangle
for (int resultIndex = 0; resultIndex < productResultsAmount; resultIndex++) {
Result result1 = p.getResult(resultIndex);
...
e.gc.drawLine(sepStart.x, sepStart.y, sepEnd.x, sepEnd.y);
...
}
rectEdge.y += 300;
}
drawLinks(e);
}
});
Since I display a lot of graphical element, it can take some time to be displayed. That is why I would like to display them once at the beginning, and because there is a lot, I would like to scroll vertically on the canvas, so I can see all of my elements.
My problem here is that the scroll event trigger the repaint event, which is redrawing the elements. But since it's a costly operation, it is not the thing that I want.
I would like to avoid this repainting behavior, and just paint the elements once in the beginning, and then simply scroll to see them.
I have added the code, because I thought somebody would like to see it, but I don't think it is really helpfull, it was just to show the difference between the tutorial and my code. I think it is more a matter of swt comprehension, about listener and repainting life cycle ?
I also have to say that I never call canvas.redraw().
Here is a screenshot of my canvas:

Creating a clickable grid in Processing 3

I'm trying to make a grid of squares that change their fill (from black to white and vice-versa) when clicked. I'm able to turn the entire grid on or off currently, but I'm unable to figure out how to specify which particular square should be toggled when the mouse clicks within its borders. I've created buttons using mouseX and mouseY coordinates before, but they were for specific objects that I could adjust manually. I can't figure out how to do this using for loops and arrays.
I've been told to create a boolean array and pass the value of that array to the grid array, but again, I don't know how to specify which part of the array it needs to go to. For example, how do I change the fill value of square [6][3] upon mousePressed?
Here is my code so far:
int size = 100;
int cols = 8;
int rows = 5;
boolean light = false;
int a;
int b;
void setup() {
size (800, 600);
background (0);
}
void draw() {
}
void mousePressed() {
light = !light;
int[][] box = new int[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
box[i][j] = i;
int a = i*100;
int b = j*100;
if (light == true) {
fill(255);
} else {
fill(0);
}
rect(a, b, 100, 100);
println(i, j);
}
}
}
First of all, you are currently recreating the entire board whenever the mouse is pressed. You must retain that info between mouse clicks, so make box a global array up there with the others. Further, it's sufficient to make it a boolean array if all you care about is the on/off state of each square:
boolean[][] isSquareLight = new boolean[cols][rows];
Instead of
if (light == true) {
you should then just check
if (isSquareLight[i][j] == true) {
(note that the == true is redundant).
Now, you've already written code that finds the coordinates for each box: You're passing it to rect!
rect(a, b, 100, 100);
All that is left to do is check whether the mouse is inside this rect, i.e. whether mouseX is between a and a+100 (and similar for mouseY) - if that's the case, then the user clicked in the box given by the current (i, j), so you can just negate isSquareLight[i][j] (before checking it like above) and it will work.
There are ways to calculate this without looping through the entire grid every time, but maybe the above helps you find the path yourself instead of just getting the code made for you.
PS: The int a; int b; at the top does nothing and can be removed. You are using the local variables a and b in your function, which is correct.

Java, Level Editor Issue

i have ran into a quick problem which i do not know how to do it by myself but i'm sure with your help i will understand more.
I've created a level editor for the game engine i am creating and i am trying to make the mouse position snap to a 32x32(grid) or 64x64(grid) so that the tiles are placed correctly and not overlapping each other.
So that its not like this :
http://imgur.com/Sa3bh0H
but more like this :
http://imgur.com/a/nck9N
Sorry for the really bad explanation.
The mouse position code i am using is
public int getMouseX() {
return mouseX; // Get MouseX
}
public int getMouseY() {
return mouseY; // Get Mouse Y
}
public void mouseMoved(MouseEvent e) {
mouseX = (int)(e.getX() / gc.getScale() /*+ gc.getWindow().getFrame().get*/);
mouseY = (int)(e.getY() / gc.getScale()); //CODE TO GET MOUSE X AND Y
}
//Code Which Loads the texture/image where the mouse is
tMouseX = gc.getInput().getMouseX() -32;
tMouseY = gc.getInput().getMouseY() - 32;
putImage((int)tMouseX,(int)tMouseY,r);
//putImage Function
public void putImage(int x, int y)
{
objects.add(new Grass(x,y));
}
Trying to make the images snap to 32x32
Don't worry about snapping the mouse to the grid, you don't really want to do that. What you want is to snap the tile to the grid.
The best way to do this is with integer division. Divide by your tile size using integers to truncate the remainder (or use floor) and then scale back up by multiplying by your tile size.
int tilepos_x = (int)(mousex / tileSize) * tileSize;
int tilepos_y = (int)(mousey / tileSize) * tileSize;

Java Arrays - Different values for the same object

Using Slick2D I am looping through my buttons and highlighting the one that is currently being hovered over. I do this by saving the index of a button whose onHover event fires. However, when I hover over the first button, it highlights the last. Hovering over the second button highlights the second to last, etc. If I use the keyboard to change the selected button it works perfectly, though.
Here is the method to save the index :
public void onHover(int x, int y) {
Button but;
for (int i = 0; i < b.size(); i++) {
but = b.get(i);
if (but.isClicked(x, y)) {
choice = i;
return;
}
}
}
And the one to compare them :
public void draw(Graphics g) {
Button but;
for (int i = 0; i < b.size(); i++) {
but = b.get(i);
if (i == choice) {
but.drawHighlighted(g);
} else {
but.draw(g);
}
}
}
Is there a special way to do this?
EDIT : I figured out where the problem was.
Mouse.getY() from LWJGL (which i use with onHover method) returns the height of the window minus the Y position of the mouse ; while mouseReleased(button, x, y) from Slick2D (which i use with draw method) returns the "true" Y position.
But both returns the same value with the x position.
I still don't know why the values are different (since Slick2D is based on LWJGL), but to bypass the problem, i use now HEIGHT - Mouse.getY() to have the right position.
Thanks everbody for helping me with that problem !

Swing: In Jtable when cursor move down the JScroll bar also should move down?

I am using JTable when user double click on the row then mouse should foucs on next row. -> ok
At the time scroll bar how to move Jscrollbar & mouse at same poistion
public void TableMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
//if the double click is performed or left button
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() >= 2) {
Robot r;
Point p = e.getPoint();
JTable table = (JTable) e.getSource();
int rowNumber = table.rowAtPoint(p);
PointerInfo a1 = MouseInfo.getPointerInfo();
Point b1 = a1.getLocation();
int x1 = (int) b1.getX();
int y1 = (int) b1.getY();
System.out.println(x1+" :X : "+x1);
System.out.println(y1+" : Y : "+y1);
//Mouse move to next record based row height
r = new Robot();
r.mouseMove(x1, y1 +table.getRowHeight());
//Can any one suggest move the scrollbar based on the cursor move
//Rectangle rect = viewport.getViewRect();
//rect.y = rect.y + table.getRowHeight();
//viewport.scrollRectToVisible(rect); -> IF scrollbar is used to move but not in next poistion
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
set proper ListSelectionModel to JTable
add ListSelectionListener instead of using AWT.Robot, have to test if is any row selected (.... >-1),
have to test if isn't 1st. or last row in the JTables view,
change selection table.setRowSelectionInterval(int index0, int index1)

Categories

Resources