Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.awt.Graphics cannot be applied

843810Jan 6 2006 — edited Jan 10 2006
Hi I'm creating a app that draws shapes and bounces them around the screen. I've followed and they had draw/paint methods to set colours/fills and draws the shape. the code below is not the whole app just the parts that is giving me trouble. I'm getting a draw(java.awt.Graphics) cannot be applied to () in both my Rectangle and Circle classes and I don't know why. : (
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Color;

public abstract class Shape{

	public String shapeName, shapeColour;
	public double xValue, yValue, sizeValue1, sizeValue2;
	public boolean fillCheck;
	public Ellipse2D.Double cir;
	public Rectangle2D.Double rec;
	public Point2D.Double p;
	public int sWidth;
	public int sHeight;
	public int xV,yV;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public Shape(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled)
	{
		shapeName = shape;
		shapeColour = colour;
		xValue = (double)xPos;
		yValue = (double)yPos;
		sizeValue1 = (double)size1;
		sizeValue2 = (double)size2;
		fillCheck = filled;
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void setDirection()
	{
		int dir = (int)(8*Math.random());
		
		switch(dir)
		{
			case 1:
			xV=0; yV=-1;
			break;
			
			case 2:
			xV=1; yV=-1;
			break;
			
			case 3:
			xV=1; yV=0;
			break;
			
			case 4:
			xV=1; yV=1;
			break;
			
			case 5:
			xV=0; yV=1;
			break;
			
			case 6:
			xV=-1; yV=1;
			break;
			
			case 7:
			xV=-1; yV=0;
			break;
			
			case 8:
			xV=-1; yV=-1;
			break;
		}
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//public abstract void createShape();
	//public abstract void move(int xPos, int yPos, int screenW, int screenH);//not using cos it created can't override errors
	//public abstract void drawShape();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}//end of Shape class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.*;
import java.awt.Point;

public class Rectangle extends Shape{
	
	int xV,yV; // shape velcoties
	
	public Rectangle(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled){
		super(shape, colour, xPos, yPos, size1, size2, filled);
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void createShape(){
	
		p = new Point2D.Double(xValue, yValue);
		double width = sizeValue1;
		double height = sizeValue2;
		rec = new Rectangle2D.Double(p.x, p.y, width, height);
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void move(int xPos, int yPos, int screenW, int screenH){
		xValue = (double)xPos;
		yValue = (double)yPos;
		sWidth = screenW;
		sHeight = screenH;
		
		if(xPos<=0 || (xPos >= sWidth-sizeValue1))
			{xV=-xV;}
		else if(yPos<=0 || (yPos >= sHeight-sizeValue2))
			{xV=-xV;}
		
		//rect.setPosition(xPos+xV, yPos+yV);
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void draw(Graphics g){
		
		Graphics2D g2D = (Graphics2D)g;
		
		System.out.println("Colour is set to "+shapeColour);
		
		if(shapeColour.equals("red")){
		g2D.setColor(Color.red);
		}
		else if(shapeColour.equals("blue")){
		g2D.setColor(Color.BLUE);
		}
		else if(shapeColour.equals("green")){
		g2D.setColor(Color.GREEN);
		}
		else if(shapeColour.equals("yellow")){
		g2D.setColor(Color.YELLOW);
		}
		
		if(fillCheck == true)
			{System.out.println("The shape will be filled");g2D.fill(rec);}
		
		System.out.println("Drawing...");
		g2D.draw(rec);
		System.out.println("Done");
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}//end of Rectangle class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.*;
import java.awt.Point;

public class Circle extends Shape{
	public Circle(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled){
		super(shape, colour, xPos, yPos, size1, size2, filled);// send to shape super class
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void createShape(){
		p = new Point2D.Double(xValue, yValue);// create a new point type double
		double width = sizeValue1;// set width
		double height = sizeValue2;// and height values type double
		cir = new Ellipse2D.Double(p.x, p.y, width, height);// create new ellipse
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void move(int xPos, int yPos, int screenW, int screenH){
		xValue = (double)xPos;// cast from int to double
		yValue = (double)yPos;
		sWidth = screenW;
		sHeight = screenH;
		
		if(xPos<=0 || (xPos >= sWidth-sizeValue1))// if the shape is at the left or right of the screen
			{xV = -xV;}			// set xV to negative (--=+)
		else if(yPos<=0 || (yPos >= sHeight-sizeValue2))// if the shape is at the top or bottom of the screen
			{xV=-xV;}				// set yV to negative (--=+)
		
		//cir.setPosition(xPos+xV, yPos+yV)
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void draw(Graphics g){
		
		Graphics2D g2D = (Graphics2D)g;//cast graphics to graphics2D
		
		System.out.println("Colour is set to "+shapeColour);// debugger
		
		if(shapeColour.equals("red")){// if sting equals ...
		g2D.setColor(Color.red);//set to colour ...
		}
		else if(shapeColour.equals("blue")){
		g2D.setColor(Color.BLUE);
		}
		else if(shapeColour.equals("green")){
		g2D.setColor(Color.GREEN);
		}
		else if(shapeColour.equals("yellow")){
		g2D.setColor(Color.YELLOW);
		}
		
		if(fillCheck == true)// check if fill is true
			{System.out.println("The shape will be filled");//debugger
			g2D.fill(cir);}// fill the shape
		
		System.out.println("Drawing...");//debugger
		g2D.draw(cir);// then draw the shape
		System.out.println("Done");//debugger
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}//ens of Circle class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.*;
import java.awt.Point;

class View extends JPanel{
	
	public int screenW = getSize().width;
	public int screenH = getSize().height;
	public boolean stop;
	public static Rectangle rect;
	public static Circle circ;
	public static int j;
	
	public View(Bounce theApp){
		this.theApp = theApp;
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static void createShapes()
	{	
		for(j=0; j<5; j++)
		{	
			if(Bounce.shape[j][0] == null){break;}
			
			if(Bounce.shape[j][0].equals("rectangle")){
				System.out.println(Bounce.shape[j][0]);
				rect = new Rectangle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);
				rect.createShape();
				rect.draw();
			}
			
			if(Bounce.shape[j][0].equals("circle")){
				System.out.println(Bounce.shape[j][0]);
				circ = new Circle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);
				circ.createShape();
				circ.draw();
			}
		}
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void bounce()
	{	stop = false;		
		
		while(stop == false)
		{
			if(Bounce.position[j][0]==0)
				j=0;
			
			rect.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);
			circ.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);
			repaint();
			j++;
		}
		
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private static Bounce theApp;
}//end of View class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This has been bugging me for a couple of days so hope someone can help. thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2006
Added on Jan 6 2006
8 comments
860 views