Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java程序中修改图形?_Java - Fatal编程技术网

在java程序中修改图形?

在java程序中修改图形?,java,Java,我写了下面的java程序,但我似乎不能让它做我想要的。我想用java制作两个盒子。在这两个框中,都应该有一个绿色框,其中包含一个人名和另一个框,还有一个心情框,当心情愉快时会变成红色,否则会变成灰色。像这样的东西我需要让我的Facebook_Graphics.java 我写了下面的课程,但我能做什么呢 import java.awt.*; import jpb.*; public class Facebook_Graphics{ private String

我写了下面的java程序,但我似乎不能让它做我想要的。我想用java制作两个盒子。在这两个框中,都应该有一个绿色框,其中包含一个人名和另一个框,还有一个心情框,当心情愉快时会变成红色,否则会变成灰色。像这样的东西我需要让我的Facebook_Graphics.java

我写了下面的课程,但我能做什么呢

import java.awt.*;


    import jpb.*;

    public class Facebook_Graphics{
       private String name;
       private String content;
       DrawableFrame df;
       private Graphics g;

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

           // Create drawable frame        
           df = new DrawableFrame(name);
           df.show();
           df.setSize(200, 150);

           // Obtain graphics context
           g = df.getGraphicsContext();

           // display name
           g.drawString(name+"'s mood is undefined.", 20, 75);

           // Repaint frame
           df.repaint();        
       }

       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);
            }
            // Repaint frame
            df.repaint();
       }

       public String getContent(){
        return content;
       }
    }


    public class FacebookPerson_Graphics{
      private String myName;
      private String myMood;
      private Facebook_Graphics myfacebook;

      public FacebookPerson_Graphics(String name){
          myName = name;
          myfacebook = new Facebook_Graphics(myName);
      }

      public String getName(){
          return myName;
      }

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

      public String getMood(){
          return myMood;
      }
    }




    import jpb.*;
    @SuppressWarnings( "deprecation" )
    public class testFacebook_Graphics{        
      public static void main (String[] args){
        // Prompt user to enter the number of facebookpresons
        SimpleIO.prompt("Enter the number of facebookpresons to be created: ");
        String userInput = SimpleIO.readLine();
        int numP = Integer.parseInt(userInput);

        FacebookPerson_Graphics[] fbp = new FacebookPerson_Graphics[numP];

        //Ask the user to enter the name for each person, and create the persons
        for(int i=0; i< numP; i++){
            SimpleIO.prompt("Enter the name for person "+ (i+1)+ ":");
            String name = SimpleIO.readLine();
            fbp[i] = new FacebookPerson_Graphics(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){
            SimpleIO.prompt("Enter the name for a person (enter #### to exit):");
            String name = SimpleIO.readLine();
            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;
                }
            }
            if(personID!=-1){  // found the person
                SimpleIO.prompt("Enter the mood for the person:");
                String mood = SimpleIO.readLine();
                fbp[personID].setMood(mood);
            }
            else
                System.out.println("unrecognized name!");
        } // end while

      } // end main

    }
import java.awt.*;
进口jpb。*;
公共类Facebook_图形{
私有字符串名称;
私有字符串内容;
可拉伸框架df;
私有图形g;
公共Facebook_图形(字符串nm){
content=“未定义”;
名称=纳米;
//创建可拉伸框架
df=新的可拉伸框架(名称);
df.show();
测向设置尺寸(200150);
//获取图形上下文
g=df.getGraphicsContext();
//显示名称
g、 抽绳(名字+“,语气未定义。”,20,75);
//重新粉刷框架
df.repaint();
}
公共void setContent(字符串newContent){
内容=新内容;
如果(内容等于(“快乐”)){
g、 setColor(Color.red);
g、 fillRect(0,0,200,150);
g、 设置颜色(颜色为黑色);
//显示情绪
g、 拉丝(名字+”)的心情是:“+”高兴“,20,75);
}
否则{
g、 setColor(Color.white);
g、 fillRect(0,0,200,150);
g、 设置颜色(颜色为黑色);
g、 抽绳(名字+“)的语气是:“+内容,20,75”;
}
//重新粉刷框架
df.repaint();
}
公共字符串getContent(){
返回内容;
}
}
公共类FacebookPerson\u图形{
私有字符串myName;
私人字符串myMood;
私人脸谱网;
public FacebookPerson_图形(字符串名称){
我的名字=名字;
myfacebook=新的Facebook_图形(myName);
}
公共字符串getName(){
返回我的名字;
}
public void setMood(字符串newMood){
我的心情=新心情;
myfacebook.setContent(myMood);
}
公共字符串getMood(){
还我心情;
}
}
进口jpb。*;
@抑制警告(“弃用”)
公共类测试图形{
公共静态void main(字符串[]args){
//提示用户输入FaceBookPreson的数量
提示符(“输入要创建的FaceBookPreson的数量:”;
字符串userInput=SimpleIO.readLine();
int numP=Integer.parseInt(userInput);
FacebookPerson_Graphics[]fbp=新的FacebookPerson_Graphics[numP];
//要求用户输入每个人的姓名,然后创建这些人
对于(int i=0;i
将您的
Facebook\u Graphics
类更改为以下内容:

public class Facebook_Graphics {
    private String name;
    private String content;
    DrawableFrame df;
    private Graphics g;

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

        // Create drawable frame        
        df = new DrawableFrame(name);
        df.show();
        df.setSize(200, 150);

        // Obtain graphics context
        g = df.getGraphicsContext();
        drawLayout();
        df.repaint();
    }

    public void setContent(String newContent) {
        content = newContent;

        clearGraphics();
        drawLayout();

        // Repaint frame
        df.repaint();
    }
    private void clearGraphics() {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 200, 150);
    }
    private void drawLayout() {
        g.setColor(Color.BLACK);
        g.drawString("Name", 20, 40);
        g.drawString("Mood", 20, 90);

        g.setColor(Color.GREEN);
        g.fillRect(80, 20, 100, 30);

        g.setColor(getMoodColor());
        g.fillRect(80, 70, 100, 30);

        g.setColor(Color.BLACK);
        g.drawString(name, 90, 40);
        g.drawString(content, 90, 90);
    }

    private Color getMoodColor() {
        return "happy".equals(content) ? Color.RED : Color.GRAY;
    }

    public String getContent() {
        return content;
    }
}

将您的
Facebook\u Graphics
类更改为以下内容:

public class Facebook_Graphics {
    private String name;
    private String content;
    DrawableFrame df;
    private Graphics g;

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

        // Create drawable frame        
        df = new DrawableFrame(name);
        df.show();
        df.setSize(200, 150);

        // Obtain graphics context
        g = df.getGraphicsContext();
        drawLayout();
        df.repaint();
    }

    public void setContent(String newContent) {
        content = newContent;

        clearGraphics();
        drawLayout();

        // Repaint frame
        df.repaint();
    }
    private void clearGraphics() {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 200, 150);
    }
    private void drawLayout() {
        g.setColor(Color.BLACK);
        g.drawString("Name", 20, 40);
        g.drawString("Mood", 20, 90);

        g.setColor(Color.GREEN);
        g.fillRect(80, 20, 100, 30);

        g.setColor(getMoodColor());
        g.fillRect(80, 70, 100, 30);

        g.setColor(Color.BLACK);
        g.drawString(name, 90, 40);
        g.drawString(content, 90, 90);
    }

    private Color getMoodColor() {
        return "happy".equals(content) ? Color.RED : Color.GRAY;
    }

    public String getContent() {
        return content;
    }
}

问题是什么?哦,我的问题是我试图让它看起来像上面的图片。但是我的程序在我运行它时,我输入了一个名字,它在框的中间有名字,快乐是红色填充的,但是我不知道如何使它看起来像上面。我不知道如何使我的FACEHOBooGooCAD类这样做。\什么是代码> Dabable Frase<代码>?它是从哪里来的,你在使用什么API?我在使用一个KN King java编程的可绘制框架,问题是什么?哦,我的问题是,我试图让它看起来像上面的图片。但是我的程序在我运行它时,我输入了一个名字,它在框的中间有名字,快乐是红色填充的,但是我不知道如何使它看起来像上面。我不知道如何使我的FACEHOBooGooCAD类这样做。\什么是代码> Dabable Frase<代码>?它来自哪里,您使用的是什么API?我使用的是来自KN King java编程的可绘制框架