Java jLabel drag and drop from netbeans "events" context menu - java

I'm trying to drag a jLabel around the screen following the mouse pointer. As I am no expert on GUI programming I'm trying to do it using Netbeans' GUI design facilities.
I click on "events-mousemotion-mousedragged" and then insert the following code:
private void jLabel2MouseDragged(java.awt.event.MouseEvent evt) {
int x=evt.getX();
int y=evt.getY();
jLabel2.setLocation(x, y);
jLabel2.repaint(); }
I don't expect this simple piece of code to work marvels, the issue however is that it behaves in an erratic manner, the jLabel pops up and flickers almost everywhere within its container.
If it's of any help, layout is set to absolute.
Thanks.

As MadProgrammer pointed out, that was the issue. This is my mouse drag method:
private void jLabel2MouseDragged(java.awt.event.MouseEvent evt) {
Point p = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), getContentPane());
int x=p.x;
int y=p.y;
jLabel2.setLocation(x-120, y-120);
jLabel2.repaint();
}
The jLabel now moves smoothly.
My label is roughly 240x240 pixels, so I corrected the coordinates to have the center of the label placed where the mouse pointer is.
Thanks!

Related

Cant draw a shape to a specific panel in a NetBeans GUI

I'm trying to make a simple application to draw shapes to a panel in a jFrame. The GUI is a NetBeans generated jFrame. The application has three panels. the first two hold button groups to select the shape and a color. There's a button to draw the shape to the third panel once the selections are made.
What the GUI looks like
Unfortunately I'm having no love and can't make it work. For now I just want to get the button to draw a shape then I'll add the button functionality. Here's the code I have so far.
private void btnDrawShapeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Draw shape = new Draw();
pnlDrawPad.add(shape); //pnlDrawPad is the name of the jPanel
shape.drawing();
}
import javax.swing.*;
import java.awt.*;
public class Draw extends JPanel{
public Draw(){
super();
}
public void drawing(){
repaint();
}
//#Override <-- gives error "method does not override or implement a method from a supertype"
public void paintCompnent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.drawString("Hello World!",40,30);
}
}
When I click the "Draw Shape" button nothing happens. I've been searching for a couple days now and am not finding the answer. Actually, its making me more confused as to what to do.
How to deal with this?
Don't keep creating new panels for each shape.
Instead you need a single panel with an ArrayList of objects you want to paint. Then you customize the paintComponent(...) method of the panel to iterate through all the objects in the panel and paint them.
So you button will just add another object to the ArrayList and then invoke repaint() on the panel.
Check out Custom Painting Approaches. The DrawOnComponent example shows how to paint a Rectangle objects from an ArrayList to get you started.
I found that with NetBeans you don't need to Override the paintComponents() method. Since all the GUI stuff is done when you design it, you merely need to add the drawings to the panel or what ever you need to do. I found you that by declaring and instantiating a 2D graphics object then drawing it to the place you want.
private void btnDrawShapeActionPerformed(java.awt.event.ActionEvent evt) {
int centerX;
int centerY;
Graphics2D drawFx = (Graphics2D)pnlDrawPad.getGraphics();
drawFx.setColor(Color.ORANGE);
centerX = (int)(458*Math.random());
centerY = (int)(440*Math.random());
drawFx.fillOval(centerX-50, centerY-50, 100, 100);
drawFx.dispose();
}
Worked like a charm so now every time I push the draw shape button it draws a shape to the panel in a different location every time. I've since put in the radio button functionality to change shape color and fill and added a button to clear the screen.
Not sure if this is the ideal way to do it but for this stupid simple little program it works well enough and I'm done stressing over it.

Custom JFrame movement "slipping"

Recently, I have been trying to make my gui's look good and part of it is implementing my own titlebar, maximize button, minimize button, etc.
I am having issues on the dragging, I want to be able to drag the JFrame by clicking anywhere in it.
I attempted to create a mousemotionlistener to handle it however it slips when I use it. As in I attempt to drag the window to a location however it appears to be skipping calls to mouseDragged and attempting to call mouseMoved instead.
Here is my code
public class MouseWindowDragManager implements MouseMotionListener{
private JFrame frame;
int prevPosX = -1;
int prevPosY = -1;
public MouseWindowDragManager(JFrame frame){
this.frame = frame;
}
#Override
public void mouseDragged(MouseEvent e) {
int newX = (frame.getLocation().x + (e.getXOnScreen() - prevPosX));
int newY = (frame.getLocation().y + (e.getYOnScreen() - prevPosY));
frame.setLocation(newX, newY);
prevPosX = e.getXOnScreen();
prevPosY = e.getYOnScreen();
}
#Override
public void mouseMoved(MouseEvent e) {
prevPosX = e.getXOnScreen();
prevPosY = e.getProperty("apple.awt.draggableWindowBackground", true);tYOnScreen();
}
}
Recently, I have been trying to make my gui's look good and part of it is implementing my own titlebar, maximize button, minimize button, etc.
Gee, Microsoft and Apple spend millions of dollars to develop GUI's that users can use. They will be disappointed to hear this :)
I want to be able to drag the JFrame by clicking anywhere in it.
Check out Moving Windows for a class that allows you to drag a window around. The example code shows how to use the class by just dragging on your custom title bar. However, if you really want to be able to drag the frame by clicking anywhere then you can register the frame's root pane with the ComponentMover.

JavaFX ScrollPane - speed up swiping effect on touch device

I found that if I swipe within a ScrollPane in a JavaFX application on a touch device, the scroll-animation is quite slow. I do not swipe over the ScrollBar of the ScrollPane but within the content itself.
How can I speed up the swipe animation?
This is a known problem with touch devices and the ScrollPane ;)
If you want to speedup the animation you have to set the position by your own.
I solved the same problem I while ago on this way, worked well.
You need to do something like this:
ScrollPane scrollPane = new ScrollPane();
final IntegerProperty vValueProperty = new SimpleIntegerProperty(0);
final int steps = 5;
scrollPane.vvalueProperty().bind(vValueProperty);
scrollPane.setOnSwipeDown(new EventHandler<GestureEvent>() {
public void handle(GestureEvent event) {
// calculate the needed value here
vValueProperty.set(vValueProperty.get() + steps);
}
});
If the normal swipe animation disturbes you, just kill the event with
event.consume();
But it could be that you need to use an EventFilter in this case cause the EventFilter is called before the Event is doing anything. Can't remember if a consume() at the end of the EventHandler is enough. If not you need to use something like this:
scrollPane.addEventFilter(SwipeEvent.SWIPE_DOWN,
new EventHandler<SwipeEvent>() {
public void handle(SwipeEvent event) {
vValueProperty.set(vValueProperty.get() - steps);
event.consume();
}
});
Well this is just to speedup the animation. If you want your Pane following your finger, just save the first value, which you get OnTouchDetected and calculate the Offset OnTouchMoved. This is your value you can use to translate the Pane correctly. :)
Hope it is useful. I don't have a touch device here to test it.

Stack GUI in java swing

I am creating a GUI of stack. Here is my code
private void StackActionPerformed(java.awt.event.ActionEvent evt) {
b1=new JButton("Push");
b2=new JButton("Pop");
b3=new JButton("Peek");
b4=new JButton("Clear");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pushButtonActionPerformed(evt);
}
});
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
frame_st.add(tb);
revalidate();
repaint();
frame_st.setSize(1022, 534);
drawstack();
}
public void drawstack()
{
m1.setBorder(BorderFactory.createRaisedSoftBevelBorder());
m1.setBackground(Color.white);
m1.setLayout(null);
JLabel l1=new JLabel("STACK");
l1.setBounds(500, 5, 100, 70);
m1.add(l1);
}
My code is compiling prefectly drawstack() is drawing the things which is required but as the drawstack() is called my buttons which are present in StackActionPerformed(java.awt.event.ActionEvent evt) i.e b1,b2,etc becomes invisible. I don't want this to happen . I want my buttons visible all the time. please help .
m1 is a panel which covers my entire frame.
I'm guessing you're putting m1 on top of your buttons (and frame_st). I'd put the buttons on m1 directly. Windows like this are Container objects. They have (sub)containers placed on them, with (sub)(sub)containers placed on them, etc.. You need to keep straight how all the containers at a particular level are laid out on their parent. If they overlay rather than abut, you have a problem.
I've found javax.swing.Box the most useful, and simplest, class for laying out windows, and I'd recommend it to you. Put one vertical Box on your whole window. Then make frame_st a horizontal Box, add it to the vertical one, and add your buttons just as you are doing. Then add m1, whatever it is, to the vertical Box also, and you should be good to go.
javax.swing.Box is not the solution to all the world's problems, but it should get you started. Once your buttons are showing, you can try other stuff.

How to create an image map using Java Swing?

I need to make an image map using Swing that displays a background image, and then when the mouse hovers over (or clicks) specific hotspots, I need to pop up a 'zoomed-in' image and have it display.
I was thinking of extending JPanel to include an image reference and have that drawn thru the paintComponent(g) method. This part I have done so far, and here's the code:
public class ImagePanel extends JPanel
{
private static final long serialVersionUID = 1L;
private Image image;
public ImagePanel(Image image)
{
setImage(image);
}
public void setImage(Image newImage)
{
image = newImage;
}
#Override
public void paintComponent(Graphics g)
{
Dimension size = getSize();
g.drawImage(image, 0, 0, size.width, size.height, this);
}
Could anyone recommend how I might listen for / respond to mouse clicks over defined hot-spots? Could someone additionally recommend a method for displaying the pop-ups? My gut reaction was to extend JPopupMenu to have it display an image, similar to the above code.
Thanks for any help!
To listen to the mouse clicks implement the MouseListener interface, and add it to your panel. Then when the click is recieved you can use a JPopupMenu as you suggested, or you could even use a glass pane to show the zoomed in image.
I'm guessing you want to achieve something similar to this post by Joshua Marinacci, he has also posted the source here, I would take a look at that.
I would probably:
create some instance of Shape that represents each of your hotspots (could be a plain boring old Rectangle, or see GeneralPath if you need to create fancy shapes)
register a MouseListener which iterates through each of the Shapes and calls its contains() method to see if the clicked coordinate is inside the hotspot in question

Categories