Can someone show me code that will set up a JSlider except, instead of a thumb, it is a circle? Specifically, instead of that little triangle which points to the current position, it is a blue circle of width 100;
Assuming you're using the Metal L&F you could extend javax.swing.plaf.metal.MetalSliderUI and override the horizThumbIcon or vertThumbIcon to be your circle. Then set this UI to be UI you'd like for your sider using slider.setUI()
look at this the answer here which performs a change for a JComboBox How can I change the arrow style in a JComboBox.
Related
I'm trying to figure out if there's any way to draw images (drawRect(), drawOval(), etc.) based on if/else statements or by using an ActionListener.
I don't want to post the complete problem because this is for an assignment, but for my own scenario:
If I have a button on a JPanel named "draw rectangle" and I have the x, y, width, and height from user input, is there any way I can attach an actionListener to "draw rectangle" that could somehow draw the rectangle using those values (passed by reference?).
I know I can use paintComponent, but I can't put that into the ActionListener and it seems to do things of its own accord and not based on a specific user's actions.
I don't really have any code for this because I can't figure out how to do it at all.
If I have a button on a
JPanel named "draw rectangle" and I have the
x, y, width, and height from user input, is there any way I can attach
an actionListener to "draw rectangle" that could somehow draw the
rectangle using those values (passed by reference?).
The short answer is yes.
Generally, you'll need to save the rectangle instructions in a model, and have the JPanel redraw the model when any of the shape buttons are pressed.
Let's take your rectangle example. What do you need to know to draw a rectangle on a JPanel? You need a starting point (upper left) and an ending point (lower right). You can use a java.awt.Point to hold the starting and ending point. You can set the thickness of the line, in pixels. You can set the color of the rectangle, using a JColorChooser. You can also set the rectangle to be an outline or filled with the chosen color.
That description will be similar for a line and a triangle. A circle is a little different, with a center point and a radius. As you can see, we already have a lot of information to keep track of just with these simple geometric shapes.
Then there's the issue of the drawing surface itself. It's possible to have a drawing surface larger than your computer screen can show. You can put the drawing surface inside of a JScrollPane.
All of these things must be determined so you can build the model of your application. You do this before you build the view using Swing components and the controller using action listeners.
I made a sketch with the behavior I'm trying to get.
Sketch
So I want to display a rectangle in the middle of the screen and a triangle that covers part of the rectangle. The problem is, when I use a Stackpanethe rectangle is centered but the triangle as well and I can't move it to the bottom right position. When I use a Groupit is not centered. Is there any way to get my intended behavior?
Set alignment of your triangle. Use static setAlignment(Node child, Pos value) method:
StackPane.setAlignment(triangle, Pos.BOTTOM_RIGHT)
I'm using LiquidFun to simulate water, it's a physics engine based on box2d that uses particles. My problem is when rendering the particles with a specific color.
what is the purpose of setting the particle color on it's particle definition? when you also have to set the color on which the particle is to be rendered on the ParticleDebugRenderer.
public void createWater(float x, float y){
ParticleDef def = new ParticleDef();
def.color.set(Color.Red); //set particle color
def.flags.add(ParticleDef.ParticleType.b2_tensileParticle);
def.flags.add(ParticleDef.ParticleType.b2_colorMixingParticle);
def.position.set(x, y);
int index = system.createParticle(def);
}
ParticleDebugRenderer:
pdr = new ParticleDebugRenderer(Color.BLUE, maxParticles); //set as BLUE
if I set the particle to be RED it would still be rendered in blue because the ParticleDebugRenderer is set to BLUE.
Looking at the source code we can find 2 renderers.
ParticleDebugRenderer.java and ColorParticleRenderer.java
The code difference between them is that ColorParticleRenderer gets color from ParticleSystem and ParticleDebugRenderer gets color from constuctor.
The main use difference is that we use ColorParticleRenderer everytime we are not debugging. ParticleDebugRenderer is the one to use when we want to debug a particle. We use it, because we don't want to make changes in colors at the definition of ParticleSystem, because
There may be several ParticleSystem of one definition, so changing color in definition would be pointless.
It is easier to change one line of drawing than 1 line of definition (you avoid saying: ohh I forgot that I change the color at the definition)
Your confusion comes from fact that you are using ParticleDebugRenderer when you are not debugging so you assign the same color twice.
Been building up a little game in java, already asked a few questions about the usage of JPanel in it, and used them to paint the graphics and the main part of the screens.
Now i have a little doubt about other components. My intention is to add on the corner a pair of bars to show health and mana of an entity (like in a rpg game), and wondered which was the best approach for it.
Thought about making a new JPnale with a pair of JProgressBar to set the ammount of it, but then i wondered if it would be better to paint it completely and fill a pair of rectangles.
I mean, doing a pair of new JProgressBar() for it, or a pair of g.fillRect() and then paint the ammounts.
I guess that easiest is to set the JProgress, as i can set values and text if i want, maybe, but not sure about it and if it would run smoother without overwhelming it with JComponents.
Also, if want to add buttons would be better the JButton, or paint a rectangle and check for containment of the mouse pointer with an event (I have this approach at some points where there is not KeyBinding). Should i change that?
Thank you beforehand :)
I'm writing an answer because i haven't got enough reputation to comment :(
I think is better if you paint it in your graphics engine. Use Rectangles is of course a better idea than use a new JPanel with JProgressBar.
But i think that it's even better if you use Images to build your own Progress Bar.
You can create them or find them on internet.
For example you can take an Image for the Progress Bar Background, and another Image for the Foreground (the part that will fill the Bar). Then you can set their X and Y position and then just change the Foreground Width in relation with the entity health to fill or empty the Progress Bar.
Is there a way to simply check if a mouse click is ON the Shape's border?
I'm actually using the contains method but it does not work if the click is made on the Shape border.
Use BasicStroke. Define thickness (let's say 5 pixels) and use yourShape.getStrokedShape().contains(yourPoint)