试图创建三个类并绘制蜡笔框。如何在构造函数中绘制蜡笔?

时间:2015-05-10 06:23:08

标签: java user-interface jframe paint jcomponent

说明是创建具有高度,位置和颜色的蜡笔对象。该对象还应绘制蜡笔。该程序应该拥有并拥有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);
    }
}

1 个答案:

答案 0 :(得分:2)

我保留了您的Crayon课程和CrayonBoxView课程。在CrayonBoxComponent课程内,我做了这个

&#13;
&#13;
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);
	  }
  }
 
}
&#13;
&#13;
&#13;

编辑:你也应该改变蜡笔矩形的高度和宽度,使它们看起来更像蜡笔

EDIT2:我不小心错误地命名了一个我打电话的方法。我将之前的paint方法重命名为print并修复了该方法中的错误。