Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 About the graphics; In Applets
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flashkicks




PostPosted: Wed Jun 02, 2004 8:37 am   Post subject: About the graphics; In Applets

Hi! This is kinda like an add-On to the original post about graphisc.. I have werked around with it some and came across this problem- i can load up the pic; but NOT in an applet.. Here is my applet and wutnot- could you or anybody please take a look at it and see what is going on??.. I think it has to do something about the toolkit thingy.. Bcuz if i comment it out- my proggy crashes and then I cant type anything in it. So i assume the toolkit is NEEDED.. PLEASE HELP...
code:
// The "CosineLaw" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;

public class CosineLaw121 extends Applet implements MouseListener
{
    int choice = 0;

    //delay method
    public static void delay (int milliseconds)
    {
        try
        {
            Thread.sleep (milliseconds);
        }


        catch (InterruptedException e)
        {
        }
    }


    public void init ()
    {
        setSize (640, 400);
        setBackground (new Color (0, 0, 0));
        addMouseListener (this);

        JFrame frame = new JFrame ();
        MyPanel panel = new MyPanel ("triangles.jpg");

        frame.setContentPane (panel);
        frame.setDefaultCloseOperation (3);
        frame.setBounds (new Rectangle (500, 500));
        frame.setVisible (true);

        panel.repaint ();
    }


    public void paint (Graphics g)
    {
        if (choice == 0)
        {
            int i;
            for (i = 0 ; i < 25 ; i++)
            {
                g.setColor (Color.black);
                g.fillRect (0, 0, 1000, 1000);
                g.setFont (new Font ("Ravie", 3, 20));
                g.setColor (Color.red);
                g.drawString ("Question One", 230, 0 + i);
            }

            delay (300);
            g.setFont (new Font ("Times New Roman", 3, 15));
            g.drawString ("These diagrams represent 3 problems.  A strategy for each problem", 160, 40);
            g.drawString ("is listed, but not in the correct order.", 160, 60);
            g.drawString ("  1) Match each diagram to a strategy.  Explain your reasoning.", 160, 80);
            g.drawString ("  2) Apply each strategy to the matched diagram. (Calculate)", 160, 100);
            delay (1000);

            g.setFont (new Font ("Times New Roman", 3, 12));
            g.setColor (Color.red);
            g.drawString ("a)Notice that the", 100, 310);
            g.drawString ("triangle has a right", 100, 320);
            g.drawString ("angle.  Use either", 100, 330);
            g.drawString ("the sine or cosine", 100, 340);
            g.drawString ("ratio.", 100, 350);

            g.drawString ("b)Use the sine rule", 250, 310);
            g.drawString ("with 2 angles and ", 250, 320);
            g.drawString ("there opposite sides.", 250, 330);
            g.drawString ("Solve the resulting", 250, 340);
            g.drawString ("equation for the unknown.", 250, 350);

            g.drawString ("c)Use the cosine rule", 400, 310);
            g.drawString ("with 1 angle and 2 sides", 400, 320);
            g.drawString ("to find the remaining", 400, 330);
            g.drawString ("sides.", 400, 340);


            g.setColor (Color.white);
            g.fillRect (305, 360, 50, 20);
            g.setColor (Color.black);
            g.drawString ("NEXT", 310, 373);
        }
        else if (choice == 1)
        {
            int i;
            for (i = 0 ; i < 50 ; i++)
            {
                g.setColor (Color.black);
                g.fillRect (0, 0, 1000, 1000);
                g.setFont (new Font ("Ravie", 3, 20));
                g.setColor (Color.red);
                g.drawString ("Example", 230, 0 + i);
            }

            //triangle image
            g.setColor (Color.red);
            delay (100);
            g.drawLine (150, 250, 300, 250);
            delay (100);
            g.drawLine (150, 250, 120, 100);
            delay (100);
            g.drawLine (120, 100, 300, 250);
            delay (100);
            g.setFont (new Font ("Times New Roman", 3, 15));
            g.setColor (Color.yellow);
            g.drawString ("A", 115, 95);
            delay (100);
            g.drawString ("B", 140, 265);
            delay (100);
            g.drawString ("C", 305, 255);
            delay (100);
            g.setColor (Color.orange);
            g.drawString ("a", 220, 265);
            delay (100);
            g.drawString ("b", 215, 170);
            delay (100);
            g.drawString ("c", 125, 180);
            delay (100);

            //question
            g.setColor (Color.red);
            g.drawString ("Find side 'a' to the nearest whole number", 100, 300);
            g.drawString ("Find angles B and C to the nearest degrees", 100, 320);
            delay (100);
            g.drawString ("A = 21 degrees", 100, 340);
            g.drawString ("b = 27", 100, 360);
            g.drawString ("c = 20", 100, 380);
            delay (100);
            g.drawString ("Try to solve it yourself", 100, 400);
            delay (100);
            g.drawString ("You can check your answer afterwards, just click the solution button", 100, 420);

            delay (700);

            //solution button
            g.setColor (Color.white);
            g.fillRect (450, 350, 100, 40);
            g.setColor (Color.black);
            g.drawRect (455, 355, 90, 30);
            g.drawString ("Solution", 475, 375);
        }
        else if (choice == 2)
        {
            int i;
            for (i = 0 ; i < 50 ; i++)
            {
                g.setColor (Color.black);
                g.fillRect (0, 0, 1000, 1000);
                g.setFont (new Font ("Ravie", 3, 20));
                g.setColor (Color.red);
                g.drawString ("Solution", 250, 0 + i);
            }

            g.setFont (new Font ("Times New Roman", 3, 10));
            g.drawString ("2           2           2", 110, 133);
            g.drawString ("2           2           2", 117, 211);

            g.setFont (new Font ("Times New Roman", 3, 15));
            g.drawString ("Answer:", 100, 100);
            delay (200);
            g.drawString ("b = 27     c = 20     <A = 21", 100, 120);
            g.drawString ("a  = 27  +  20   - 2(27)(20)(cos 21)", 100, 140);
            g.drawString ("a  = 729 + 400 - 1080(cos 21)", 100, 160);
            g.drawString ("a = 11", 100, 180);
            g.drawString (" ", 100, 200);
            delay (200);
            g.drawString ("20  = 11  + 27  - 2(11)(27)(cos C)", 100, 220);
            g.drawString ("400 = 121 +729 - 594(cos C)", 100, 240);
            g.drawString ("594(cos C) = 450", 100, 260);
            g.drawString ("cos C = 450/594", 100, 280);
            g.drawString ("<C = 41 degrees", 100, 300);
            g.drawString (" ", 100, 320);
            delay (200);
            g.drawString ("<B = 180 - <A - <C", 100, 340);
            g.drawString ("<B = 180 - 21 - 41", 100, 360);
            g.drawString ("<B = 118 degrees", 100, 380);

            //restart button
            g.setColor (Color.white);
            g.fillRect (450, 200, 100, 40);
            g.setColor (Color.black);
            g.drawRect (455, 205, 90, 30);
            g.drawString ("Restart", 475, 225);

        }

    }


    public void mouseClicked (MouseEvent event)
    {
        int x, y;
        x = event.getX ();
        y = event.getY ();

        if (choice == 0)
        {
            if (x > 300 && x < 365 && y > 360 && y < 385)
            {
                choice = 1;
                repaint ();
            }
        }
        else if (choice == 1)
        {
            if (x > 450 && x < 550 && y > 350 && y < 390)
            {
                choice = 2;
                repaint ();
            }
        }
        else if (choice == 2)
        {
            if (x > 450 && x < 550 && y > 200 && y < 240)
            {
                choice = 0;
                repaint ();
            }
        }
    }


    public void mousePressed (MouseEvent event)
    {
    }


    public void mouseReleased (MouseEvent event)
    {
    }


    public void mouseEntered (MouseEvent event)
    {
    }


    public void mouseExited (MouseEvent event)
    {
    }


    class MyPanel extends JComponent
    {
        private Image myImage;

        public MyPanel (String imageName)
        {
            super ();
            setImage (imageName);
        }

        public void paint (Graphics g)
        {
            super.paint (g);
            g.drawImage (myImage, 0, 0, this);
        }

        public void setImage (String fileName)
        {
            //myImage = getToolkit().getImage (fileName);
        }
    }
}


~Flashkicks
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Wed Jun 02, 2004 4:02 pm   Post subject: (No subject)

Try:

getImage(getDocumentBase(), "mypic.gif");

rather then the toolkit one.

Also may be addinf a media tracker whould help so it will w8 for the pic to load:

code:

image = getImage(getDocumentBase(), "mypic.gif");

MediaTracker tracker = new MediaTracker(this);           
tracker.addImage(image, 0);

try
{
       tracker.waitForAll();
} catch (Exception ignore)
{
}
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Flashkicks




PostPosted: Thu Jun 03, 2004 7:57 am   Post subject: (No subject)

Dear Hacker;
Howdy! I am back..lol. Ummm. Okay. I looked at what you gave me and me and 2 other guys tried werking it into our proggy; but cant get it to werk. We were wondering if you could take the coding I have given you and mod it to the way it ShOuLd be; the way you described it to look like.. Do you think this is possible??... It would mean a lot; and all i really have to giev you is like 29 bits Confused Sad ... Am i asking too much??... I raelly would love to figure it out but just acnt seem to get it right; and deadlines are coming up! Shocked Thanks a lot....
~Flashkicks
Flashkicks




PostPosted: Fri Jun 04, 2004 7:35 am   Post subject: (No subject)

<<Next Day>>
This is not suppsed to be an annoying double post; but I just wanted to say GEEZ- im STILL trying to put your code in and whatnot but it just wont seem to work. I think i am not putting it all in the proper places. Either way it ALWAYS expects for a token to be inserted at certain locations but i checked them all and everything seems okay.. Evil or Very Mad Yaaaaaaaa.... Some help would be nice..lol.. Anyhoo- thanks for everything thus far! Dont think it goes unAppreciated..
~Flashkicks
Dan




PostPosted: Sat Jun 05, 2004 3:30 pm   Post subject: (No subject)

Sory for not geting back to you for a bit, for the project i am working what i did was make a class like this:

code:

import java.awt.*;

public class PicUtils
{
        public static Image loadPic(String path, Component comp)
        {
                Image temp = comp.getToolkit().createImage(path);
                       
                       
                MediaTracker tracker = new MediaTracker(comp);
        tracker.addImage(temp, 0);
               
        try
        {
            tracker.waitForID(0);
            if (tracker.isErrorAny())
            {
                System.out.println("Error loading image.");
            }
        }
        catch (Exception ex)
        {
                System.out.println("Error loading image...." + ex.getMessage());
                ex.printStackTrace();
        }
       
        return temp;
        }
}


so i could call PicUtils.loadPic to load any pic i whont.

what you could try to do is use that class i posted and chage

code:

public void setImage (String fileName)
        {
            //myImage = getToolkit().getImage (fileName);
        }


to

code:

 public void setImage (String fileName)
        {
            myImage = PicUtils.loadPic(fileName,this);
        }


But my class was made for an apliaction and not an applet so idk, may be rizzix could take a look? I have not done that much with applets more with apliactions.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Flashkicks




PostPosted: Mon Jun 14, 2004 7:28 am   Post subject: (No subject)

Dear HackerDan;
I just wanted to say that the semester is now over as of TODAY- and i appreciate ALL the help you have assisted. We got the program Somewhat how we wanted it, but it still has a few glitches Confused But hopefully next semester some one smafrter than myself will finish it off and make it look really good.. Anyhoo- once again; thank you so much for everything.
~Flashkicks

PS: Sorry for not responding to this thread for so long:( I was extremely busy on getting my proggy to work..
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: