I have created a jframe in java with following code . this is creating the custom icon of the image passed in the given url . but its size is very small can i change the shape of it from rectangular to circular pls help
class newframe extends JFrame
{
Container cp;
newframe()
{
cp=this.getContentPane();
cp.setLayout(null);
}
public static void main(String args[])
{
newframe frm= new newframe();
frm.setbounds(0,0,1000,800);
frm.setVisible(true);
ImageIcon im1= new ImageIcon("path upto image");
frm.setIconImage(im1.getImage());
}
}
you can't change the size of Icon for JFrame, because properties of Top-Level Container came from Native OS, AFAIK and because
Icon can
a) fill all available area
b) smaller than available area
c) only part of Icon is visible, because is larger than available area
size of Icon is platform sensitive (WinXP == 16 x 16, Win7 depends of current theme 16 x 16 or 32 x 32 )
there are two Custom Look and Feels that implementing own injections to the properties for Top-Level Container came from Native OS, one of them is Substance L&F, 2nd isn't important to mentioned, because touched shadowed area, and required to remove toolbar and returns modified back (dirty hacks)
for JFrame based on JSR296 in Netbeans isn't possible to change Icon for Top-Level Containers, because this Swing Framework to override and protected some important methods, have to override RootPane (please don't do that, required tons of hacks and result will be only Borders (transparent window without contens) without ContentPane, no way back)
ONLY BY WINDOWS 7!
When you set the Width on 32x32 pixels, the icon of the JFrame Window in the the superbar is bigger. It bust be this Size, because when it isn't 32x32 it shows only 16x16 pixels.
Related
I have two simple JDialog dialogs that should be small. The first has a JTextField of 5 columns and a vertical slider under it. The second has a JLabel, a JSpinner and two JButton(s). Both, after pack() and setVisible(), are too wide. For example, the first dialog has a preferred size of about 80, but shows up with width of 258.
Here are a few things that do not work: 1) a custom layout with the right preferred size computations vs. GridBagLayout; 2) overriding getPreferredSize(); 3) setSize and setBounds before or after setVisible; 4) a custom root pane UI with a custom title pane; 5) a forced constant width set on componentResized; 6) removing the slider (in case there is confusion between vertical and horizontal sliders width and height); 7) removing all controls and the dialog title.
In general, the dialogs can be resized by the user to have smaller width (by dragging the corners), but not programmatically (they can be resized programmatically with setBounds to have smaller height).
The dialogs do have the right sizes under some commercial look and feels, but not under Metal or Nimbus. Under both Metal or Nimbus, the preferred and minimum sizes of the dialog, root panes, glass panes, layered panes make sense. The size of the dialog itself doesn't.
I have tested this without setting a look and feel (which, on Windows, presumably means Metal) and it does not work.
I know that the width of 258 is set in addNotify in Dialog, on getComponentFactory().createDialog(this).
I assumed this could be related to the title portion of the dialog, but the icons there are not of some significant size.
Any ideas are appreciated.
My next move will be to create a simple standalone JDialog or a JFrame that calls a JDialog, outside of the main application with which I am working now. However, I cannot see anything special happening in the larger application.
Here is the code that will produce a dialog with the same width (looks like):
public class Main
{
public static void main(String[] args)
{
JDialog dlg = new JDialog();
dlg.pack();
dlg.setVisible(true);
}
}
I have code that generates a JPanel that needs to run on both Mac OSX and Windows environments. I would like to draw a series of rectangles that frame the JPanel's display area. However the display area of the JPanels differs in the two environments. In the diagram below, the Mac JPanel is on the left and a Windows JPanel is on the right.
Is there a property that makes reference to the display area of a JPanel as opposed to its size?
The two JPanels in the diagram were made with the same code. In both cases I have the following command:
setSize(400, 400);
And in the override of paintComponent, the outermost rectangle (cut off in both in the vertical but only for Windows in the horizontal) is drawn as follows:
g2.drawRect(0, 0, 399, 399);
(The inner rectangles are displaced by an increment of 4 pixels and alternate in color. This isn't exactly what I hope to draw, but it illustrates the problem.)
I could manually determing the adjustments needed, and create separate constants for the two environments, but I'm wondering if there is something I've missed in Swing where it is possible to obtain the display size of a given JPanel. If such a function or property exists, making use of it would be cleaner and more flexible, as well as hedging against JPanels being given different title-bar sizes or padding settings.
In the paintComponent() method of your panel you use:
int width = getWidth();
int height = getHeight();
This will give you the current size of the panel whenever the component is repainted.
I need a way to manually control/stretch the size of my whole GUI.
I have a 1400 x 1050 Desktop (native resolution) and I want to scale the resolution manually to 1024 x 1050 inside the code, because my application was written on/for a 1024 x 768 Desktop. So in general the whole frame and all buttons etc should be stretched / bigger as a whole and the relation should stay the same.
I can´t do it via Windows properties because the resolution in general needs to be 1400 x 1050 because of another application.
My approach was something like:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenHeight = screenSize.height;
screenWidth = screenSize.width;
..and then change the screen size via setSize()? But I don´t understand how.
Java Tookit Screen Resolution - How can I use it?
..unfortunately the link in the answer here does not work anymore.
How to set resolution manually in Java Swing?
As mentioned in the comments, the best approach here is to fix the underlying code to not mandate a 1024 x 768 display. This is usually a very distinct code smell of front-end Java code. Proper use of Layout Managers can almost always get your display to function in a more flexible manner.
However, in industry, sometimes refactoring to get legacy components functioning properly is not a feasible effort. In such a case, I would propose that you treat the inflexible GUI you can't refactor as a component of a larger GUI that you can control.
Consider the following example code:
public static void main(String[] args) {
// Initialize the frame
JFrame myApp = new JFrame("App");
myApp.setSize(1400, 1050);
// Create container for the GUI
JPanel container = new JPanel(new BorderLayout());
container.setPreferredSize(new Dimension(1024, 768));
// Load the GUI into the container
JComponent myGui = new JPanel(); // Replace this with actual GUI.
myGui.setBackground(Color.RED); // Remove this once using actual GUI.
container.add(myGui, BorderLayout.CENTER);
// Create the frame's content pane
JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER));
content.setBackground(Color.BLUE); // Also remove this in production.
// Add GUI to content pane
content.add(container);
// Add content pane to frame, show frame
myApp.setContentPane(content);
myApp.setVisible(true);
}
This works because we've added the GUI to the CENTER of a BorderLayout panel (which will stretch the GUI to occupy the entire size of the panel in the absence of any components on the NORTH/SOUTH/EAST/WEST). We set the preferred size of that BorderLayout panel to be 1024 x 768 (IE: the size that the GUI is specified to work for), and then feed that panel into a FlowLayout panel (which will preserve the preferred size).
The result is that our 1400 x 1050 application contains a 1024 x 768 rendering of our GUI component (you can easily change this to 1024 x 1050 by modifying the preferred size of the panel containing the BorderLayout).
As an exercise to the user, you'll notice that the GUI code isn't centered vertically if you run this. This can be tackled by modifying the layout of the content panel.
This question already has an answer here:
Creating a space for Graphics2D drawings
(1 answer)
Closed 8 years ago.
I am trying to make a simple game in Java. I set a size of my frame in this way:
getContentPane().setPreferredSize(new Dimension(1280, 720));
And then I added a JComponent object, which draws whole graphics in my game:
getContentPane().add(gameGraphics);
And it all seemed to work properly, till I tried to prevent game objects from going out-of-map. "Game engine" teleports every object that went below 0 or above 720 in height or 1280 in width. I am sure that algorithm works perfectly (I checked it very precisely) and just look what happens when I try to reach lower-right corner of the window (while upper-left works properly!):
(A player is that blue rectangle in a corner)
There is a 10-pixel line of free space on the right and down of the window. So a content of the window must be bigger than 1280x720 pixels.
Do you have any ideas what is wrong here? Thanks.
There is a whole JFrame code:
MainFrame() {
super("GameOne");
setLayout(new BorderLayout());
getContentPane().setPreferredSize(new Dimension(RES_WIDTH, RES_HEIGHT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameGraphics = new GameGraphics();
getContentPane().add(gameGraphics);
new Player();
new TestShip(10, 10);
pack();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
GameGraphics as I said is a class that extends JComponent and everything which can be seen in the game is generated in a paintComponent method of this class.
RES_WIDTH and RES_HEIGHT are constants that are equal to 1280 and 720 respectively.
PROBLEM SOLVED:
A solution is to override a JComponent class's getPreferredSize() method (in my application it will be in GameGraphics class) (many layout managers ask every component for that and pack() method also does it). You just need to make it return a size (in 'Dimension') you want it to have.
Also it's probably better to make a JFrame 'setResizable(false)' before you call pack().
PROBLEM SOLVED:
A solution is to override a JComponent class's getPreferredSize() method (in my application it will be in GameGraphics class) (many layout managers ask every component for that and pack() method also does it). You just need to make it return a size (in 'Dimension') you want it to have. Also it's probably better to make a JFrame 'setResizable(false)' before you call pack().
I am adding a bunch of JInternalFrames into a JDesktopPane, as the user selects to open various features through the menus. But I would like the internal frames to open centered in the desktop pane, as opposed to the upper left, where they seem to default.
How can I specify that the JInternalFrames open centered, or move them to the center after opening?
jDesktopPane.add(jInternalFrame); // jInternalFrame is not centered!
For reference, here is the solution I used, based on dogbane's advice:
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = jInternalFrame.getSize();
jInternalFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
(desktopSize.height- jInternalFrameSize.height)/2);
Work out the top-left corner of the new location (based on the size of the JDesktopPane and JInternalFrame) and then call JInternalFrame.setLocation.
If you are using Netbeans (which is recommended for desktop apps) you just need to:
Select the form, right click and then properties;
Go to code tab;
Change "Form size policy" from "Generate Pack()" to "Generate Resize
Code";
Form Position (option above Form size policy) will be available.
Now you can set the for position as you wish :)
I would suggest the Window.setLocationRelativeTo(Component) method, which will center the window relative to a specified component. Instead of passing in a JDesktopPane, you might want to obtain the parent frame for a component, since otherwise, your JInternalFrame will be centered according to whichever component you pass in.
Here is a code sample:
private void showDialog(Dialog dialogToCenter, Component button) {
Frame frame = JOptionPane.getFrameForComponent(button);
dialogToCenter.setLocationRelativeTo(frame);
dialogToCenter.setVisible(true);
}
Add this void
public void addCentered(Component jif) {
desktopPane.add(jif);
jif.setLocation((desktopPane.getWidth()-jif.getWidth())/2, (desktopPane.getHeight()-jif.getHeight())/2);
jif.setVisible(true);
}
and when adding the jInternalFrame call:
addCentered(jifName);