Drawing from one class to another

creating a simple graphics program

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 12964 Views - Last Post: 12 May 2009 - 11:50 AM Rate Topic: -----

#16 Charlie IronGleet   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Drawing from one class to another

Posted 12 May 2009 - 11:16 AM

View Postg00se, on 12 May, 2009 - 07:17 AM, said:

Dont' worry - there'll be plenty of work to do - even if you do use the existing Shape implementations ;-)


I know how to use the existing shape implimentations, as I said. And we have to use the Graphics class. I assume you're referring to drawOval() darRect() drawLine... etc. etc.

Here is my hang-up that I can't get past -- and I have been busting my ass. I can't express my difficulty better than this. I'm just writing this quickly -- my concern here is to focus on the one point in question, so just assume height and width are equal to each other, etc. etc.
public class Circle {
	private x;
	private y;
	private width;
	private height;

	public Circle(x, y, width, height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;

	public drawCircle() {
		 g.drawOval(x,y,width,height);
		 }
	 }
}


Now I want to use this Circle class to place a circle on the JPanel called canvasPanel inside this class:
public class Canvas extends JFrame {
	  private JPanel canvas = new JPanel();
	  private JButton draw = new JButton("draw");

	  public Canvas() {
		 setLayout(new BorderLayout());
		 add(draw, BorderLayout.NORTH);
		 add(canvas, BorderLayout.SOUTH);
		 setVisible(true);
		 setLocationRelativeTo(null);
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		 draw.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
					   // STICKING POINT.  HOW THE HELL DO I TELL IT TO PUT THE CIRCLE HERE????
					  // PLEASE HELP!!
				}

		  }
		 
	   }
		  protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				// if I remember right, this causes an error cannot find symbol paintComponent(g)
		   }
}



If someone could please help me fix this code, my questions would be answered and I could work from this model. I'm not trying to cheat here -- I've been busting my butt on my own for hours over this. I have a genuine interest in learning Java. I'm not just trying to 'get through'. I'm just stumped.

Thanks!
Irongleet

This post has been edited by Charlie IronGleet: 12 May 2009 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#17 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Drawing from one class to another

Posted 12 May 2009 - 11:40 AM

public class Circle {
    private x;
    private y;
    private width;
    private height;

    public Circle(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

    public drawCircle(Graphics g) {
         g.drawOval(x,y,width,height);
         }
     }
}



and ...

public void paintComponent(Graphics g) {
	super.paintComponent(g);
	aCircleInstance.drawCircle(g);
}


Was This Post Helpful? 0
  • +
  • -

#18 Charlie IronGleet   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Drawing from one class to another

Posted 12 May 2009 - 11:50 AM

thanks -- I'm close to getting it right now I think. I'm getting that same cannot find symbol error about super.paintComponent(g) though. Here is my code. Can you point out where I've strayed here? and I don't see why this would even work to draw on the panel in question 'bottomPanel', which is what I want to do.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JFrame;



public class CadGui extends JFrame {
	private JButton drawButton = new JButton("Draw Shape");
	private JButton eraseButton = new JButton("Erase Shape");

	private String[] shape = { "Circle", "Rectangle", "Triangle", "Line" };
	private JComboBox jcbShape = new JComboBox(shape);
	private String[] size = { "small", "medium", "large" };
	private JComboBox jcbSize = new JComboBox(size);
	private String[] location = { "north", "south", "east", "center", "west" };
	private JComboBox jcbLocation = new JComboBox(location);
	private JPanel topPanel  = new JPanel();
	private JPanel bottomPanel = new JPanel();
	private Circle circle= null;



	private int sizeChoice = 0;
	public Graphics canvasGraphics = bottomPanel.getGraphics();

	public CadGui() {
		setLayout(new BorderLayout());
		bottomPanel.setLayout(new BorderLayout());
		topPanel.add(drawButton);
		topPanel.add(jcbShape);
		topPanel.add(jcbSize);
		topPanel.add(jcbLocation);

		topPanel.add(eraseButton);
		add(topPanel, BorderLayout.NORTH);
		add(bottomPanel, BorderLayout.CENTER);
		setSize(640,480);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

		drawButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bottomPanel.repaint();
			}
		});

		jcbShape.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int tempChoice = jcbShape.getSelectedIndex();
				switch(tempChoice) {
					case 0: circle();
								break;
					case 1: rectangle();
								break;
					case 2: triangle();
								 break;
					case 3: line();
								 break;
				}
			}
		});
		jcbSize.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				sizeChoice = jcbSize.getSelectedIndex();
			}
		});
	}

	public void circle() {
		circle = new Circle(100,100,100,100);
	}
	public void rectangle() {

	}
	public void triangle() {
	//
	}
	public void line() {
	//
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		circle.draw(g);


	}

}

This post has been edited by Charlie IronGleet: 12 May 2009 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2