Applet,drawpolygon代替String特殊字符

时间:2013-02-08 14:01:33

标签: java applet polygon

我没有长时间学习Java,所以原谅我,但是我被要求制作一个applet,它的一个基本功能是允许用户进入文本框并让applet显示什么他们写的,在我的2个首字母(G,P)的任何情况下,程序应该不是写这个字符,而是填充它的多边形。

显然我不能每次在同一个coord中创建这个多边形,所以我使用了一个我认为应该工作的方法,即为第一个点创建变量然后让applet接收角色的坐标(通过一个for循环),然后显示多边形,我遇到的主要问题是多边形是一个单独的函数,它一直说标识符预期,摆脱这个我改变了变量xpoints,ypoints和npoints初始化的地方他们不再是全球性的。但现在我得到60个错误说不是声明(在多边形函数中)和“;”预期。任何帮助??

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.font.*;
import java.io.*;
import java.util.regex.*;
import java.awt.color.*;

import java.net.*;
import java.awt.geom.*; 

public class assignment extends Applet implements MouseListener,ActionListener {
    int gx;
    int gy;
    int px;
    int py;
    char c;
    int i;

    String replaced;
    char sl1='G';
    String sl2="P";
    String pr_name;
    String pr_styleType;
    Label pr_label;
    TextField pr_text;
    TextField pr_style;

    Label pr_textInput;
    Label pr_textStyle;

    public void init(){
        pr_label= new Label ("Enter text you would like to display!");
        add(pr_label);

    pr_textInput= new Label ("Text:");
        add(pr_textInput);

    pr_textStyle= new Label ("Style:");
        add(pr_textStyle);

        pr_text= new TextField(20);
        add (pr_text);
        pr_text.addActionListener(this);
        addMouseListener(this);

        pr_style= new TextField(5);
        add (pr_style);
        pr_style.addActionListener(this);
        addMouseListener(this);
    }

    public void start() {
        setSize(800,800);
    setBackground( new Color(125,158,192) );
        pr_name="";
        pr_styleType="";
    }

    public void paint (Graphics g) {
        pr_label.setLocation(100,0);
        pr_textStyle.setLocation(275,50);
    pr_textInput.setLocation(50,50);
        pr_text.setLocation(100,50);
    pr_style.setLocation(325,50);

    g.setColor(Color.white);
    Font font = new Font("Arial",Font.BOLD,20);
    g.setFont(font);

    for (i=0; i<pr_name.length(); i++) {
            c=pr_name.charAt(i);

        if (c == sl1) {
        drawGPolygon(xpoints,ypoints,npoints);
            }
    }            

    }   

    public void mouseClicked(MouseEvent e){}


    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}

    public void actionPerformed(ActionEvent e) {
        //set ActionCommand
    if (e.getSource()== pr_text) {
            pr_name =e.getActionCommand();
    }
    else if (e.getSource()==pr_style) {
            pr_styleType=e.getActionCommand();
    }

    //set Style 
    repaint();
    }

    public void drawGPolygon(int xpoints, int ypoints,int npoints) {
        gx=e.getX(c);
    gy=e.getY(c);
        xpoints[] = {gx, gx, gx+40, gx+40,gx+45,gx+45,gx+25,gx+25,gx+30,gx+30,gx+5,gx+5,gx+50,gx+50};
    ypoints[] = {gy, gy+50, gy+50, gy+30, gy+30,gy+20,gy+20,gy+30,gy+30,gy+40,gy+40,gy+10,gy+10,gy};
        npoints = 14;
        g.drawPolygon(xpoints, ypoints, npoints);
    }
}

1 个答案:

答案 0 :(得分:1)

基本上,您需要使用字体的字形将文本转换为Shape。

查看java/swing: converting a text string to a Shape