说明是创建具有高度,位置和颜色的蜡笔对象。该对象还应绘制蜡笔。该程序应具有并容纳8种不同颜色的蜡笔。蜡笔将在ArrayList中提供帮助。我们需要三个类:CrayonCrayonBoxComponentCrayonBoxViewer。我的课程根本无法正常工作,我不确定如何连接这三个课程。最重要的是,我不知道如何在蜡笔对象中绘制蜡笔。另外,蜡笔应该使用loop绘制,我也不知道该怎么做。谢谢!

import java.awt.*;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Crayon extends JComponent
{
    int loc;
    int h;
    Color myColor;
    private JPanel panel;

    public Crayon(int location, int height, Color input)
    {
        loc=location;
        h=height;
        myColor=input;

    }

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(myColor);
        g.fillRect(loc, 40, 40, h);



    }


}


...

  import java.awt.*;
import java.util.Random;
import javax.swing.JComponent;
import java.util.ArrayList;
class CrayonBoxComponent extends JComponent
{

  public void paint(Graphics g)
  {
    //typecast
    Graphics2D g2 = (Graphics2D) g;
    ArrayList<Crayon> box = new ArrayList<Crayon>();

     Crayon c1=new Crayon(10, 200, Color.RED);
        box.add(c1);
     Crayon c2=new Crayon(60, 200, Color.BLUE);
        box.add(c2);
     Crayon c3=new Crayon(110, 200, Color.CYAN);
        box.add(c3);
     Crayon c4=new Crayon(160, 200, Color.GREEN);
        box.add(c4);
     Crayon c5=new Crayon(210, 200, Color.MAGENTA);
        box.add(c5);
     Crayon c6=new Crayon(260, 200, Color.ORANGE);
        box.add(c6);
     Crayon c7=new Crayon(310, 200, Color.BLACK);
        box.add(c7);
     Crayon c8=new Crayon(360, 200, Color.YELLOW);
        box.add(c8);



    }


}


....

import java.awt.*;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class CrayonBoxViewer
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();

        frame.setSize(1000, 1000);
        frame.setTitle("Crayon Box");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        CrayonBoxComponent component = new CrayonBoxComponent();
        frame.add(component);

        frame.setVisible(true);
    }
}

最佳答案

我保留了您的Crayon类和您的CrayonBoxView类。在CrayonBoxComponent类中,我这样做了



import java.awt.*;
import javax.swing.JComponent;
import java.util.ArrayList;

class CrayonBoxComponent extends JComponent
{
  public ArrayList<Crayon> addCrayons()
  {
	  ArrayList<Crayon> box = new ArrayList<Crayon>();

	  Crayon c1=new Crayon(10, 200, Color.RED);
      box.add(c1);
      Crayon c2=new Crayon(60, 200, Color.BLUE);
      box.add(c2);
      Crayon c3=new Crayon(110, 200, Color.CYAN);
      box.add(c3);
      Crayon c4=new Crayon(160, 200, Color.GREEN);
      box.add(c4);
      Crayon c5=new Crayon(210, 200, Color.MAGENTA);
      box.add(c5);
      Crayon c6=new Crayon(260, 200, Color.ORANGE);
      box.add(c6);
      Crayon c7=new Crayon(310, 200, Color.BLACK);
      box.add(c7);
      Crayon c8=new Crayon(360, 200, Color.YELLOW);
      box.add(c8);

      return box;
  }

  public void print(Graphics g)
  {
	  Graphics2D g2 = (Graphics2D) g;
	  for (int i = 0; i < addCrayons().size(); i++){
		  addCrayons().get(i).paint(g2);
	  }
  }

}





编辑:您还应该更改蜡笔矩形的高度和宽度,以使其看起来更像蜡笔

EDIT2:我不小心错误地命名了我调用的方法。我将以前的paint方法重命名为print并修复了该方法中的错误。

10-06 09:55