I have a paint component where I draw some shapes but the problem my shapes exceed jPanel Size . because of that I search a solution of zoom jpanel. I don't know if this possible could someone help me I use net beans gui builder
If you want the shapes to size according to your JPanel, make use of the JPanel's getWidth() and getHeight() e.g.
int x = (int)(getWidth() * 0.1);
int y = (int)(getHeight() * 0.1);
int width = (int)(getWidth() * 0.8);
int height = (int)(getheight() * 0.8):
g.fillRect(x, y, width, height);
See a full running example here
You can see here how to customize initialization code for your JPanel in GUI Builder
Add your JPanel to a JScrollPane and add the JScrollPane to the container you were originally adding your JPanel to. Make sure to setPreferredSize to a size large enough to see all of your drawings whenever you draw a new shape or however you are doing your graphics.
Related
If you run the code below, you will see that there is a red square with a blue line along the bottom and right edges of the square. However, as you can see in the code, the parameters for the rectangle being drawn are the same as the rectangle being filled.
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.drawRect(50, 50, 100, 100);
g.setColor(Color.red);
g.fillRect(50, 50, 100, 100);
}
};
panel.setPreferredSize(new Dimension(200, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
Does anyone know the reasoning behind this behavior? Or is this a bug in Java's codebase?
It seems like drawing the rectangle adds 1 to the width and height of the rectangle you specify. This seems like un-intuitive behavior for one, but in my case it is also causing some undesired effects for a gui I am building.
This behavior is also found when drawing polygons with horizontal/vertical lines. Is there a simple way of getting some consistency in what is painted between the graphics draw and fill functions? It would be great if there was a way for any arbitrary polygon to completely paint over a previously drawn polyline using the same points.
One approach I had thought of was to both draw and fill whenever I want to fill a polygon and then just draw when I want to draw the polygon.
This would actually work in cases of opaque polygons, but in my case I have both opaque and transparent polygons so this approach is not an option.
This is clearly documented in the Graphics API:
drawRect
public void drawRect(int x,
int y,
int width,
int height)
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.
and
fillRect
public abstract void fillRect(int x,
int y,
int width,
int height)
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
Note the difference in the calculation for the right and bottom edges. Although this difference is clearly documented, there is no justification for this difference.
Finally, note that the first line of paintComponent() should be super.paintComponent(g);. This will ensure that the super class has a chance to initialize things, including clearing the region where you will draw.
I want my jframe to open in the center of a person's monitor.
(By default a jframe will open at coordinate. (0,0))
To achieve this, before setting the frame visible, I use this method.
this.setLocation(x,y);
In theory, monitor screen sizes can be different, meaning the center coordinate will be different for almost all computers.
HERES MY QUESTION:
How would I get the center coordinate of the computer monitor running the swing application?
How would I get the center coordinate of the computer monitor running the swing application?
You can get the center coordinate with java.awt.Toolkit:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.width/2;
int centerY = screenSize.height/2;
However, you don't need it.
Just use:
yourFrame.setLocationRelativeTo(null);
And it will be automatically centered
Try it
//call `setSize` first
this.setSize(300, 600);
Toolkit tk = this.getToolkit();
Dimension dim = tk.getScreenSize();
int x = (int) dim.getWidth() / 2 - this.getWidth() / 2;
int y = (int) dim.getHeight() / 2 - this.getHeight() / 2;
this.setLocation(x, y);
Here this represents JFrame class object.
JFrame will display in the center of the screen.
Query the Toolkit object.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.getWidth() / 2;
int centerY = screenSize.getHeight() / 2;
Swing's documentation states that the window will be centered on screen if setRelativeTo is passed null.
public void setLocationRelativeTo(Component c)
As stated in their docs:
"If the component is null, or the GraphicsConfiguration associated with
this component is null, the window is placed in the center of the screen. The center point can be obtained with the GraphicsEnvironment.getCenterPoint method."
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo(java.awt.Component)
So simply do:
frame.setLocationRelativeTo(null);
..since your question was really how to get it to open in the center, not how to get the center coordinate.
When drawing on my extended JPanel I want the size to be variable. I've worked out the code to always outline a 1 pixel margin. My code works but I don't understand why I must adjust the height and width and height of the dimension by -1 when drawing the rectangle.
If I draw a rectangle that has 1,1 as a size it draws a single pixel square so shouldn't drawing the width and the height work without the -1 modifier.
If any one can explain why there is a discrepancy between the size of my extended JPanel or explain a better way to get the exact dimensions of the drawing area I would appreciate it.
public class Engine extends JPanel {
Engine(){
}
#Override
public void paintComponent(Graphics g){
Dimension a = this.getSize();
g.setColor(Color.RED);
g.drawRect(0, 0, a.width-1, a.height-1);
}
}
Remember, most values are 0 based. But instead of having to say, " I want the width of my panel to be 199" so you get a panel that is 200 pixels wide (0-199), Swing allows you to specify 1 based values and makes adjustments internally
If you create a rectangle of 1, 1, 1, 1, your actually creating a rectangle at position 1x1, whose width & height is 1 pixel (1 + 1 would be 2)
"or explain a better way to get the exact dimensions of the drawing area I would appreciate it."
Override getPreferredSize() in your JPanel class
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
When you you paint, make use of getWidth() and getHeight(), inherited from the JPanel class. Using these methods, your painting will resize dynamically with the resizing of the panel.
int width = getWidth();
int height = getHeight();
g.fillRect((int)(width * 0.9), (int)(height * 0.9),
(int)(width * 0.8), (int)(height * 0.8));
Side Note
You should call super.paintComponent inside the paintComponent method so as not to break the paint chain.
paintComponent should be protected not public
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
I've worked out the code to always outline a 1 pixel margin.
Don't do custom painting. Use a Swing Border. See How to Use Borders. You probably want a LineBorder.
Is there anything obvious wrong with this line of code? I want rectangle to stay centered regardless the size of the window. But this don´t work for some reason, the rectangle stays the same place.
public void run() {
setSize(800, 800);
createEntireFigure();
}
private void createEntireFigure(){
int centerOfWindowWidth = getWidth() / 2;
int centerOfWindowHeight = getHeight() / 2;
GRectWithGLabel ("A String",centerOfWindowWidth, centerOfWindowHeight);
}
Your rectangle size code is only called on rectangle creation, and so it makes sense that the rectangle's position will not change if the GUI is re-sized. You need to somehow listen for size changes in your GUI and call code to re-position the rectangle then for this to work. What graphics library are you using?
Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work :
Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame
jDesktopPane1.add(ex);
ex.setLocationRelativeTo(this);
ex.setVisible(true);
So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane.
Thank you
Try something like :
JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();
public void centerJIF(JInternalFrame jif) {
Dimension desktopSize = mainPanel.getSize();
Dimension jInternalFrameSize = jif.getSize();
int width = (desktopSize.width - jInternalFrameSize.width) / 2;
int height = (desktopSize.height - jInternalFrameSize.height) / 2;
jif.setLocation(width, height);
jif.setVisible(true);
}
And
centerJIF(jif_test);
A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window, but you can use a similar approach. Just subtract half the width and height of the internal frame from the corresponding center coordinates of the desktop pane. Use the new coordinates in your call to setLocation().
You'll also want to adjust the internal frame's dimensions if necessary.