Jump to content
  • Advertisement
Sign in to follow this  
BlackSunStudios

Java JFrame/JPanel issues regarding Paint

This topic is 2618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys i need some quick help.

I want to draw multiple objects on the screen that draw themselves when initiated.
At the moment it works.
Heres how i do it.

import javax.swing.*;


public class Main extends JFrame {




public static void main(String[] args){
JFrame frame = new JFrame("hello");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 535);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 640, 535);
frame.add(panel);
panel.add(new Rect(0,0,100));
panel.add(new Rect(0,0,5));


}

}


now when i start the program it puts the rectangles in the middle of the screen?
I tried panel.setBounds(); but it didnt work?

Can anyone help?

heres my Rect Class:


import java.awt.*;
import java.awt.event.*;


import javax.swing.*;


public class Rect extends JPanel implements ActionListener {
int x,y,size;
public Text(int X,int Y,int Size){
x = X;
y = Y;
size = Size;

Timer time;
time = new Timer(10,this);
time.start();
}


public void actionPerformed(ActionEvent arg0) {

repaint();

}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.fillRect(x, y, size, size);
}

}


the rect class is alright i think but the Main class is a bit messed up :s

Share this post


Link to post
Share on other sites
Advertisement
Instead of adding "panel" to the JFrame, use it as the content pane. This is really the first component we can render too, without overriding the JFrame itself.
Also you can use Absolute Positioning to prevent the Layout Managers from interfering with positions & sizes.


public static void main(String[] args) {
JFrame frame = new JFrame("hello");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 535);

JPanel panel = new JPanel();

// Use Absolute positioning. (Disable Layout Manager).
panel.setLayout(null);

panel.setBounds(0, 0, 640, 535);
panel.add(new Rect(0,0,100));
panel.add(new Rect(0,0,5));

// Set as content pane. (The root container where we can draw).
frame.setContentPane(panel);

frame.setVisible(true);
}


In future you may want to consider Active Rendering instead of Swing. Swing is really designed for application GUI s, not games.

Share this post


Link to post
Share on other sites
Hi I think your problem lies in the layout style, if you want to set exact positions (setBounds) then you need to set the panel layout style to null. I have not programmed in java for some time now so I think the syntax is (panel.setLayout(null))

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!