Paint only part of screen
Hi everyone
I've trying to make simple 2D game in Java, which involves moving sprites around.
However, i don't know much about paint, so I ask is there a way for paint to use only part of screen? Like on image here:
http://img829.imageshack.us/img829/4166/paintqe.th.jpg
Uploaded with ImageShack.us
That middle square with red lines is area i would like to use with paint, and around it are JPanels that contain buttons with controls, score, etc.
Is it possible?
Re: Paint only part of screen
Well, if you have JPanels surrounding the middle area, why not make the middle area a JPanel and paint to that? That should effectively limit the painting to the JPanel in the middle.
Re: Paint only part of screen
Can you give some example? Right now i have one class, AppCore, that extend JFrame, initialize other panels, and paint is also done here.
Re: Paint only part of screen
Ok, well I can only provide so much because I would assume you are wanting to use BoarderLayout for this and I have no experience with JAVA Provided Layouts (I made my own, its easier).
Basically, in your main you can create a JFrame where you can add 6 or whatever JPanels to it. All of those JPanels can be normal JPanels, except for 1, which will be a JPanel class you make.
Instead of having a class that extends JFrame, have a class that extends JPanel. That class will be your middle JPanel in your JFrame. From there, you can override the paintComponent(Graphics g) method in your JPanel class and essentially do the exact same thing you would with your extended JFrame.
Re: Paint only part of screen
Thank you for your advice. I will try to rewrite code, if i encounter problems, i will post them here.
Re: Paint only part of screen
I tnink that i could get it working, but i have encountered problem.
So, middle panel is PaintPanel (where drawing should be).
On the left side, there is some, let say ControlPanel with some buttons, but they must be placed on specific place so i used setLayout(null), and setBounds, to place them where i want them. But ControlPanel doesnt show up, because of null layout (if i comment that line, ControlPanel shows up but buttons are aragned in line, like in FlowLayout).
Any thoughts, how to get it done?
Re: Paint only part of screen
Quote:
Originally Posted by
bonus_clip
I tnink that i could get it working, but i have encountered problem.
So, middle panel is PaintPanel (where drawing should be).
On the left side, there is some, let say ControlPanel with some buttons, but they must be placed on specific place so i used setLayout(null), and setBounds, to place them where i want them. But ControlPanel doesnt show up, because of null layout (if i comment that line, ControlPanel shows up but buttons are aragned in line, like in FlowLayout).
Any thoughts, how to get it done?
Without code to look at, there isn't much help that can be given.
Re: Paint only part of screen
Oh sorry. Well here is constructor for left panel:
Code java:
setLayout(null);
ii = new ImageIcon("green arrow.png");
label1 = new JLabel(ii);
JLabel label2 = new JLabel(ii);
label1.addMouseListener(new MouseList());
label2.addMouseListener(new MouseList());
label1.setBounds(50, 75, 70, 70);
label2.setBounds(50, 425, 70, 70);
add(label1);
add(label2);
And this is main class, that extends JFrame:
Code java:
setLayout(new BorderLayout());
getContentPane().add(new LeftPointerPanel, BorderLayout.WEST);
getContentPane().add(new PaintPanel(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1024, 768);
setVisible(true);
For some strange reasons, if i set LeftPointerPanel to BorderLayut.CENTER, it show up even with null layout!