如何创建使用多个类来创建图形的单个绘图面板

时间:2015-03-17 01:57:45

标签: java class graphics

我正在寻找一个起点,而不是一个答案,更多的是试图理解这个概念。如何将所有窗口打开到只有一个面板/窗口?

创建FacebookPerson

public class FacebookPerson{

private String myName;
protected String myMood;
protected Facebook myfacebook;

public FacebookPerson(String name){
  myName = name;
  myfacebook = new Facebook(myName);
  //System.out.println("FacebookPerson_Graphics's constructor");
}

public FacebookPerson(){

}

public String getName(){
  return myName;
}

public void setMood(String newMood){
 myMood = newMood;
 myfacebook.setContent(myMood);
}

public String getMood(){
  return myMood;
}

}

这是创建和编辑的代码:

package facebook;

import java.awt.*;

public class Facebook{

private String name;
private String content;
DrawingPanel panel;
private Graphics g;

public Facebook(String nm){
   content = "undefined";
   name = nm;

   // Create the drawing panel
   panel =new DrawingPanel(200,150);
   g = panel.getGraphics();
   // display name
   g.drawString(name+"'s mood is undefined.", 20, 75);
  }

public void setContent(String newContent){
content = newContent;
    if(content.equals("happy")){
        g.setColor(Color.red);
        g.fillRect(0, 0, 200, 150);
        g.setColor(Color.black);
        // display mood
        g.drawString(name+"'s mood is:"+ "happy", 20, 75);
    }
    else{
        g.setColor(Color.white);
        g.fillRect(0, 0, 200, 150);
        g.setColor(Color.black);
        g.drawString(name+"'s mood is:"+ content, 20, 75);
    }
 }

      public String getContent(){
        return content;
  }



}

创建用户界面以实现这两个类:

 package facebook;

 import java.util.*;

 public class testFacebook{


 public static void main (String[] args){

// Prompt user to enter the number of facebookpresons
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the number of facebookpresons to be created: ");
int numP=0;
while(true){
    try{
        numP = userInput.nextInt();
        userInput.nextLine();
        if(numP>0 && numP<=9)  // accept the number if it is within range. Here we define the range to be from 1 to 5. 
            break;
        else
            System.out.println("the number is out of range [1, 9]! enter again");
    } catch (InputMismatchException e){
        System.out.println("invalid input. Enter an integer number!");
        userInput.nextLine();
    }
}

FacebookPerson[] fbp = new FacebookPerson[numP];

//Ask the user to enter the name for each person, and create the persons
for(int i=0; i< numP; i++){
    System.out.println("Enter the name for person "+ (i+1)+ ":");
    String name = userInput.nextLine();
    fbp[i] = new FacebookPerson(name);
}
System.out.println("-------select a person and type the mood below--------");

//Ask the user to set the mood for a person, and update the mood, enter "####" to exit
while(true){
    System.out.println("Enter the name for a person (enter #### to exit):");
    String name = userInput.nextLine();
    if(name.equals("####"))
         System.exit(0);
    int personID = -1;
    for(int i=0; i< numP; i++){
        if(fbp[i].getName().equals(name)){
            personID = i;
            break;  // break the for loop
        }
    }
    if(personID!=-1){  // found the person, otherwise personID should still be -1
        System.out.println("Enter the mood for the person:");
        String mood = userInput.nextLine();
        fbp[personID].setMood(mood);
    }
    else
        System.out.println("unrecognized name!");
} // end while

} // end main

}

1 个答案:

答案 0 :(得分:0)

让我们从不做g = panel.getGraphics();开始,这不是自定义绘画的工作原理。请查看Painting in AWT and SwingPerforming Custom Painting,了解有关绘画应如何完成的详细信息。

首先定义一个“可绘制”或“可绘制”的概念(使用一个接口来定义可以绘制的东西的入口点,可以绘制一些可绘制的东西)

public interface Drawable {
    public void draw(Graphics2D g2d, JComponent parent); // Any other parameters that might be useful...
} 

您希望在“可绘制表面”上绘制的任何内容都应实现此界面。

创建一个“可绘制表面”,可以获取Drawable个对象的列表,并绘制它们......

public class DrawablePane extends JPanel {
    private List<Drawable> drawables;

    public DrawablePane() {
        drawables = new ArrayList<>(25);
    }

    public void add(Drawable drawable) {
        drawables.add(drawable);
        repaint();
    }

    public void remove(Drawable drawable) {
        drawables.remove(drawable);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        for (Drawable drawable : drawables) {
            Graphics2D g2d = (Graphics2D)g.create();
            drawable(g2d, this);
            g2d.dispose();
        }
    }
}

作为一个想法...