Can't Get ImageIcon to Display - java

I've looked at a ton of other questions where people have similar issues to what I'm having here (most solutions found by simple mistakes) but I can't for the life of me figure out why my graphics won't display in my jframe. I'm pretty new to Java Graphics so I'd appreciate all the help that I can get. (If someone thinks this is a repeat question, all I ask is that you wait until I get an answer before you close it)
Oh, also, when I run the program, it tells me that the repaint method is called, if that helps in some way
package game.try5;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window {
public Window() {
JFrame frame = new JFrame("Epic Game");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args){
Window window = new Window();
}
}
.
package game.try5;
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
GameObject go = new GameObject(0,0,false,"Dog.jpg");
public GamePanel(){
repaint();
System.out.println("Repaint method called");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(go.getImg().getImage(), go.getxLoc(), go.getyLoc(), 50, 50, null);
System.out.println("Graphics method called");
}
}
.
package game.try5;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window {
public Window() {
JFrame frame = new JFrame("Epic Game");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args){
Window window = new Window();
}
}

Comment out frame.setLayout(null); and you'll most likely see the game.
The default layout of a JFrame is currently a BorderLayout
An object added to a BorderLayout without a constraint defaults to the CENTER.
An object in the CENTER of a BorderLayout is stretched to the available with and height.
Since the code calls frame.setSize(800,600); that available width and height is 800x600 pixels less the frame decorations.
A better all round approach is to:
Have the custom painted component override getPreferredSize() to return a sensible value.
Add the component to a container.
Lastly, pack the top level window. This will make the frame the smallest size it needs to be in order to display the content.

Related

My JFrame wont become the size that i set

no matter what i try my jframe window wont resize,its either really small or just goes full screen, i dont know what must resize either if possible could you help with that too
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TheHundeler {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("java poes");
JFrame Frame = new JFrame();
CalculaterContructer builder1 = new CalculaterContructer();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel board = new JPanel();
board.setLayout(null);
builder1.buttonBuild(board);//this function has no effect on the end result of the size change
//not working
board.setSize(1000, 1000);
Frame.add(board);
Frame.setSize(new Dimension(1000, 1000));
Frame.pack();
Frame.setVisible(true);
}
}
I suggest you let the panel dictate the size. That way, the frame borders won't affect it and the actual space you see will reflect the dimensions.
For the panel, do a setPreferredSize() of your size. Then add it to the Frame and do a frame.pack().
And after pack if you do frame.setLocationRelativeTo(null) it will center it on the screen.

Why is the textfeld not enlarging on the JFrame?

Below is a Java program which does not work properly. I wanted the JTextfield to go to the bottom, and be 50x50, but it is not working. I was wondering if I could get some help.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Main {
public static JTextField intext = new JTextField();
public static JPanel panel = new JPanel();
public static boolean running = true;
public static String runtext;
public static String word = "HELLO";
public static String guesses = "";
public static void main(String[] args) {
intext.setBounds(5,667,50,50);
panel.add(intext);
JFrame frame = new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setVisible(true);
I inferred that when using the .setBounds() method the JTextfield would go where it was needed, though it remains at the top, and extremely thin.
Try editing your code this way:-
panel.setLayout(null); // setting layout for panel
panel.setBounds(0,0,600,650); // setting bounds for panel with respect to frame
intext.setBounds(5,557,50,50); //setting bounds for textfield with respect to panel
panel.add(intext);
JFrame frame= new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setLayout(null); // setting layout for frame
frame.setVisible(true);
Make sure to include layouts for your components and set the bounds for your JPanel's and JTextField's properly.

Jframe window will not work anymore

package Jframe;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JavaWindows {
public static void main(String args[]) {
JFrame Window = new JFrame();
JLabel Label = new JLabel("test");
JPanel Panel = new JPanel();
ImageIcon icon = new ImageIcon("Heart.png");
Window.setIconImage(icon.getImage());
Window.add(Panel);
Window.add(Label);
Window.setSize(500,750);
Panel.setSize(500, 900);
Window.getContentPane().setBackground(Color.PINK);
Window.pack();
}
}
About 10 min ago this code worked. It made a window that was about 100,100 with a 500,900 panel that was sized correctly. The window was not 500,750 like i specified I don't know what went wrong there but all sudden the window wont run in Eclipse. I deleted window.pack(); and retyped it now its broke. Any ideas?
you have to set visibility of the frame
window.setVisible(true);

JButton takes entire frame

I'm creating a login system and when I made my login button, it took up the entire frame. I tried the .setBounds(); but it did not work. Not sure what I did wrong, please help.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main extends JFrame{
public Main(){
JTextField name = new JTextField("Username");
JTextField pass = new JTextField("Password");
JButton login = new JButton("Login");
name.setBounds(230, 110, 100, 25);
pass.setBounds(230, 145, 100, 25);
login.setBounds(230, 165, 100, 25);
add(name);
add(pass);
add(login);
}
public static void main(String [] args) {
Main a = new Main();
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setSize(500, 300);
a.setLocationRelativeTo(null);
a.setVisible(true);
a.setTitle("Login System");
a.setResizable(false);
a.setLayout(new FlowLayout());
}
}
in your constructor, try setLayout(new FlowLayout());.
This will likely not lead to what you want, but from there on you can investigate further. i recommend you read about layouts here:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
By default, Frame has a BorderLayout installed. When the items are added they are all added to "centre" because constraints are not passed. And the centre component occupies all free space in the BorderLayout. You can either change the layout manager or provide constraints while adding components.
FlowLayout or BoxLayout are good candidates for this.
Problem is by default the layout manager for JFrame is BorderLayout. Once you add components with method add() it gets added to the center region. So the last component added is shown. In your case its the login button. Also setBounds() don't work with the said layout manager.
You have to work a lot on your coding style. What you did is first created the frame and added components to it and later in the main() you have set the size, made it visible and then you set the layout manager to FlowLayout.
Ideally, you must construct the frame, set layout, add components to frame, use pack() to set frame size, set frame's location and finally make frame visible.
Using BorderLayout:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
#Override
public void run() {
JFrame frame = new JFrame("BorderLayout");
frame.getContentPane().add(new JTextField(15), BorderLayout.WEST);
frame.getContentPane().add(new JPasswordField(15), BorderLayout.CENTER);
frame.getContentPane().add(new JButton("Login"), BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Using FlowLayout:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
#Override
public void run() {
JFrame frame = new JFrame("FlowLayout");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JTextField(15));
frame.getContentPane().add(new JPasswordField(15));
frame.getContentPane().add(new JButton("Login"));
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
P.S. If you still want to use absolute positions, I would recommend you using SpringLayout or GroupLayout.

My program won't paint Here's the code:

This code won't fill the rectangle. I tried to extend JComponent but I received an error. How would I extend JComponent?
package com.lewis.GooseEgg;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class GooseEgg extends Canvas{ //I couldn't extend JComponent
//How would I also extend JComponent
protected void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0, 100, 100);
}
public static void main(String[] args) {
//This is just all the basic stuff I learned
JFrame frame = new JFrame();
GooseEgg goose = new GooseEgg();
frame.setResizable(false);
frame.setTitle("Goose Egg");
frame.add(goose);
frame.pack();
frame.setSize(900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//The website keeps asking me to add more detail because it says my post is mostly code so I added this. If anyone wants to tutor me at Java
//I could pay money.
}
}
You don't need to extend from Canvas and JComponent: You should only extend from JComponent.
Also you should not do pack() and setSize(…). They both set the size of your frame, this does not make sense. Either do pack() (in which case you also need to specify a preferred size e.g. by overriding getPreferredSize() or setting it with setPreferredSize(…)) or do setSize(…).
Changing Canvas to JComponent and removing the pack() method call seems to produce the correct result.

Categories

Resources