Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Central New Jersey
    Posts
    207
    Rep Power
    22

    How do I call the draw() method?


    In the Classes chapter of the book Java by Example they introduce a Rectangle class and a RectTest class, which are very straightforward.

    What I'm confused about is how I would call the draw() method of the DrawableRect class. It's heading is

    public void draw(java.awt.Graphics g)

    I tried using the following code in RectTest.java, but got compile-time errors:

    DrawableRect dr1 = new DrawableRect(1,1,2,3);
    dr1.draw(java.awt.Graphics g);

    I'm wondering if I'm setting up the DrawableRect object correctly and what I need to do to declare/define the parameter of type Graphics that gets passed to this draw method.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    Assuming you want to draw to the screen, you should create a subclass of JComponent and override the paintComponent() method. paintComponent() takes a single parameter of type Graphics, which is provided for you.
    Code:
    class MyComponent extends JComponent {
    ...
        protected void paintComponent(Graphics g) {
            dr1.draw(g);
        }
    }
    You will then need to create an instance of MyComponent and add it to a JFrame's content pane.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Central New Jersey
    Posts
    207
    Rep Power
    22
    Thanks rossM. The GUI terms are slightly familiar. I think I'm going to quickly finish up this chapter and move on to the GUI chapter next.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2

    I'm having a similar problem


    I have three classes. What I'm confused about is how to call the draw method from one of them based on what I selected in my menu. The heading is:
    public void draw(Canvas canvas, Paint paint)

    How do I write my code so that it calls that method?

    The onDraw method from LineDrawView.java(my view I will be drawing on):
    Code:
    protected void onDraw(Canvas canvas) 
    	{
    		
    		
    	switch(drawType){
    	case 1:
    		MyLine ml1 = new MyLine (startX, startY, endX, endY, MyLine.color);
    		ml1.draw(canvas, MyLine.paint);
    		MainActivity ma = new MainActivity();
    		ma.ShapeArray.add(ml1);
    		break;
    	case 2:
    		MyCircle mc1 = new MyCircle (startX, startY, endX, endY, MyCircle.color);
    		mc1.draw(canvas, MyCircle.paint);
    		MainActivity ma2 = new MainActivity();
    		ma2.ShapeArray.add(mc1);
    	break;
    	case 3:
    		MyRectangle mr1 = new MyRectangle (startX, startY, endX, endY, MyRectangle.color);
    		mr1.draw(canvas, MyRectangle.paint);
    		MainActivity ma3 = new MainActivity();
    		ma3.ShapeArray.add(mr1);
    	break;
    	
    	}
    Last edited by darthvader45; April 26th, 2013 at 12:05 PM. Reason: Make my question less confusing.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    how to call the draw method
    Code:
    public void draw(Canvas canvas, Paint paint)
    You need to have a reference to a Canvas class object and a Paint class object as args to the draw() method to call it.

    BTW This thread was over 9 years old
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2

    Question OK, here's what I have in my classes MyLine, MyCircle, and MyRectangle:


    Originally Posted by NormR
    Code:
    public void draw(Canvas canvas, Paint paint)
    You need to have a reference to a Canvas class object and a Paint class object as args to the draw() method to call it.

    BTW This thread was over 9 years old
    Code:
    public static Canvas canvas;
    	public static Paint paint;
    If that does not create a reference, then I'm not sure what does. How do I create a reference to a Canvas class object?
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    How do I create a reference to a Canvas class object?
    Those statements define references with an initial value of null.
    Now you need to assign values to those references. Has an instance of those classes been created yet? If so, their references need to be copied to those variables.
    Otherwise you can use the new statement to create an object and assign its value to the reference:
    Code:
    SomeClass smCLsRef = new SomeClass();  // create object and set reference
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2

    Question


    Originally Posted by NormR
    Those statements define references with an initial value of null.
    Now you need to assign values to those references. Has an instance of those classes been created yet? If so, their references need to be copied to those variables.
    Otherwise you can use the new statement to create an object and assign its value to the reference:
    Code:
    SomeClass smCLsRef = new SomeClass();  // create object and set reference
    As in, an actual class file for Canvas needs to be created? That's built in, isn't it? So if I use this:
    Canvas canvas = new Canvas(); and Paint paint = new Paint(); in MyLine, MyCircle and MyRectangle, it should work, right?
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    It all depends on what the requirements are in the draw() method. What does the draw() method use those objects for? Will the ones you create provide what is needed?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by NormR
    It all depends on what the requirements are in the draw() method. What does the draw() method use those objects for? Will the ones you create provide what is needed?
    I use the draw method to draw the selected shape when I select it in my menu. I have the menu working, I just need to make sure that it eventually calls the draw method I need(If I go into the menu and select an option I made(i.e. Line), it goes to the appropriate class(i.e. MyLine if I select Line) and calls the draw method there.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Without seeing a complete program it is hard to say what the code needs to do.
    Can you make a small, complete program that compiles, executes and shows what you are trying to do?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by NormR
    Without seeing a complete program it is hard to say what the code needs to do.
    Can you make a small, complete program that compiles, executes and shows what you are trying to do?
    Here's all my code, each class(MyLine, MyCircle, and MyRectangle) contains a draw method, which I need to call:

    LineDrawView.java

    Code:
    // Project:			Java2Final
    // File:			LineDrawView.java
    // Date:			4/16/13
    // Description:		Final Exam
    //
    package com.lcc.java2final_lefelhocz;
    
    
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.MotionEvent;
    import android.view.View;
    
    @SuppressLint("DrawAllocation")
    public class LineDrawView extends View 
    {
    	private float currentX;
    	private float currentY;
    	private float startX=0;
    	private float startY=0;
    	
    	
    	private int drawType = 3;
    	private Paint paintStroke;
    		
    	public LineDrawView(Context context) 
    	{
    		// call the super class constructor
    		super(context);
    		
    		// The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
    		// For efficiency create the paint objects in the constructor, not in draw
    		// paint.setStrokeWidth(10); // works on lines
    		// You can change the color of Paint without effecting objects already drawn
    		// You can NOT change the style of Paint without effecting objects already drawn
    		// The Style, TextSize apply to all objects drawn with the paint.
    		
    		new Paint();
    		
    		// added for lab12
    		// Create a Paint Object with Style=Stroke
    		paintStroke = new Paint();
    		paintStroke.setStyle(Paint.Style.STROKE);
    		
    		// set the background color when the view is created
    		this.setBackgroundColor(Color.LTGRAY);	
    	}
    
    	
    	@Override
    	@SuppressLint("DrawAllocation")
    	protected void onDraw(Canvas canvas)
    	{
    		// Draw a Red Diagonal line from upper left corner to bottom right corner
    		
    		
    		// added for lab12
    		switch(drawType){
    		case 1:
    			@SuppressLint("DrawAllocation")
    			MyLine ml1 = new MyLine (startX, startY, currentX, currentY, MyLine.color);
    			ml1.draw(MyLine.canvas, MyLine.paint);
    			
    			return;
    		case 2:
    			
    			
    		
    			return;
    			
    		case 3:
    			
    		
    			
    	}
    	
    	@Override
    	public boolean onTouchEvent(MotionEvent event)
    	{
    		// get the current X and Y position from the event
    		currentX = event.getX();
    	    currentY = event.getY();
    	    
    	    // switch on the touch event action
    		switch(event.getAction())
    		{
    		case MotionEvent.ACTION_DOWN:
    			
    			// start the line
    			startX = currentX;
    			startY = currentY;
    			invalidate();
    			return true;
    		case MotionEvent.ACTION_UP:
    			
    			invalidate();
    			return true;
    		case MotionEvent.ACTION_MOVE:
    			
                
    			invalidate();
    			return true;
    		default:
    			return super.onTouchEvent(event);
    		}
    	}
    	
    	// Called back when the view is first created or its size changes.
    	@Override
    	public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) 
    	{
    	}
    	
    	// added for lab 12
    	public void setDrawType(int aDrawType)
    	{
    		drawType = aDrawType;
    	}
    }
    MainActivity.java:
    Code:
    // Project:			JavaPaintProgram
    // File:			MainActivity.java
    // Date:			4/9/13
    // Author:			Joshua Lefelhocz
    // Description:		Main Activity displays a custom view to draw lines on
    
    package com.lcc.java2final_lefelhocz;
    
    
    
    import java.util.ArrayList;
    
    import com.lcc.java2final_lefelhocz.R;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class MainActivity extends Activity 
    {
    	private LineDrawView myView;
    	ArrayList<MyDrawable> ShapeArray;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		// this line not needed since we are using are own View to draw on
    //		setContentView(R.layout.activity_main);
    		
    		// create the LineDrawView object
    		myView = new LineDrawView(this);
    		
    		// set the content view to it
    		setContentView(myView);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) 
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.activity_main, menu);
    		return true;
    	}
    	
    	public boolean onOptionsItemSelected(MenuItem item)
    	{
    		switch(item.getItemId())
    		{
    		case R.id.Line:
    			myView.setDrawType(1);
    			return true;
    		case R.id.Circle:
    			myView.setDrawType(2);
    			return true;
    		case R.id.Rectangle:
    			myView.setDrawType(3);
    			
    		return true;	
    		default:
    			return super.onOptionsItemSelected(item);
    		}
    	}
    		
    		public boolean onOptionsItemSelected2(MenuItem item)
    		{
    			switch(item.getItemId())
    			{
    			case R.id.Red:
    				MyLine.color = Color.RED;
    				MyCircle.color = Color.RED;
    				MyRectangle.color = Color.RED;
    				return true;
    			case R.id.Green:
    				MyLine.color = Color.GREEN;
    				MyCircle.color = Color.GREEN;
    				MyRectangle.color = Color.GREEN;
    				return true;
    			case R.id.Blue:
    				MyLine.color = Color.BLUE;
    				MyCircle.color = Color.BLUE;
    				MyRectangle.color = Color.BLUE;
    				
    			return true;	
    			case R.id.Magenta:
    				MyLine.color = Color.MAGENTA;
    				MyCircle.color = Color.MAGENTA;
    				MyRectangle.color = Color.MAGENTA;
    				
    			default:
    				return super.onOptionsItemSelected(item);
    			}
    	}
    
    }
    MyCircle.java:
    Code:
    package com.lcc.java2final_lefelhocz;
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    
    public class MyCircle extends MyDrawable {
    	static Canvas canvas = new Canvas();
    	static Paint paint = new Paint();
    	private float startX;
    	private float startY;
    	private float endX;
    	private float endY;
    	static int color  = Color.BLACK;
    
    
    	public MyCircle(float aStartX, float aStartY, float aStopX, float aStopY, int aColor)
    	{
    		startX = aStartX;
    		startY = aStartY;
    		endX = aStopX;
    		endY = aStopY;
    		color = aColor;
    	}
    		public void draw(Canvas canvas, Paint paint) {
    			// TODO Auto-generated method stub
    			switch (color){
    
    			}
    	canvas.drawCircle(startX, endX, (float) Math.sqrt((Math.pow(endX - startX,2) + Math.pow(endY - startY, 2))), paint);
    		}
    
    	}
    MyDrawable.java:
    Code:
    package com.lcc.java2final_lefelhocz;
    
    import android.graphics.Canvas;
    import android.graphics.Paint;
    
    public abstract class MyDrawable {
    public abstract void draw (Canvas canvas, Paint paint);
    }
    MyLine.java:
    Code:
    package com.lcc.java2final_lefelhocz;
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    
    public class MyLine extends MyDrawable {
    float startX;
    float startY;
    float stopX;
    float stopY;
    static int color  = Color.BLACK;
    static Canvas canvas = new Canvas();
    static Paint paint = new Paint();
    
    public MyLine(float aStartX, float aStartY, float aStopX, float aStopY, int aColor)
    {
    	startX = aStartX;
    	startY = aStartY;
    	stopX = aStopX;
    	stopY = aStopY;
        color = aColor;
    }
    
    	public  void draw(Canvas canvas, Paint paint) {
    		
    		// TODO Auto-generated method stub
    
    canvas.drawLine(startX, startY, stopX, stopY, paint);
    	}
    	
    }
    MyRectangle.java:

    Code:
    package com.lcc.java2final_lefelhocz;
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    
    public class MyRectangle extends MyDrawable {
    
    private float startX;
    private float startY;
    private float stopX;
    private float stopY;
    static int color  = Color.BLACK;
    static Canvas canvas = new Canvas();
    static Paint paint = new Paint();
    
    public MyRectangle(float aStartX, float aStartY, float aStopX, float aStopY, int aColor)
    {
    	startX = aStartX;
    	startY = aStartY;
    	stopX = aStopX;
    	stopY = aStopY;
    	color = aColor;
    }
    	@Override
    	public void draw(Canvas canvas, Paint paint) {
    		// TODO Auto-generated method stub
    		
    canvas.drawRect(startX, startY, stopX, stopY, paint);
    	}
    
    }
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Those are android packages. I have no way to compile and test them.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    2

    Question


    Originally Posted by NormR
    Those are android packages. I have no way to compile and test them.
    There's an Android plugin for Eclipse available for download @ http://developer.android.com/sdk/installing/bundle.html
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Maybe I get started with Android later this summer.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo