我学习Java的时间不长,请原谅我,但我被要求制作一个小程序,它的基本功能之一是允许用户输入文本框并让小程序显示他们写的内容,在我的 2 个首字母 (G,P) 的任何情况下,程序都应该填充它的多边形,而不是写这个字符。
显然我不能每次都在同一个坐标中制作这个多边形,所以我使用了一种我认为应该可行的方法,即为第一个点创建变量,然后让小程序接收角色的坐标(通过 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);
}
}