2 Replies - 110 Views - Last Post: 13 September 2012 - 08:55 AM Rate Topic: -----

#1 pietomb00  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 68
  • Joined: 28-June 11

Confused by Graphics2D

Posted 13 September 2012 - 08:28 AM

Hi,

This is probably very simple, but I'm just not understanding it. I'm trying to draw a rectangle, but when I come to it nothing shows.

I'm not sure if and how I should call the paint() method. After reading another post I got the impression it should be called whenever the program decides to repaint, so without me doing anything.

public class UserInterface {

	public static void main(String[] args){
		UserInterface GUI = new UserInterface();
		GUI.run(args);
	}
	
	void run(String[] args){
		JFrame window = new JFrame("Main");
		window.setLayout(new BorderLayout());
		window.setVisible(true);
		window.setSize(800, 750);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel content = new JPanel(new GridLayout(2,2,10,10));
		window.add(content, BorderLayout.NORTH );
		content.repaint();
		
	}
	
	public void paint (Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		g2.draw(new Rectangle2D.Double(10,10,100,500));
		
	}
	
	
}


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Confused by Graphics2D

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1007
  • View blog
  • Posts: 2,248
  • Joined: 05-April 11

Re: Confused by Graphics2D

Posted 13 September 2012 - 08:46 AM

The thing is that it is the JFrame having the paint method, but you are placing it in your UserInterface class.
You should make a class that inherits from JFrame and then override the paint method.

The compiler would have warned you if you added the @Override annotion
	@Override
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		g2.draw(new Rectangle2D.Double(10,10,100,500));
	}


Was This Post Helpful? 1
  • +
  • -

#3 pietomb00  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 68
  • Joined: 28-June 11

Re: Confused by Graphics2D

Posted 13 September 2012 - 08:55 AM

Ah yes I see, got it now. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1