Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

getting letters to print vertical down an applet task

$
0
0
Can anybody help me out with this problem?

i have created an applet that prints out the user input and if the characters J or D are entered a graphical representation of the letters is drawn using fill polygon method();

My next task is to enter either d or i in to the second textfield and the string, as well as the polygons are printed vertically or horizontally across the applet.

Also when i click the buttons bigger or smaller i need the text to become larger every time the user clicks either button...

I will be so grateful for any help as i dont know what to do next....

Here is the code so far which compiles and runs as an applet.

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;

public class example1 extends Applet implements MouseListener, ActionListener
{
	int pr_xCord, pr_yCord;
	int xCord = 80, yCord = 80;
	Label enter, style;
	TextField pr_input1, pr_input2;
	Button Bigger, Smaller;
	Color pr_col;
	String text1, text2;
	Font Font1;
	String message1;
	int letterSize;
	
	public void init()
	{
	//initialising and adding all the elements needed for the applet
		enter = new Label("Enter text:");
		add(enter);
		pr_input1= new TextField(20);
		add(pr_input1);
		style = new Label("Enter a style");
		add(style);
		pr_input2 = new TextField(1);
		add(pr_input2);
		Bigger = new Button("Larger");
		add(Bigger);
		Smaller = new Button("Smaller");
		add(Smaller);
		pr_input1.addActionListener(this);
		pr_input2.addActionListener(this);
		Bigger.addActionListener(this);
		Smaller.addActionListener(this);
		addMouseListener(this);
	}
	
	public void start()
	{
	//making the strings from the textfield are blank
	setBackground(Color.green);
	text1 = "";
	text2 = "";
	Font1 = new Font ("Calibri", Font.BOLD, 40);
	}
	
	public void mouseClicked(MouseEvent e)
	{
	//getting the position of the mouse click to paint the string	
		showStatus("Clicked at: "+ e.getX() + e.getY());
		message1 = "Clicked at" + e.getX() + e.getY();
		repaint();
	}
	
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e){}
	
	public void paint(Graphics g)
	{
		char output [] = text1.toCharArray();
		//each letter is stored in array [i] 
		//print each char in array using toCharArray()
		
		int L;
		for(L=0; L<output.length; L++)
		{
			g.setColor(Color.blue);
			g.drawString(""+output[L],xCord, yCord);
			
			if(output[L] == 'd'|| output[L] == 'D' )
			{
			
				
				//co-ordinates for the Letter 'D':
				int sl1a[]={xCord,xCord+25,xCord+32,xCord+35,xCord+35,xCord+33,xCord+25,xCord};
				int sl1b[]={yCord-40,yCord-40,yCord-38,yCord-30,yCord-10,yCord-3,yCord,yCord};
				g.setColor(Color.blue);
				g.fillPolygon(sl1a, sl1b, 8);
				g.setColor(Color.green);
				g.fillRect(xCord+10,yCord-30,15,20);
				g.setColor(Color.black);
				//g.drawString(200, 200);
				xCord = xCord + 30;	
			}
			
			else if(output[L] == 'j' || output[L] == 'J')
			{
				//co-ordinates for the letter 'J':
				int sl2a[]={xCord, xCord+40, xCord+40, xCord+25, xCord+25, xCord, xCord, xCord+15, xCord+15, xCord, xCord};
				int sl2b[]={yCord-40, yCord-40, yCord-30, yCord-30, yCord, yCord, yCord-10, yCord-10, yCord-30, yCord-30, yCord};
				
				//setting colour to red and using fill polygon method to create the J.
				g.setColor(Color.red);
				g.fillPolygon(sl2a, sl2b, 11);
				g.setColor(Color.white);
				xCord = xCord + 20;
			}
		xCord = xCord + 10;
		
		}
	}

	public void actionPerformed(ActionEvent e)
	{
	//gets string, style and sizes for the string output then repaints to the following co-ords
		text1 = e.getActionCommand();
		xCord = 80;
		yCord = 250;
		if (e.getSource() == Bigger)
		{
			if (letterSize < 75)
			{
				letterSize += 3;
			}
		}
		else if ((e.getSource() == Smaller) &&
				(letterSize > 15))
				{
					letterSize -=3;
				}
		repaint();
		
	}
}






PLEASE HELP !!!!!!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>