Reputation: 1
I'm creating a Java game for fun and I want to add many things to a JFrame at once. But for some reason, one class and the main method class executes, but the third class containing the second object I want to add doesn't execute. I'm still new to Java so I might get some terms wrong.
Basically I have 3 classes:
main.java (main method class + JFrame constructor class)
Infout.java (class that draws a keyboard-controlled circle + some stationary rectangles)
obj2.java (class that draws a second stationary circle)
main.java
import javax.swing.JFrame;
public class main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
Infout m = new Infout();
obj2 o = new obj2();
frame.add(o);
frame.add(m);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
frame.setSize(300, 400);
frame.setTitle("Circle");
}
}
Infout.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Infout extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
double x = 0, y = 0, velx = 0, vely = 0;
public Infout(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(new Ellipse2D.Double(x, y, 40, 40));
g2.fill(new Rectangle2D.Double(0, 270, 300, 5));
g2.fill(new Rectangle2D.Double(140, 270, 5, 300));
g2.fill(new Rectangle2D.Double(140, 60, 70, 5));
g2.fill(new Rectangle2D.Double(50, 140, 5, 70));
g2.fill(new Rectangle2D.Double(150, 130, 5, 40));
g2.fill(new Rectangle2D.Double(190, 210, 40, 5));
if (x >= 120 && y >= 270) {
System.out.println("sum1 has reached tha corner");
g.drawString("You win!",115,35);
}
if (x <= 120 && y >= 270) {
System.out.println("sum1 has reached tha corner");
g.drawString("You lose!",115,35);
}
if (x == 120 && y >= 270){
velx = 0;
vely = 0;
}
if (x == 31.5 && y <= 200 && y >= 100){
velx = 0;
}
if (x == 132 && y <= 170 && y >= 100){
velx = 0;
}
if (x <= 190 && x >= 120 && y == 42){
velx = 0;
}
if (x <= 210 && x >= 171 && y == 192){
velx = 0;
}
}
public void actionPerformed(ActionEvent e) {
System.out.println("x "+x+"y "+y);
repaint();
x += velx;
y += vely;
if (x < 0 || x > 260)
{
velx = 0;
vely = 0;
}
if (y < 0 || y > 340)
{
velx = 0;
vely = 0;
}
}
public void up() {
vely = -1.5;
velx = 0;
}
public void down() {
vely = 1.5;
velx = 0;
}
public void left() {
velx = -1.5;
vely = 0;
}
public void right() {
velx = 1.5;
vely = 0;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
up();
}
if (code == KeyEvent.VK_DOWN) {
down();
}
if (code == KeyEvent.VK_RIGHT) {
right();
}
if (code == KeyEvent.VK_LEFT) {
left();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
obj2.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class obj2 extends JPanel {
public void paintComponent(Graphics g1)
{
super.paintComponent(g1);
Graphics2D g3 = (Graphics2D)g1;
Ellipse2D circle = new Ellipse2D.Double(50.0D, 50.0D, 40.0D, 40.0D);
g3.fill(circle);
}
public obj2(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So basically, I get no compile errors, but even though I created an instance of each class variable in the main method, and added them both to the JFrame, they cannot both execute at once. If I comment out obj2 from the main method, Infout will show up. If I comment out Infout from the main method, obj2 will show up. But not both at once. If I try both at once, only Infout shows up.
As you may have saw, I thought maybe it had something to do with multithreading so I added some code for multithreading that you may have noticed but I'm sure it's wrong since I only learned about multithreading like an hour ago.
May someone pleeeease help me figure this out? I've tried everything I know to solve it, but it just won't work :C.
I would absolutely LOVE example code of maybe a simple program you guys could quickly whip up showing me how this works. I would even love more if you could explain why/how it works the way it does. I like learning! :)
Thanks!
Upvotes: 0
Views: 771
Reputation: 324157
By default a JFrame uses a BorderLayout. When you use the add(...) method without a constraint the component will be added tot he CENTER or the BorderLayout. However, only one component (panel) can be displayed in the CENTER, so only the last panel added is visible.
Read the section from the Swing tutorial on How to Use the BorderLayout for more information and examples.
Generally when I see code like you have you can display the components on top of one another with code like:
panel1.add( panel2 );
frame.add( panel1 );
Or if you want the coponents side by side then you can change the layout manager of the content pane to use a FlowLayout
. Again, read the tutorial. There are examples for the FlowLayout and other layout managers.
Upvotes: 1