Results 1 to 2 of 2
  1. #1
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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.

    Java 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.

    Java 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.

    Java 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!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: drawing on a JPanel - is requestfocus(); the problem?

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Drawing Shapes on to a Jpanel
    By pepsi in forum AWT / Swing
    Replies: 7
    Last Post: 03-09-2012, 06:39 PM
  2. Possible to do JPanel drawing without extending?
    By SM2010 in forum AWT / Swing
    Replies: 7
    Last Post: 07-03-2010, 12:30 AM
  3. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 05:46 AM
  4. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 09:12 PM
  5. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •