-1

这是我试图放置图像的代码,但每次我在 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);
    }



}

}
4

1 回答 1

2

ex.getGraphics()是一个非常糟糕的主意。这不是在 AWT 或 Swing 中绘制的方式。

混合命令行输入和 UI 元素从来都不是一个好主意,再加上您正在使用 aApplet并且您的用户失去了实际提供反馈的能力......

AWT/Swing 是事件驱动的环境。事件是从(主要)鼠标和键盘输入创建的。

花点时间阅读使用 Swing 创建 GUI 以了解详细信息。

别忘了还看看Performing Custom Painting2D Graphics,特别是关于形状的部分。

于 2013-02-06T00:21:58.707 回答