-1

here is my code im trying to put a image but every time, i have an error in the getimage() part. Can you please help? Thank you. I know it's a weird project. Or can you tell me how to write a code for drawing a circle and dividing them to pieces. Every piece is a different color then i put them in a linked list and ask the user which color they hate and then i remove it. I do the same thing every time until i only have one color left. I don't know how to do that so i did it a different way so can you help me:

import java.util.*;
import java.util.Scanner;
import javax.swing.*;
import java.applet.*; 
import java.awt.*; 
public class ExtraCredit extends Applet
{
//graphic
public void paintOr(Graphics g) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "Or.png");
  tr.addImage(img,0);
  g.drawImage(img, 0, 0, this);
}
public void paintRed(Graphics g1) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "red.png");
  tr.addImage(img,0);
  g1.drawImage(img, 0, 0, this);
}

public void paintR(Graphics g2) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "R.png");
  tr.addImage(img,0);
  g2.drawImage(img, 0, 0, this);
}
public void paintRBrown(Graphics g3) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "rbrown.png");
  tr.addImage(img,0);
  g3.drawImage(img, 0, 0, this);
}
public void paintBr(Graphics g4) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "Br.png");
  tr.addImage(img,0);
  g4.drawImage(img, 0, 0, this);
}

public void paintBrBlue(Graphics g5) 
{
Image img;
  MediaTracker tr;
 tr = new MediaTracker(this);
  img = getImage(getCodeBase(), "brblue.png");
  tr.addImage(img,0);
  g5.drawImage(img, 0, 0, this);
}

     //----

   public static void main(String[] args)
   {
       ExtraCredit ex = new ExtraCredit();

      String cF;
       int i=0;
        LinkedList<String> c = new LinkedList<String>();
        c.add("red");
        c.add("brown");
        c.add("blue");
        c.add("green");
   Scanner scan = new Scanner (System.in);
   //first loop
   while(i!=4)
   {
   i++;
    System.out.println ("You have a list of color:brown, blue, green, red"); 
    Graphics g = ex.getGraphics();

  ex.paintOr(g);
    System.out.println ("which color you most hate:"); 
    String color1 = scan.nextLine();
     //cF=c.getFirst();

    if(color1 == c.getFirst())
    {
        Graphics g1 = ex.getGraphics();
  ex.paintRed(g1);
    }

    c.remove(color1);
    }
    i=0;

    // second loop
    while(i!=3)
    {
   i++;
    System.out.println ("You have a list of color:"+ c); 
    Graphics g2 =ex.getGraphics();
  ex.paintR(g2);
    System.out.println ("which color you most hate:"); 
    String color2 = scan.nextLine();
    if(color2 == c.getFirst())
    {
        Graphics g3 = ex.getGraphics();
  ex.paintRBrown(g3);
    }


    c.remove(color2);
    }

    i=0;
    //third loop
     while(i!=2)
    {
   i++;
    System.out.println ("You have a list of color:"+ c); 
    Graphics g4=ex.getGraphics();
  ex.paintBr(g4);
    System.out.println ("which color you most hate:"); 
    String color3 = scan.nextLine();
    cF=c.getFirst();
    if(color3 == c.getFirst())
    {
        System.out.println ("The color you like:" + c);
      Graphics g5 = ex.getGraphics();
  ex.paintBrBlue(g5);  
    }


    c.remove(color3);
    }



}

}
vijay
  • 2,646
  • 2
  • 23
  • 37
user2022871
  • 111
  • 1
  • 1
  • 5
  • 1
    Do you intend running this as an applet, an application, or both? – Andrew Thompson Feb 06 '13 at 00:03
  • 1
    Start by providing us with more details about the error. Update to the 21st Century and use `JAppelt` instead of `Applet`. Have a read through [Perform Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) and [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/index.html) – MadProgrammer Feb 06 '13 at 00:07

1 Answers1

2

This ex.getGraphics() is a really bad idea. This is NOT how painting is done in AWT or Swing.

Mixing command line input and UI elements is never a good idea, add in the fact that you are using a Applet and your user losses the ability to actually provide feedback...

AWT/Swing are event driven environments. Events are created from (mostly) mouse and keyboard inputs.

Take the time to read through Creating a GUI with Swing for details.

Don't forget to also take a look at Performing Custom Painting and 2D Graphics, in particular, the section on shapes.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366