Java Canvas white edge - java

So I'd like to make a Canvas based Java application. I've extended my main class to Canvas and I size it in it's constructor.
public CanvasApp() {
Dimension size = new Dimension(640, 480);
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
and in the main function, I make a frame for it, like this:
CanvasApp cnv = new CanvasApp();
JFrame frame = new JFrame("");
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(null); //I've tried this
panel.setSize(640,480); //but still doesn't work =(
panel.add(cnv, BorderLayout.CENTER);
frame.setContentPane(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
But the content pane appears 650x490 in size. Why is this?
I've attached a picture too.
I've drew a line from 0,0 to 640,480

Because a JFrame has a 5px border around it, look at any frame on your computer screen you'll notice a kind of embossed border - that border is 5 px in width adding 10 pixels onto height and width. You've only assigned the panel to 640 x 480 and plonked it inside the frame - the frame then adds it's own border onto that.

Thats probably because every JComponen have a border, you put your CanvasApp inside JPanel and JPanel itself into JFrame. That is probably the reason why you get bigger dimensions at the end.
See the oracle website on how to use borders here: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html

Related

Java JPanel Resizing to Dimension(10, 10)

I am creating a game, and I have a JPanel inside my JFrame, and I want my panel to maintain a constant size ratio: 1.3x1 (as large as possible). I have a component listener on the frame, and when the frame is resized it resets the size of the panel using setPreferredSize, the problem is that every time I change the frame size, the panel gets resized to a Dimension(10, 10) - meaning that the frame is always tiny. The frame also has a GridBagLayout set because i want the panel in the middle. In addition - sometimes if you resize the frame quickly (draw the corner fast) the frame will appear at the right size for a split second, then go back to 10, 10.
Resizing code in the component listener:
Game.frameSize.x = (int) Math.round((GameFrame.frameSize.y - 2) * 1.2);
Game.frameSize.y = GameFrame.frameSize.y - 2;
this.panel.setPreferredSize(new Dimension(Game.frameSize.x, Game.frameSize.y));
* Game is my jpanel and GameFrame is my JFrame, Game.frameSize and GameFrame.frameSize is only for resizing sprites on the screen
Configuration of the JFrame and the JPanel
JFrame frame = new JFrame("");
frame.setLayout(new GridBagLayout());
frame.setPreferredSize(new Dimension(frameSize.x, frameSize.y));
frame.setSize(new Dimension(frameSize.x, frameSize.y));
frame.setMinimumSize(new Dimension(800, 600));
frame.setResizable(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
Game game = new Game(frame);
Game.frameSize = new Vector((int) Math.round((GameFrame.frameSize.x - 2) * 1.3), GameFrame.frameSize.y - 2);
game.setSize(new Dimension(frameSize.x, frameSize.y));
game.setPreferredSize(new Dimension(frameSize.x, frameSize.y));
frame.addMouseMotionListener(new GameMouseMotionListener());
frame.addComponentListener(new GameComponentListener(frame, game));
frame.addMouseListener(new GameMouseListener());
frame.addKeyListener(new GameKeyListener());
frame.add(game);
I am really not sure what to do here, as far as google is concerned, no one has ever had this issue. Thank you, any help is appreciated :)

JLabel wont change Width

I'm programming right now a Chomp Game for Uni. Everything works fine but the Label at the bottom. Its background is supposed to fill out the entire bottom. In the attachment you can see how it instead looks now. I tried setting the minimum and the preferred size of the label. The Height is changing but the width just stays adjusted to the text. How can I change that?
Note: The snippet only contains the setting up of the Frame and Panels in a custom method and not the main class.
private void init()
{
JFrame fenster = new JFrame();
this.spielfeld = new SpielfeldPanel(M, N);
this.anzeige = new SpielerAnzeigeLabel(this.spieler);
JPanel panel = new JPanel();
BoxLayout boxlayout = new BoxLayout(panel,BoxLayout.Y_AXIS);
panel.setLayout(boxlayout);
fenster.setTitle("Chomp");
fenster.setSize(1000,700);
panel.setPreferredSize(new Dimension(1000, 60));
Dimension d = new Dimension(getPreferredSize());
panel.setMinimumSize(d);
panel.add(spielfeld);
panel.add(anzeige);
fenster.add(panel);
this.spielfeld.setVisible(true);
this.anzeige.setVisible(true);
panel.setVisible(true);
fenster.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
A BoxLayout respects the width of the component to the label is displayed at its preferred width/height.
A JFrame uses a BorderLayout by default. So just add the label to the frame independently of the panel:
//panel.add(anzeige);
//fenster.add(panel);
fenster.add(panel, BorderLayout.CENTER);
fenster.add(anzeige, BorderLayout.PAGE_END);
The PAGE_END constraint respects the height but makes the width equal to the space available. Read the section from the Swing tutorial on How to Use BorderLayout for more information and examples.

vlcj black screen when playing video

I am having a bit of trouble with nested JPanels playing a video. I have an AVPlayer class extend JPanel which plays up to 4 videos simultaneously. Each video is played inside its own canvas which is inside its own JPanel. All the panels are then put into the AVPlayer panel. But when I try to play the videos all I get is a black square.
I'm not sure what the actual problem in my bigger program is but I think I can solve it if I can get the videos to play using the second bit of code below. Can someone tell me why the first bit of code is properly able to display all the videos, but the second one is not.
Code that works:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
AVPlayer player = new AVPlayer();
frame.getContentPane().add(player);
frame.revalidate();
String[] path = {"(ei)ga_00.mp4", "ei-utsu(ru)_00.mp4", "video.mp4"};
player.playVideo(path);
Code that shows one small black square
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
AVPlayer player = new AVPlayer();
JPanel panel = new JPanel();
panel.add(player);
frame.getContentPane().add(panel);
frame.revalidate();
String[] path = {"(ei)ga_00.mp4", "ei-utsu(ru)_00.mp4", "video.mp4"};
player.playVideo(path);
Change JPanel panel = new JPanel(); to JPanel panel = new JPanel(new BorderLayout());
Your AVPlayer should also override the getPreferredSize method of JPanel and return the "preferred size" of the component, this way the layout managers have some hope of actually been able to do there jobs
See Laying Out Components Within a Container for more details
Beware that vlcj's primary video surface is a heavy weight component and mixing them on light weight containers can generate some undesirable effects

Java JFrame setSize doesn`t work properly

I am making a JFrame with the size of 500x500 pixels.
I make a blue background and add a red square in the right-bottom corner from (490,490) to (500,500).
Image:
I don't see the red square on the screen.
I switched the frame from not resizable to resizable and if I make the window larger the red dot is there.
Is the frame size the same as application's window size?
How can I make the application's window to be the exactly 500x500?
Your content pane should override the getPreferredSize() method, returning a Dimension object with width and height of 500 pixels:
public class MyContentPane extends JPanel {
private Dimension dimension;
public MyContentPane() {
this.dimension = new Dimension(500, 500);
}
#Override
public Dimension getPreferredSize() {
return this.dimension;
}
}
// How to use your new class
SwingUtils.invokeLater(() -> {
JFrame frame = new JFrame("Title");
frame.setContentPane(new MyContentPane());
frame.pack();
frame.setVisible(true);
});
The size of your JFrame will be calculated by Swing by taking in consideration the preferred size of the components inside it.
The frame is the size of the entire window, including the title bar required by the OS. When drawing things in the JPanel in the JFrame, the (0, 0) coordinate is in the top left corner if the JPanel, which begins just below the title bar. It sounds like your title bar is taller than 10 pixels, so 490 as a y component is actually off the window, since the visible height of the JPanel is windowHeight - titleBarHeight.
Should user the following
#Override
PreferedSize()
Remember preferedSize method is method of the super class JFrame.
this may be useful for you?
JPanel aa = new JPanel(new GridBagLayout());
aa.setMaximumSize(new Dimension(500,500));
aa.setMinimumSize(new Dimension(490,490));
aa.setBorder(BorderFactory.createLineBorder(Color.black));

Draw Rectangle as Progressbar in JFrame

I have a JFrame with a BorderLayout(). In the SOUTH Layout Constraint I want to put a progressbar. Not a dynamic one but one that gets set with a value (0-100) from time to time.
I thinking of a JPanel in which I draw a Rectangle with appropriate Width.
How can I draw a rectangle inside a JPanel?
JFrame frame = new JFrame();
frame.setBounds(100, 100, 790, 539);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0,0));
frame.setResizable(false);
JPanel panelSouth = new JPanel();
frame.getContentPane().add(panelSouth, BorderLayout.WEST);
What I was thinking, maybe I could just resize the JPanel panelSouth. But in the Borderlayout it always stretches out to filll the whole SOUTH.
Instead of starting from scratch, stick with JProgressBar to simplify your event coding. You can draw a rectangle in your implementation of the paint() method in a BasicProgressBarUI, as shown here with an ellipse drawn using fillOval() in paintIndeterminate().

Categories

Resources