What is the best way to implement custom sliders (for volume), buttons normal, with images (icons) and with text, text boxes and other stuff with java.awt.Graphics?
Thanks,
Kerdo
This is easy.
First you have to capture the click and move events:
addMouseListener(new MouseListener() {
@Override void mouseClicked(MouseEvent e) {
mouse(e)
}
...
And you travel through all the button
s, slide
s, etc.. in the mouse
method, to pass the location of the mouse to them:
public void mouse(MouseEvent e) {
allComponents.forEach(a -> a.hey(e.getX(), e.getY()));
}
And then you update the states in the hey
method. For example, if it's a button, you see if the location of the mouse is inside the button. If so, you set your button
's color to a darker one (just for example).
Then draw all the game objects to the screen. Override the paintComponent
method, and invoke the methods of the parameter Graphics g
, like drawString
, drawRect
, etc..
The approach described above is used in one of my personal project.
g.fillRect(0, 0, width, height)
before every refresh
\$\endgroup\$
for (A a : as) { if (!a.x()) continue; a.y(); }
can be replaced with for (A a : as) { if (a.x()) a.y(); }
:D
\$\endgroup\$