Why the background isn't right? - java

I am trying to make a java game. And I am confronted with a problem:
public void setUp () {
JFrame frame = new JFrame ("Key test");
//MyDrawPanel4 dp4 = new MyDrawPanel4();
//frame.setContentPane(dp4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());//why this sentence is necessary
p.addKeyListener (this);
p.add (dp);
frame.getContentPane().add(p);
frame.pack();
}
And the result is a small snack.
I want to use dp4 as the backgound of game. But what actually happen is
So my question is :
1.Why this happen?
2.How to make background of game?
Thanks in advance.
Edit: what I really mean is the two black body of the snack disappear, and it cannot move when I press the key (I use the key listener to fulfill it).

JFrame frame=new JFrame("arjun");
JPanel panel=new MyDrawPanel();
panel.setSize(100,100);
image=new ImageIcon("C:/raptor.jpeg").getImage(); // use this
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);

JFrame frame = new JFrame ("Key test");
//MyDrawPanel4 dp4 = new MyDrawPanel4();
//frame.setContentPane(dp4);
-->FIRST
**Panel p = new Panel ();
p.setLayout(new BorderLayout());//it's a container
p.addKeyListener (this);
p.add (dp);
frame.getContentPane().add(p);**
-->AFTER
frame.setSize (300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
The image that you try to load is too large.
When you load an image, you MUST resize it to Image (or simile) control sizes.

Related

ImageIcon in swing package of Java not functioning correctly

I am having issues with creating an custom imageIcon in Java using swing. The file-name works, but it appears that the ImageIcon isn't changing at all. Here is the code:
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JFrame frame = new JFrame("NumberPad");
frame.setIconImage(icon.getImage());
frame.setName("Pin Pad");
frame.setContentPane(new NumberPad().NumberPadPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Anyone got suggestions for what I should do?
You should make a compilable example.
public class IconCheck{
public static void main(String[] args){
JFrame frame = new JFrame("icon test");
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JLabel label = new JLabel(icon);
frame.setIconImage( icon.getImage() );
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
Does that show your icon in the JFrame? If yes, then does it update your JFrame icon?
You probably want your png to be used as a resource.
ImageIcon icon = new ImageIcon( IconCheck.class.getResource("/sample.png") );
It looks up the path on the classpath, which intellij knows how to include.

Adding JPanel created on netbeans on top of to anotherJpanel created programatically

I have a panel that I am drawing some stuff on and I want to have an interface on top of it. I created an interface as a JPanel on netbeans, visually. But interface is not displayed properly.
Here is my code
public static void main(String[] args) {
JFrame frame = new JFrame("WorldGen");
Interface inter = new Interface();
JLayeredPane lpane = new JLayeredPane();
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
lpane.add(panel, new Integer(0), 0);
lpane.add(inter, new Integer(1), 0);
panel.setBounds(0,0,600,400);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
}
Panel is declared as a static JPanel.
static JPanel panel = new JPanel()
Here is my result:
This is the Interface class that is created in netbeans visually
When I add this line:
inter.setBounds(0,0,600,400);
inter.setOpaque(true);
this is what I get:
Just a blank screen. I don't expect it to be transparent since I set it to opaque myself but It seems I have another problem. The button is not showing whether I set it to opaque or not.
Why is the button not showing? I am hoping that the button will still be visible when I set opaque to false, after I resolve this problem.
I've solved it by creating a JFrame in netbeans visually, adding a JPanel to it. Then using that panel(by overriding the paint method) to draw my image.

How to change the color of the background in a jFrame [duplicate]

This question already has answers here:
Setting background color for a JFrame
(14 answers)
Closed 7 years ago.
I am trying to make a planner and want to change the background of my JFrame.
I have already tried frame.getContentPane().setBackground(Color.); but that doesn't seem to work.
Here is the code for the frame portion
`public Planner(){
frame = new JFrame();
main = new JPanel();
menu = new Menu(this);
frame.setPreferredSize(preferredSize);
frame.add(main);
frame.setJMenuBar(menu);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Myva");
JLabel loading = new JLabel();
JOptionPane pane = new JOptionPane();
pane.showMessageDialog( null, "Hi. ");
name = pane.showInputDialog("What is your name:");
}
Thanks in advance
If I understand your question, then you could call JFrame.setBackground(Color) like
frame.setBackground(Color.BLUE);
If you want to change the color in a more visible manner, you can make it on a JPanel. Like,
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setVisible(true);
}
which will give you a very BLUE window.
To change the color of your JFrame, use the following code:
frame.setBackground(Color.BLUE);
(Color doesn't have to be blue, I just used it as an example)

Call class/ JPanel from Main Project?

I've finished my store management program with many classes (many JForm panels actually. Thanks to people in this forum whom helped me so much).
I just need to call JPanel Login when I click Run Project.
Any idea how to call it? what code I have to insert to main project?
You cannot display JPanel independently.
So you have to create a JFrame and then add this JPanel object to this JFrame and then display the JFrame.
write these lines in your main method.
JFrame frame = new JFrame("Title");
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // as per your requirement.
frame.add(new Login(), BorderLayout.CENTER);
frame.setVisible(true);
it will work properly.
JFrame myFrame = new JFrame("MyTitle");
myFrame.setSize(width, height);
myFrame.setLocation(x, y);
myFrame.setContentPane(new Login());
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// if you want add your jcomponent
myFrame.getContentPane().add(yourJComponent);
try it!
If you want add JComponent(JButton)
JButton myBtn = new JButton("btn name");
myBtn.setLocation(x, y);
myFrame.getContentPane().add(myBtn);
If you want add JComponent(JText)
JText myText = new JText("text");
myText.setLocation(x, y);
myFrame.getContentPane().add(myText);
JComponent [http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html]

Swing: Showing a transparent Panel hovering over another Panel

I want to show a textArea showing some text (will show log lines) , and have an animated gif hoovering above it. I tried the solution described here , but all I get is a grey screen. Hints?
public class TestLayeredPanes {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
public TestLayeredPanes() {
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
//Build the animated icon
JLabel buildingIcon = new JLabel();
buildingIcon.setIcon(new ImageIcon(this.getClass().getResource(
"/com/ct/tasks/cmviewer/gui/progress_bar.gif")));
JPanel iconPanel = new JPanel();
iconPanel.add(buildingIcon);
//Build the textArea
JTextArea textLog = new JTextArea("Say something");
JPanel textPanel = new JPanel();
textPanel.add(new JScrollPane(textLog));
//Add the panels to the layered pane
lpane.add(textPanel, 0);
lpane.add(iconPanel, 1);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new TestLayeredPanes();
}
}
Try putting your animated GIF on the glass pane of your root pane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
JXLayer make easier to do that. Look at JXLayer samples.
You also can take a look at code of XSwingX
Since you started with a working example, why did you remove lines of code from the example you copied?
Layered panes don't use a layout manager therefore the size of your components are (0, 0), so there is nothing to display. The setBounds(...) method in the example are there for a reason.

Categories