How can I detect the scrollbar is dragged/clicked in a Jscrollpane? - java

I have a Jscrollpane with a Jpanel inside. In this Jpanel I draw some shapes. The problem I have is when I scroll up and down. The drawing on the Jpanel appears cut. So I want to redraw the Jpanel every time I drag the scrollbar or click the direction arrows. How can I do it?
(Excuse me for not posting a SCCE, I've generated the interface with netbeans and it's quite complicated)

In this Jpanel I draw some shapes. The problem I have is when I scroll
up and down. The drawing on the Jpanel appears cut. So I want to
redraw the Jpanel every time I drag the scrollbar or click the
direction arrows.
good idea in the case that you repainting concrete Rectangle, Dimension for this Rectangle returns JViewport
I have a Jscrollpane with a Jpanel inside. In this Jpanel I draw some
shapes. The problem I have is when I scroll up and down.
use AdjustmentListener added to JScrollBar (derived directly from JScrollPane or as local variable)

Related

How to scale JPanel to fit JFrame

Currently, I have a full screen JFrame. Within that JFrame, is a panel called MainPanel which is 1280 by 640, a portion of the full screen game. The MainPanel is where my game is rendered to. Is there any way to scale this MainPanel to fit JFrame without having to adjust all the component sizes of sprites and such? I'm thinking of rendering MainPanel to an image and drawing it over JFrame, but I do not know how to go about this.
Thanks.
By default, the layout manager of a JFrame is BorderLayout, and adding a JPanel to it without any arguments will place it in CENTER, which will automatically scale to the size of the JFrame. It will ignore any size you have given the JPanel.

Scroll doesn't work in JScrollPane

I have a JPanel inside a JScrollPane, where I draw some shapes with Graphics.
The problem is when I draw outside the bounds of the panel, the scroll in the scroll pane doesn't work. I have autoresize activated on the JPanel.
Any tip?
The custom painted JPanel should return a preferred size appropriate for the graphic elements it contains.

Make Graphics from JPanel visible outside JPanel

I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
This my paint method:
public void paintComponent (Graphics g) {
g.setColor(Color.red);
g.fillRect(dist, 0, 10, 10);
dist++;
}
Update:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
Is there a better solution for this problem?
You state:
I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
If the JPanel is drawing it, the short answer is: "you can't".
The longer answer will depend on just where you're trying to draw the JPanel and how the rest of your GUI is set up.
Edit
You now state:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
If I coded the way you were doing it, I wouldn't have these local JPanels draw the car but rather would have the car be its own sprite that exists on a different layer from the streets, perhaps using a JLayeredPane. It could exist in its own JPanel that encompasses your entire map, as long as this JPanel is not opaque. Then you could move the car any which way you'd like.
As said before you can't but if you want it to occupy and larger area you should either make the JPanel bigger or put the paintComponent in the parent component.

Oversize JPanel in WindowBuilder

I am designing a large form with the Eclipse WindowBuilder plugin. The class extends JPanel and uses GridBagLayout as its LayoutManager.
Now my panel has become vertically larger than my screen size and I cannot make the JPanel any larger by dragging its borders.
How can I vertically extend the JPanel so it's still usabe with WindowBuilder?
Now my panel has become vertically larger than my screen size and I
cannot make the JPanel any larger by dragging its borders.
How can I vertically extend the JPanel so it's still usabe with
WindowBuilder?
put JPanel to the JScrollPane

Drawing 2Dcircle in an Jinternal frame

I have a JInternal Frame and I want to draw a circle(using 2Dgraphics) in it and make it flexible. I mean when I change the size of frame the circle become smaller or in making frame larger circle also become larger. Can somebody help?
You would draw in the paintComponent method of a JPanel or JComponent that is held in the JInternalFrame's contentPane, same as you would draw in any other JPanel. I'd get the dimensions of the JPanel at the start of the paintComponent method and use those values to tell how big to draw the circle.
Also, if you add the JPanel directly to the JInternalFrame's contentPane, it will be added by default BorderLayout.CENTER, and so when the JInternalFrame changes size, the JPanel also changes size, it's paintComponent will be called by the JVM, and the new drawing will be resized automatically.
add a WindowListener to the jInteralFrame and redraw whenever the size changes

Categories