这是我试图放置图像的代码,但每次我在 getimage() 部分都有错误。你能帮忙吗?谢谢你。我知道这是一个奇怪的项目。或者你能告诉我如何写一个画圆并将它们分成几块的代码。每件都是不同的颜色,然后我把它们放在一个链接列表中,并询问用户他们讨厌哪种颜色,然后我将其删除。我每次都做同样的事情,直到我只剩下一种颜色。我不知道该怎么做,所以我以不同的方式做到了,所以你能帮助我:
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);
}
}
}