java - Selecting Text within Graphics / Graphics2D -


selecting text using graphics/graphics2d

how select text string using mouse when it's rendering in graphics?

hey, i'm developing ui controls software i'm making in java. i'm rendering using graphics/graphics2d , need little assistance problem i've come across. have textbox control set can type, , backspace text. next problem have being able select portions of text. i'm not sure should start on matter. low post @ controls render code , it's typing code.

render code

@override public void render(graphics g) {      // draw fill     graphics2d g2 = (graphics2d) g.create();     if (isactive) {         g2.setpaint(new gradientpaint(new point(x, y), colors.textboxactivetop, new point(x, y + h), colors.textboxactivebottom));     } else {         g2.setpaint(new gradientpaint(new point(x, y), colors.textboxtop, new point(x, y + h), colors.textboxbottom));     }     g2.fillrect(x, y, w, h);      // draw text     g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);     if (ispassword) {         int count = text.tostring().length();         (int = 0; < text.tostring().length(); i++) {             g2.setcolor(colors.white50percent);             g2.fillarc(x + (w / 2) + (i * 5) - ((count * 5) / 2), y + (h / 2), 4, 4, 0, 360);             g2.setcolor(forecolor);             g2.fillarc(x + (w / 2) + (i * 5) - ((count * 5) / 2), y + (h / 2) - 1, 4, 4, 0, 360);         }     } else {         if (iscentered) {             g2.setcolor(colors.white50percent);             g2.drawstring(text.tostring(), x + (w / 2) - (g.getfontmetrics().stringwidth(text.tostring()) / 2), y + (h / 2) + (g.getfontmetrics().getmaxascent() / 2) + 1);             g2.setcolor(forecolor);             g2.drawstring(text.tostring(), x + (w / 2) - (g.getfontmetrics().stringwidth(text.tostring()) / 2), y + (h / 2) + (g.getfontmetrics().getmaxascent() / 2));         } else {             g2.setcolor(colors.white50percent);             g2.drawstring(text.tostring(), x + 5, y + (h / 2) + (g.getfontmetrics().getmaxascent() / 2) + 1);             g2.setcolor(forecolor);             g2.drawstring(text.tostring(), x + 5, y + (h / 2) + (g.getfontmetrics().getmaxascent() / 2));         }     }     // draw border     g.setcolor(colors.bordercolor);     g.drawrect(x, y, w, h);      // draw hightlights     g.setcolor(colors.white50percent);     g.drawrect(x + 1, y + 1, w - 2, h - 2);     g.drawrect(x - 1, y - 1, w + 2, h + 2); } 

typing code

@override public void keytyped(keyevent e) {     int code = (int) e.getkeychar();     if (isactive) {         if (code == 8) {             if (text.tostring().length() >= 1) {                 text = text.tostring().substring(0, text.tostring().length() - 1);             }         } else {             string character = "" + (char)code;             if(acceptedcharacters.contains(character.tolowercase()))                 text = text.tostring() + (char) code;         }     } } 

how select text string using mouse when it's rendering in graphics?

add mouselistener , when fires, check if within bounds of string.

ways check bounds of string:


Comments