0

I have not been learning Java for too long so forgive me, but I've been asked to make an applet and one of its basic functions is to allow a user to enter into a text box and for the applet to display what they write, in any case of my 2 initials (G,P) the program should instead of writing this character, fill a polygon of it.

Obviously I can't make this polygon in the same coord's everytime, so i have used a method I thought should work, which is to create variables for the first point and then get the applet to receive the coordinates of the character (through a for loop) and then display the polygon, the main problem I'm having is that the polygon is a separate function and it keeps saying identifier expected, to get rid of this i changed where the variables xpoints, ypoints and npoints are initialized and they are no longer global. but now i get like 60 errors saying not a statement (in the polygon function) and ";" expected. any help??

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);
    }
}
Dan
  • 1,763
  • 4
  • 26
  • 40
georgep93
  • 3
  • 1
  • 2

1 Answers1

1

Basically, you need to convert the text to a Shape, using the font's glyphs.

Take a look at java/swing: converting a text string to a Shape

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366