Alex Edwards 321 Posting Shark

Keep in mind that swing is event-driven.

In order to check a word for a character, you should create a method that iterates through each character of that word.

public boolean charIsInWord(String word, char arg)
{
char[] charWord = word.toCharArray();

//iterate through the values of the char array and compare them to the char argument

//return true if a match was found, or return false if no match was found
}

If this method returns false when someone clicks a button then something needs to happen. Like I said, swing is event driven so your buttons need their ActionListener interfaces set to non-null so their ActionListener.actionPerformed(ActionEvent e) methods can fire.

Basically, you need to implement the ActionListener interface, one way or another (either through an interface implementation directly to your class, to an interface object, in an enum, in some other class that supports buttons... any of those ways work!) and setActionCommand(String arg) for each button.

Alex Edwards 321 Posting Shark

Not sure why but this is working for me, using a sample pic called Sunset.jpg

It may be that filetypes such as .gif and .png etc are not supported by the standard 1.4 Java Applet. In this case you may want to use one of two options--

-Try JApplet
-Import a GCanvas and use GImage to upload other image types.

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

public class ImageTest extends Applet
{
	private Image img;

	public void init()
	{
		img = null;
	}

	public void loadImage()
	{
		try
		{
			img = getImage(getCodeBase(), "Sunset.jpg");
			System.out.println(img);
			System.out.println(prepareImage(img, 300, 400, this));
		}
		catch(Exception e){}
	}

	public void paint(Graphics g)
	{
		if (img == null)
			loadImage();
		g.drawImage(img, 0, 0, this);
	}
}
Azzy1234 commented: Alex Edwards! I. Love. You. I have been struggling for days with getting an image on an applet and you're code worked like magic!! Thank you so very much +0
Alex Edwards 321 Posting Shark

I'm fairly certain that before you can load an image you have to prepare it first.

System.out.println(prepareImage(img, 300, 400, this));

use this after you attempt to get the Image specified by your source.

Alex Edwards 321 Posting Shark

I think it has something to do with the way the stream works--

if you call the method nextInt it will return the value on the line and attempt to the next line of code.

I'm almost certain that because the type returned was not the type that should be returned, the method nextInt threw an exception and most likely returned 0 (since the method is an int and must therefore return a valid argument). Upon doing so, the stream might not have been flushed, leaving the value on the line and for it to be re-evaluated again since the line jump was unsuccessful.

Here is the edit--

import java.util.Scanner;
import java.util.InputMismatchException;

public class P4Exceptions
{
	public static void main(String[] args)
	{

	Scanner scan = new Scanner(System.in);
	int n1, n2;
	double r = 0;
	boolean continueLoop = true; // determines if more input is needed

		while ( continueLoop )
		{
			try
			{
				System.out.println("Enter two numbers. I will compute the ratio:");
				n1 = Integer.parseInt(scan.nextLine());
				n2 = Integer.parseInt(scan.nextLine());
				r = (double) n1 / n2;

				if (n2 == 0)
				throw new ArithmeticException();

				System.out.println("The ratio r = " + r );
				continueLoop = false;

			}
			catch ( ArithmeticException arithmeticException )
			{
				System.out.println("There was an exception: Divide by zero. Try again\n");
				n1 = 0;
				n2 = 0;
			}
			catch ( NumberFormatException inputMismatchException )
			{
				System.out.println("You must enter an integer. Try again.\n");
				n1 = 0;
				n2 = 0;
			}
			catch(Exception e)
			{
				System.out.println(e);
                                n1 = 0;
                                n2 = 0;
			}

		}

	}
}
darklich13 commented: Excellent post!! Thanks for the help!! +1
Alex Edwards 321 Posting Shark

import java.awt.*;
import java.applet.*;

public class graphic extends Applet {
Button button1;
public void init() {
}

public void paint(Graphics g) {

int CYCLE=4;
int MAX = 1000;
int x1=0;
int x2=0;
g.setColor(Color.red);//color for axes
g.drawLine(0,150,700,150);//x-axis
g.drawLine(240,0,240,500);//y-axis
g.drawString("X-Axis", 430,140);//Label for x-axis
g.drawString("Y-Axis",200,270);//Label for y-axis
g.setColor(Color.blue);//color for the sin curve

for (int i=-130;i<=368;i++)
{
try
{
Thread.sleep(10); // 10 millisecond delay
}
catch(InterruptedException e)
{
e.printStackTrace();
}

x1 = (int)(100 * Math.sin(((i)*2*Math.PI*CYCLE)/(MAX)));
x2 = (int)(100 * Math.sin(((i+1)*2*Math.PI*CYCLE)/(MAX)));
g.drawLine(i+121,x1+138,(i+1)+121,x2+138);
}
g.setFont(new Font("Times New Roman",Font.BOLD,15));
g.drawString("IT WORKS ; )",100,50); }
}
//Here is the code

import java.awt.*;
import java.applet.*;

public class graphic extends Applet {
	Button button1;
	public void init() {
	}

	public void paint(Graphics g) {
     
        int CYCLE=4;
        int MAX = 1000;
        int WEIGHT = 100;
		int x1=0;
		int x2=0;
		g.setColor(Color.red);//color for axes
		g.drawLine(0,150,700,150);//x-axis
        g.drawLine(240,0,240,500);//y-axis
        g.drawString("X-Axis", 430,140);//Label for x-axis
        g.drawString("Y-Axis",200,270);//Label for y-axis
        g.setColor(Color.blue);//color for the sin curve
        
         for (int i=-130;i<=368;i++)
       {  	  
       	try 
            {
           Thread.sleep(10); // 10 millisecond delay 
            } 
            catch(InterruptedException e)
            {
           e.printStackTrace();
            } 
	
       	x1 = (int)(WEIGHT * Math.sin(((i)*2*Math.PI*CYCLE)/(MAX)));
       	x2 = (int)(WEIGHT * Math.sin(((i+1)*2*Math.PI*CYCLE)/(MAX)));  	
       	g.drawLine(i+121,x1+138,(i+1)+121,x2+138);
       }
        g.setFont(new Font("Times New Roman",Font.BOLD,15));
        g.drawString("IT WORKS ; )",100,50);	}	
}
//Here is the code

Keep in mind that the MAX is really the maximum difference between the min-x and max-x values.

For example, if you can go from -100 to 100 in your graph, MAX …

PoovenM commented: Good work :) +2
Alex Edwards 321 Posting Shark

Thanks for your replies , i really wasn't expecting so many replies for my simple question.Because of your explanation in detail i understood it properly.Mathematically Alex is 100% right , because it really gives me a gradient of zero which is literally a straight line. I have changed my code and it gives me a sine curve but , it sticks to the top of the window but i hope that can be figured out . Once again thanks for the help and thanks for supporting the idea that g.drawLine(i,i,i,i) represents a point .Hopefully i ill be posting the code when its all done :)

I wish I could help you with using more of the Graphics class's functions but unfortunately I truly HATE using Graphics from getGraphics in containers. I wouldn't be able to help you with setting the location of your Graphics object.

I personally prefer using GObjects, GCompounds, GLines that work on GCanvases.

The only method I have an issue with understanding is the move method in most GObject classes. I'm almost certain that it fires a thread that continuously offsets the object but i could be wrong.

Alex Edwards 321 Posting Shark
do
         {
            System.out.print("Enter Tour Type (G for guide tour, U for no guide tour): ");

			String temp2 = console.nextLine();//apparantly there was still data in the stream that needed to be flushed?

            type = console.nextLine().toUpperCase();

            System.out.print("");

         } while (!type.equalsIgnoreCase("G") && !type.equalsIgnoreCase("U"));

Sorry for the multi-posts, unfortunately you can't edit your previous post after 30 minutes.

Alex Edwards 321 Posting Shark

I've not checked if Alex is correct, but as per your question regarding the storage of your array information. It can so easily be done by writing the entire array object to a file.

The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.

Whoops i was tired when I replied, it's definitely not the solution.

I was trying to figure out if it was the do-while loop repeating or if it was the information on the screen reprinted and it's obviously not the 2nd option. My apologies.

Alex Edwards 321 Posting Shark

By the way, it might not be a good idea to add a button in paint(Graphics g) method, because each time you resize the applet paint is called (which makes sense, because it needs to redraw components when it has been resized).

Alex Edwards 321 Posting Shark

Hi guys, drawLine() doesn't require the points to vary. In fact drawLine(10,10,10,10) would simply result in a point being drawn.

It's Math.PI because all constants, as per the Java convention, are in uppercase. He isn't using degrees though so there isn't a need to convert between degrees and radians; Math.sin(x) where x is an angle measured in radians. The conversion is degrees * PI/180 btw.

He isn't drawing a circle and his approach is almost correct...

What you need to remember Jahan, is that sin graphs have an amplitude of 1 unit. That is, your y value is going to range between -1 and 1 (at least in this case). Since you're dealing with a continuous function, your (int) conversion is going to give you values of either -1, 0 or 1 and this is not what you want. I suggest multiply the double value you get from the sin method with some constant before you convert the value to int. I hope this makes sense? This constant should be used to scale your x value of the Cartesian plane to pixels so perhaps 1 x unit is 10 pixels.

Moreover, your graph isn't going to be drawn on the axises you created because your (x, y) pairs in the drawLine() method don't match the values of your axises. It's not a difficult thing to do, but it does require you to think of the differences between a Cartesian plane and the pixel co-ordinate system you're using on your computer.

Alex Edwards 321 Posting Shark

Line 203 is the problem--

203 type = console.nextLine().toUpperCase();

Change it to--

type = console.next().toUpperCase();

--and you should be golden.

Alex Edwards 321 Posting Shark

Now that I think about it, you probably don't want to simply use Math.sin(i) in your statement.

This means that you're going to go around the circle VERY fast as i increments. Your wavelength will most likely be small unless you use some kind of scalar that multiplies by the sin function (like 50 or so).

If anything, think about how many sin cycles you want in 1000 increments.

Let's say you want 4 cycles (where a cycle is simply 360 degrees (or 2pi radians) reached). Then you'd have to find out when your number reaches a cycle and multiply by that value.

final double CYCLE = 4
final double MAX = 1000

math.sin( ((i + 2)* Math.pi * CYCLE) / ( MAX) ) for the second y arg, and most likely
math.sin(((i) * Math.pi * CYCLE)/ MAX) for the first y arg.

Alex Edwards 321 Posting Shark

Keep in mind what Graphics.drawLine does.

You're trying to draw a point from i to i, and Math.sin(i) to Math.sin(i) so you're going to get something like a line due to the fact that your points aren't varying from x1 to x2 and y1 to y2.

Try g.drawLine(i,(int)Math.sin(i),i + 2,(int)Math.sin(i))));

sorry for quoting myself, but instead of Math.sin(i) use Math.sin(i + ((2 * i)/(2 * Math.PI))) in the last argument.

it might be Math.pi or Math.Pi for the pi constant... bah!

I'm not anywhere near a java compiler atm >_>

Alex Edwards 321 Posting Shark

Keep in mind what Graphics.drawLine does.

You're trying to draw a point from i to i, and Math.sin(i) to Math.sin(i) so you're going to get something like a line due to the fact that your points aren't varying from x1 to x2 and y1 to y2.

Try g.drawLine(i,(int)Math.sin(i),i + 2,(int)Math.sin(i))));