drawing on a JPanel - is requestfocus(); the problem?
hi,
atm, i am mostly still learning java, but, i want to write a gui/text-based rpg game...
here's my problem; i have a JFrame and several JPanels on it and all is okay in that i can add JButtons/JLabels/g.fillOval/g2.drawRect and so on!
some code for you.
Code:
public class Into extends JPanel {
public void drawing() {
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.blue);
g.fillOval(100, 50, 100, 100);
more code.
Code:
JPanel panel = new JPanel();
panel.setBackground(Color.white);
panel.validate();
panel.setLayout(new FlowLayout());
Dimension d2 = new Dimension(600, 600);
panel.setSize(600, 600);
panel.setLocation(400, 200);
frame.getContentPane();
frame.add(panel);
panel.setVisible(true);
panel.setFocusable(true);
panel.requestFocus();
Into myDraw = new Into();
panel.getRootPane().add(myDraw);
panel.requestFocus();
panel.add(myDraw);
myDraw.setVisible(true);
myDraw.drawing();
myDraw.validate();
myDraw.repaint();
myDraw.setPreferredSize(d2);
panel.validate();
panel.repaint();
and lastly.
Code:
Dimension size1 = new Dimension(
600,
600);
panel.requestFocus();
Rings rolf = new Rings();
rolf.drawme();
panel.add(rolf);
panel.getRootPane()
.add(rolf);
panel.add(rolf);
rolf.setVisible(true);
panel.requestFocus();
rolf.validate();
rolf.repaint();
rolf.drawme();
rolf.setPreferredSize(size1);
panel.validate();
panel.repaint();
now this is my problem.
ok, the first and second chunks of code work just fine for me... everything gets drawn and printed.however the third chunk doesn't work/draw! why? for size-of-post reasons, i haven't included anycode from my Rings class, but i can if it would help... why won't the third chunk of code not work? can you help me with this?
thanks!
Re: drawing on a JPanel - is requestfocus(); the problem?
Moved from New to Java
db