I want to implement this solution Display Modified JSlider Value Above Thumb.
But I do NOT want to use a MouseAdapter to control the Label (position and content). I just want the label to update when the value for the JSlider is modified. In my code I modify the value for the Jslider value in the paintComponent(Graphics g2) method. I dont know if this is the correct place to update the slider value using
slider.setValue (x)
-- Edit:
I tried painting a rectangle but for some reason I cannot understand the position of the rectangle drawn and the slider pointer are not aligned.
if (modified) {
position += 55 - 2;
modified=false;
}
g2.draw3DRect(position, slider.getY(), 150, 25, true);
g2.setColor(Color.WHITE);
g2.fill3DRect(position, slider.getY(), 150, 25, true);
g2.setColor(Color.BLACK);
if(startDate != null)
g2.drawString(startDate.toString(), slider.getX(), slider.getY());
-- Edit:
I modified the code above and it looks like subtracting 2 from modified makes them more aligned
Related
When I see my application, it draws my image not on (0,0) but somewhere middle in my program, So I debug my program,and I saw that the clipRegion.lox is 456 and loy is 130 and this is the location that program draw (0,0). So I think that Clip Region position is wrong, but I don't know how to fix it. Any help please?
code:
public void changeState(int bef,int cur){
if(bef==1){
if(cur==2){
intro.setVisible(false);
this.setContentPane(play);
play.init();
play.setVisible(true);
}
}
}
This is when first my Panel is started. In play.init() I set my socket to connect with server program, and start sound file. That is all.
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(new Color(255,255,255));
g.drawImage(backgroundImg, 0, 0, this);
g.drawImage(player, 368, 280, this);
for(int i=0;i<60;i++){
for(int j=0;j<100;j++){
if(map[i][j]==1){
g.drawImage(stone, (j-10)*32-dx, (i-10)*32, this);
}
else if(map[i][j]==2){
if(i>0&&map[i-1][j]==0) g.drawImage(grass, (j-10)*32-dx, (i-10)*32, this);
else g.drawImage(dirt, (j-10)*32-dx,(i-10)*32,this);
}
}
}
g.drawString(servermessage, 320, 200);
}
I erased my Println code and Thread Sleep Code. It was just for debugging and when I deleted however, nothing changed.
Plus, I repainted it, and it draws in (0,0) but it doesn't draw full screen.
I think that drawn image is the same size as (456,130) to (800,600) so I think the picture is cut out.
I can't post my picture because of low reputation... Any help too?
this.setBackground(new Color(255,255,255));
Don't set the background in the painting method. Set the background in the constructor of your class.
it draws my image not on (0,0) but somewhere middle in my program,
g.drawImage(backgroundImg, 0, 0, this);
No, the image is drawn at (0, 0) which is relative to the location of the panel. This would mean that the panel is not being painted at (0, 0) relative to the frame. This could be because of the layout manager you are using is positioning the panel in the center of the frame. Check your layout code.
I saw that the clipRegion.lox is 456 and loy is 130 and
If you think the clipping area is wrong it may be because your panel does not have a preferred size. Whenever you do custom painting you should override the getPreferredSize() method to return the size of the panel so the layout manager can do their job properly. Maybe the preferred size should be the size of your image?
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
I have a little problem, I need to add a ToolTipText to JPanel. How should I do this?
I want to have a tooltip when I have mouse over the circle.
This is part of my code.
JPanel component1 = new JPanel();
JPanel component11 = new JPanel();
okno.add(component1,"align left,cell 0 0, h 75!, grow,wrap");
component1.setLayout(new MigLayout("","[][grow][grow]", "[grow]"));
component1.add((okno.add(creLab("Kraj", i, czcionka, etykietki))),"left align, cell 0 0");
component1.add(t1,"cell 1 0,grow");
//component1.add(new circle1(),"right align, cell 2 0,h 50!, w 53!, gapleft 50, wrap");
component1.add(component11," right align, cell 2 0, h 30!, gapleft 300, wrap");
component11.setLayout(new MigLayout("","[]","[]"));
component11.add(new circle1(),"cell 0 0,h 50!, w 50!, dock north");
component11.setToolTipText("<html>W polu obok wpisz kraj pochodzenia towaru</html>");
I add also code of circle1:
class circle1 extends Applet{
public void paint(Graphics g){
setForeground(Color.yellow);
g.drawOval(0, 0, 50, 50);
g.fillOval(0, 0, 50, 50);
g.setColor(Color.black);
g.drawString("Jak", 14, 14);
g.drawString("wpisac", 3, 28);
g.setColor(Color.red);
g.drawString("kraj?", 14, 42);
//g.drawString(arg0, arg1, arg2)
}
}
Take a look at JComponent#getToolTipText(MouseEvent)
This will allow you to determine what text to return based on the location of the mouse.
It's difficult to determine for your code snippet, exactly where the circle is been drawen, but I would avoid drawing directly to the surface of the applet, but instead use a custom component (like a JPanel) instead (overriding its paintComponent method). This I would then either add to the applet or to the control panel.
This way your going to avoid issues with the mouse events been consumed
I would also take a look at Ellipse2D, which can be used to determine if the ellipse contains a given point
The first thing is to identify when the mouse is inside the circle. To do that you could verify the mouse position on a mouseMotionlister according to the circle area
http://www.java2s.com/Code/JavaAPI/javax.swing/JPaneladdMouseMotionListenerMouseMotionListenerlis.htm
Once you identify this situation you could proceed to change the tooltip
See Playing With Shapes. You can create a JLabel with a ShapeIcon. Then you just use the setToolTipText() method of the JLabel. You can then add the label to the panel like any other component.
Now that you can use a component to represent a Shape there is no need to do custom painting. Just create a panel add add components to the panel. You can also create JLabels for all your text strings.
Don't do custom painting, unless you have a good reason to do so.
Basically, I'm trying to move a JButton (or even a JLabel) inside a frame using the arrow keys.
I've successfully figured out the part to detect the arrow key presses. Now all i want to do is to change the position of the button (or label) depending on the key pressed.
E.G. when I press the "UP" key, the button/label should move up by say 5 pixels. I couldn't find the property to change the position of the button.
Do I need to put button/label inside the panel or something like that?
Please help, its not a homework, just curious :)
How I would do it is to use the Graphics instead of a Panel. So the bit of code that draws it could be:
public class panel extends JPanel{
public int X = 50; //Starting x
public int Y = 50; //Starting y
public panel(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.drawString("Hello this will be moved", X, Y);//<--- This draws the text
}
}
This bit of code will draw the text and repeatedly draw it. All you have to do is in the Key Listener or what ever was used, do;
X++;
or:
Y++;
That will add 1 everytime. So when you press the key it is moved one x pixel or y pixel or back or forth, depending on the ++ or --. Then add this to the JFrame and it will draw it on screen.
use of a raw image is much better in the application i'm doing but here is something that i accidently found while doing it ;)
This infact can be done by absolute positioning of label(dont know about button) ie. without use of any Layout Manager.
all we have to do is the follwing:
setLayout(null);
Jlabel testLabel = new JLabel("Test Moving label");
now with that done. we have to set
testLabel.setBounds(XCoord+ insets().left, yCoord+ insets().top, 150, 25);
here xCoord and yCoord are the coordinates of the top left pixel of the label. the above line can be added to the actionListener. in my case its the action for pressing UP, RIGHT and LEFT arrow keys.
we can increment and decrement the xCoord and yCoord accordingly.
I'm working on a 3D scene in Java using the Processing API. It's a force-directed graph layout algorithm (although that isn't too important). I've got all the graph drawing done -- the nodes, edges, layout, etc. are looking good. Each node on the graph has a label, though, and I'd like to be able to display said label as text next to the node. I've tried working with the text() function, but so far it seems like my code just doesn't work. I see no text anywhere in the scene.
My code looks like the following:
pushMatrix();
translate(width/2, height/2, 0); // put 0,0,0 at the center of the screen
text("foo!", 20, 20, 20);
popMatrix();
And I don't see anything. Just the graph. So what am I missing?
Everything is fine with the tiny bit of code you displayed. You can see a modified version running here:
void setup() {
size(400,400,P3D);
textSize(20);
}
void draw() {
background(0);
translate(width * .5, height*.5,0);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,-PI,PI));
pushMatrix();
text("foo!", -20, 0, 20);
popMatrix();
}
There might be something else along the way. Care to share more information ?
The fill color controls the text color, and defaults to white. So if you are painting white text on a white background, it won't show up. Add fill(0); before you draw text.
Also remember that shapes drawn after you send your text to the screen may over-write your text. The last 'hidden' statement in draw is to paint the screen.
Here's an example of drawing a series of vertical lines (in 2D) with a row of labels at the top:
int startX;
void setup() {
size(400,400);
textSize(12);
startX = width/10;
}
void draw() {
background(255);
int curX = startX;
fill(0); // Set the text color
while (curX < width)
{
line(curX, 30, curX, height-10);
text(curX, curX-(curX/20), 20);
curX += width/10;
}
} // end draw
I need to draw graphic element (square) dynamically in different positions of Canvas and I need to listen to mouse clicks in order to change place of my square.
How to add mouse listener to Graphics object? Do I have to use another approach?
int x = 0;
int y = 0;
Graphics g = getGraphics(); // get Graphics context
g.setColor(Color.red);
g.fillRect( x - 25, y - 15, 60, 30 );
g.setColor(Color.black);
g.drawRect( x - 25, y - 15, 60, 30 );
g.dispose();
I'd probably use a JPanel as a child element of your larger component that forms the canvas. A JPanel, since it is a subclass of JComponent, allows you both to add a mouse listener via addMouseListener(), and to override its paintComponent() method.
If you want to move the square, just reposition the JPanel.
(for that matter, if it's a square or rectangle, you don't even need to override paintComponent, you could just accomplish this with appropriate calls to setBorder and setBackground.)
Another approach would be to use a JPanel as the entire canvas, override the paintComponent to draw whatever you like, addMouseListener on the JPanel, and then manually determine whether the mouse listener events occur within the geometry of your graphic element.