如何在处理中使用类中的类?

7qhs6swi  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(188)

例子
我需要创建在处理上述草图。我遵循了一个描述性的教程,除了我不能使按钮遵循它们的样式之外,其他的一切都正常工作,正如教程中所说:“修改button类中的display函数,使用button的文本标签,从stylecollection中获取与该名称相关联的样式,并使用background,fill,笔划和文本大小从该样式到样式按钮使用这些设置。现在在画布上显示按钮。更新绘图功能以显示按钮。“
我的版本如何修改button类并从其他类获取输入?

String message;
String[] styleNames={"dark","light","red","blue"};
StyleCollection styles;
Style currentStyle;
Button[] buttons= new Button[4];
float bx;

void setupButtons(){
  for(int index=0;index<styleNames.length;index++){
    buttons[index] = new Button (bx, 500);
    buttons[index].setLabel(styleNames[index]);
    bx=bx+150;
  }
}

void setupStyles(){
  styles = new StyleCollection();

  Style darkStyle= new Style("dark",0,255,255,15);
  styles.addStyle(darkStyle);
  Style lightStyle= new Style("light",255,0,0,15);
  styles.addStyle(lightStyle);
  Style redStyle= new Style("red",#D69797,#FF0000,#FF0000,15);
  styles.addStyle(redStyle);
  Style blueStyle= new Style("blue",#8788EA,#0000FF,#0000FF,15);
  styles.addStyle(blueStyle);  
  styles.setDefaultStyle(lightStyle);
  currentStyle=styles.getDefaultStyle(); 
}

void setup(){
  size(800,600);
  setupStyles();
   bx=150;
  setupButtons();

  message = ("Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
  "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, "+
  "when an unknown printer took a galley of type and scrambled it to make a type "+
  "specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, "+
  "remaining essentially unchanged. It was popularised in the 1960s with the release of "+
  "Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing "+
  "software like Aldus PageMaker including versions of Lorem Ipsum.");
}

void draw(){
  background(currentStyle.getBackground());

  fill(currentStyle.getFillColor());
  textSize(currentStyle.getTextSize());
  textAlign(LEFT);
  text(message,100,100, width/2,height);

  for (int i=0; i<buttons.length;i++){
  buttons[i].display();
  }

}

void mousePressed(){
  if (buttons[0].isInside(mouseX,mouseY)){
    String sw = buttons[0].getText();
    currentStyle=styles.getStyle(sw);
  }
  else if (buttons[1].isInside(mouseX,mouseY)){
    String sw = buttons[1].getText();
    currentStyle=styles.getStyle(sw);
  }
  else if (buttons[2].isInside(mouseX,mouseY)){
    String sw = buttons[2].getText();
    currentStyle=styles.getStyle(sw);
  }  else if (buttons[3].isInside(mouseX,mouseY)){
    String sw = buttons[3].getText();
    currentStyle=styles.getStyle(sw);
  }  

}
class Style{
  private int fillColor;
  private int strokeColor;
  private int bgColor;
  private int textSize;
  private String name;

  public Style(String nm, int bgCol, int strCol, int fC, int tSize){
    name=nm;
    bgColor=bgCol;
    strokeColor=strCol;
    fillColor=fC;
    textSize=tSize;
  }

  public String getSName(){ return name;}
  public int getBackground(){return bgColor;}
  public int getFillColor(){return fillColor;}
  public int getStrokeColor(){return strokeColor;}
  public int getTextSize(){return textSize;}
  public String toString(){return name+ " "+bgColor;}

}
import java.util.HashMap;
class StyleCollection{
  private HashMap <String,Style> styles;

  public StyleCollection(){  
    styles= new HashMap <String, Style>();
  }

  public void addStyle(Style s){
    styles.put(s.getSName(),s);
  }
  public void setDefaultStyle(Style sD){
    styles.put("default", sD);
  }
  public Style getStyle(String stname){
    return styles.get(stname);
  }

  public Style getDefaultStyle(){
    return styles.get("default");
  }

}
class Button{
  private float x,y,w,h;
  private String label;
  private StyleCollection sc;
  private Style s;

  public Button(float _x, float _y){
    x=_x;
    y=_y;
    w=100;
    h=75;
    sc= new StyleCollection();
  }
  public void display(){
    s=sc.getStyle(getText());

    fill(255);
    rect(x,y,w,h);
    textAlign(CENTER);
    fill(0);
    textSize(15);
    text(getText(),x+w/2,y+h/2);
  }
  public void setLabel(String _label){
    label=_label;
  }
  public String getText(){return label;}

  boolean isInside(float _mx, float _my){
    float mx,my;
    mx=_mx;
    my=_my;
    if((mx>x)&&(mx<x+w)&&(my>y)&&(my<y+h)){
      return true;
    }
    else{
      return false;
    }
  }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题