Jump to content

My friend needs help (JAVA)

The_Eddie

Hey LTT community, my friend is creating a paint java program, but is haveing a problem with changing the color of the pen. Everytime he changes the color of the pen everything (including the stuff that was painted last also changes color). I'll inclue the code blow, just remember to create a html file if you want to view it properly. I woulld appreciate help, thank you!!!!!!!!!! 

 

 

import java.awt.*;import java.applet.*;import java.awt.event.*;public class PaintProgram extends Applet{ int xcoor[]; int ycoor[]; boolean firstpaint=true; int numberofsquares; Rectangle green, blue,red,orange,gray; int numcolor;  public void init() {  firstpaint=true;  xcoor=new int[907500];  ycoor=new int[907500];  numberofsquares=100;    green= new Rectangle(20,20,80,75);  blue=new Rectangle(20,100,80,75);  red=new Rectangle(20,180,80,75);  orange=new Rectangle(20,260,80,75);  gray=new Rectangle(20,340,80,75);  numcolor=0;   } public void paint(Graphics g) {  g.drawRect(10,10,500,500);  g.drawRect(10,10,100,415);    g.setColor(Color.green);  g.fillRect(20,20,80,75);    g.setColor(Color.blue);  g.fillRect(20,100,80,75);    g.setColor(Color.red);  g.fillRect(20,180,80,75);    g.setColor(Color.orange);  g.fillRect(20,260,80,75);    g.setColor(Color.gray);  g.fillRect(20,340,80,75);//=======================================================  switch(numcolor)  {      case 1:    g.setColor(Color.green);    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);    break;   case 2:    g.setColor(Color.blue);    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);    break;   case 3:    g.setColor(Color.red);    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);    break;   case 4:    g.setColor(Color.orange);    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);    break;   case 5:    g.setColor(Color.gray);    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);    break;  }    /*  if(firstpaint)   firstpaint=false;  else  {   g.setColor(Color.red);   for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5);  };*/ } public boolean mouseDown(Event e, int x, int y) {  if(green.inside(x,y))   numcolor=1;     else if (blue.inside(x,y))   numcolor=2;     else if(red.inside(x,y))   numcolor=3;     else if(orange.inside(x,y))   numcolor=4;     else if(gray.inside(x,y))   numcolor=5;         repaint();  return true; }  public boolean mouseDrag(Event e2, int x, int y) {   if (x<110)  x=110;    if (x>505)  x=505;    if (y>505)  y=505;    if (y<10)  y=10;  xcoor[numberofsquares] = x;  ycoor[numberofsquares] = y;  numberofsquares++;  repaint();  return true; } public void update(Graphics g) {  paint(g); }}
Edited by looney
I have put the code in a code field :)
Link to comment
Share on other sites

Link to post
Share on other sites

Can you use the code tag?

 

Also, he seems to be looping through the squares and setting their colour. It is quite hard to debug unless you post the entire code.

Link to comment
Share on other sites

Link to post
Share on other sites

So just quickly looking through this the reason all of your friend's objects change colors is due to when you are setting the colors.

 

e.g. numColor == 1

    g.setColor(Color.green); //This is setting the color    for(int k=0;k<numberofsquares;k++)    g.fillRect(xcoor[k],ycoor[k],5,5); //This is now looping through all objects created creating the new objects...but you only set the color once which is why they are all drawing the same new color

To fix this you need to remember each color for each object. (This will also change your switch loop)

 

Here is a quick example of the new switch loop

for(int k=0; k<numberofsquares;k++) {  switch(objectColor[k])  {   case 1:    g.setColor(Color.green);    break;   case 2:    g.setColor(Color.blue);    break;   case 3:    g.setColor(Color.red);    break;   case 4:    g.setColor(Color.orange);    break;   case 5:    g.setColor(Color.gray);    break;  }  g.fillRect(xcoor[k],ycoor[k],5,5);}

This assumes you create a new array called objectColor.  The easiest way to add this would be like this

 public boolean mouseDrag(Event e2, int x, int y) {   if (x<110)  x=110;    if (x>505)  x=505;    if (y>505)  y=505;    if (y<10)  y=10;  objectColor[numberofsquares] = numcolor; //This is the added line (also your friend needs to initialize the objectColor just like xcoor  xcoor[numberofsquares] = x;  ycoor[numberofsquares] = y;  numberofsquares++;  repaint();  return true; }

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×