Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with Java based paint program

Started by Ueneth Echil Jan 20, 2007 at 10:59 AM 0 replies 3.2k views
Original Post
Ueneth Echil
Ueneth Echil
In my Computer Science I class we're working on creating our own Microsoft Paintesque program. We've studied inheritance, composition, basic graphics, mouse events, data types, and arrays. We're also not allowed to use any Swing code. Since I'm pretty hyped about this project, I wanted to get a head start and start off. I've ran into some problems, but this one has been sticking for quite some time. What I want to do, is create a class that I can call that will make a new Rectangle and draw it. What that will do is draw the rectangle where I want to in the color it's called. It will also create a abstract Rectangle behind it, an object, so that I can have interaction with the user clicking on that spot. Here are two related snipets of code: CiPaint.java
public void init(Graphics g) 
{
	UI.UI(g,"red",10,10);
		
	/*red = new Rectangle(50,50,100,100);
	green = new Rectangle(50,200,100,100);
	blue = new Rectangle(50,350,100,100);*/
	numColor = 0;
}



UI.java
import java.awt.*;

public class UI
{
	public static void UI(Graphics g, String strColor, int x, int y)
	{
		g.setcolor(Color.strColor);
		
		Rectangle color = strColor;
		color = new Rectangle(x,y,25,25);
	}
}



When I just compile the FILE CiPaint.java I get this error:
CiPaint.java:15: UI(java.lang.String,int,int) in UI cannot be applied to (java.awt.Graphics,java.lang.String,int,int)
                UI.UI(g,"red",10,10);
                  ^



But when I compile the PROJECT or just the FILE UI.java I get these errors:
I.java:7: cannot find symbol
symbol  : variable strColor
location: class java.awt.Color
                g.setcolor(Color.strColor);
                                ^



UI.java:9: incompatible types
found   : java.lang.String
required: java.awt.Rectangle
                Rectangle color = strColor;
                                  ^



CiPaint.java (Full)
// CiPaint
// Version 0.1
// Programmed and Designed by Paul Adamski
 
import java.awt.*;
import java.applet.*;

public class CiPaint extends Applet 
{
	//Rectangle red, green, blue, purple, black;
	int numColor;
	
	public void init(Graphics g) 
	{
		UI.UI(g,"red",10,10);
		
		/*red = new Rectangle(50,50,100,100);
		green = new Rectangle(50,200,100,100);
		blue = new Rectangle(50,350,100,100);*/
		numColor = 0;
	}

	public void paint(Graphics g) 
	{
		/* Draws the borders and UI
		g.drawLine(200,0,200,600);
		g.drawLine(1,1,800,1);
		g.drawLine(800,1,800,600);
		g.drawLine(800,600,1,600);
		g.drawLine(1,600,1,1);
		
		g.setColor(Color.red);
		g.fillRect(10,10,25,25);
		
		g.setColor(Color.green)
		g.fillRect(10)*/
		
		
		/*switch (numColor)
		{
			case 1:
				g.setColor(Color.red);
				g.drawString("Mouse clicked inside red", 200,75);
				break;
			case 2:
				g.setColor(Color.green);
				g.drawString("Mouse clicked inside green", 200,225);
				break;
			case 3:
				g.setColor(Color.blue);
				g.drawString("Mouse clicked inside blue", 200,375);
				break;
			case 4:
				g.setColor(Color.black);
				g.drawString("Mouse is clicked outside the colored squares", 50,20);
				break;
		}*/		
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		/*if(red.inside(x,y))
			numColor = 1;
		else if(green.inside(x,y))
			numColor = 2;
		else if(blue.inside(x,y))
			numColor = 3;
		else
			numColor = 4;
		repaint();*/
		return true;
	}
}



UI.java
import java.awt.*;

public class UI
{
	public static void UI(Graphics g, String strColor, int x, int y)
	{
		g.setcolor(Color.strColor);
		
		Rectangle color = strColor;
		color = new Rectangle(x,y,25,25);
	}
}



Thanks for all your help in advance. If you have any questions, feel free to ask, I should be able to reply within 5-10 minutes. [Edited by - Ueneth Echil on January 20, 2007 3:11:09 PM]
rogierpennink
rogierpennink
The errors you are getting are caused by some elemental type-issues. I'll try to point out what's wrong, and more importantly, why it's wrong.
g.setcolor(Color.strColor);
First of all, the setcolor method of the Graphics class must be spelled with a capital C. So it'd be g.setColor(). Though this probably wouldn't compile, the major problem is in the Color.strColor bit of the code. Putting a dot behind a class name means you are accessing a static member variable or method of that class. You are however, trying to access a member that does not exist (namely, strColor). I understand what you tried to do - by using a string that contains the name of the member you wish to access you were hoping that it'd actually 'read' that string and compile successfully. The compiler, however, does not do that. It will try to find a strColor member in the color class, which doesn't exist so it returns an error. The correct version would be:
g.setColor( Color.RED )
Of course, this doesn't suit your needs since you obviously want to let the color be variable. In this case I'd suggest you either pass an array as argument to that function, containing three integers in the range of 0-255 and then constructing a new Color object with that. You could also create a hashmap (which is basically an associative array) in which you'd 'link up' the strings you pass as arguments and their respective color members. (btw, this was about the error on line 7).

You make the same mistake in appointing the string passed to your UI function to a Rectangle object. What the compiler sees is this: "Create a rectangle object and assign a string to it". This is where the compiler will protest because there are two conflicting types. In order for this to pass the compiler's critical test you'd have to explicitly cast the string to a rectangle but that won't yield the expected results. I understand that you wish to make a rectangle object that has the same name as the string you passed, but that is not possible as far as I know, and the way you implemented it will definately not work.

I probably should have provided more information and possibly even an amount of code as suggestion, but unfortunately it's getting quite late here and I'll have to dive into bed ;) If you have any further questions, don't hesitate to ask though! There are many others who would help you way better than I did, and who, more importantly, are living in another timezone.

Good luck.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.