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

Username:   Password: 
 RegisterRegister   
 Title Bar + Threads + embeding fonts, any ideas?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
aussilia




PostPosted: Fri Sep 22, 2006 10:33 pm   Post subject: Title Bar + Threads + embeding fonts, any ideas?

Hello, this is my first time posting on the java side of compsci!
(I have migrated from Turing)

After about 3 weeks and tons of hours learning Java, I have hit three main points that I am unable to understand/implement.

The program I am writing is a copy of the matrix screen-saver (letters fall from top of screen) application. I have made an entire .java file that has double-buffer, and the code to make one line of varying length and characters fall from the top of the screen.

problems:

1. Is there any way to make the title bar with the little cup go away (to get the full screen effect) either in the code or in making it into a .jar file?

2. I can make one line appear, but I need ~ 70 at the same time! Will the double buffers interfere with each other? I also plan to use the mouse (click on a character to freeze the entire screen); can I implement it in a "main thread" sort of thing? Or should I make another class and implement the first one?? Threads and making objects are very confusing for me so far Embarassed Sad

3. I have Arial Unicode MS on my computer, and I use this in my program to generate the Japanese and other characters needed, but other computers don't have it. Instead of asking others to download the font (which is pretty hard to find because it is Microsoft proprietary) is their a way to "bundle" the font with my program?

Any help in any of these areas would be very very very appreciated. Sorry if the questions seem naive but other help I found wasn't much help in the area of applications.
Thanks allot!!!
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Sat Sep 23, 2006 2:57 pm   Post subject: (No subject)

1. You'll want to use a JWindow instead of using JFrame.
2. No idea what you're trying to say here, sorry. Wink
3. Most likely. Just include the .ttf file with your program. Can't help you with any specific problems you might have, since I've never done it myself.
aussilia




PostPosted: Sat Sep 23, 2006 6:55 pm   Post subject: (No subject)

1. Thanks, exactly what i was looking for! Very Happy

2. sorry if i was being unclear, but i think i have accidentaly found my own answer, explanation to come soon Exclamation

3. What do you mean by "include"? in the compilation? in the java program itself?? Shocked
i searched for include but none of the things mentioned had much to do with what i am trying to do. Has anyone ever done anything along these lines?

Thanks so much to Gandalf for the help!
[Gandalf]




PostPosted: Sat Sep 23, 2006 8:25 pm   Post subject: (No subject)

No problem. Smile

What I meant by include was simply have the .ttf file in the same folder as the .java/.class file. There is a chance that you will then simply be able to use it like any other font already on that computer, or you might have to do something more like installing it to C:/Windows/Fonts for Windows.
Aziz




PostPosted: Mon Sep 25, 2006 3:29 pm   Post subject: (No subject)

Wow, 3 weeks, either you're skipping ahead and doing too advanced stuff, or you've REALLY put a lot of effort into this. I'm going to go ahead and assume the ladder, and point you to the Full-Screen Exclusive API of Java, check out the Java Tutorial for it: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
aussilia




PostPosted: Tue Oct 03, 2006 12:09 pm   Post subject: (No subject)

So the final problem i have left is integration.

I have code that lets me generate a line of characters and make them fall down the screen. The only problem is that i need about 60 of these lines to make the matrix effect. What i have been trying to do is to make one class that just generates a line, the call it from another class that makes the backbuffer adn screen. Am i on the right track?

What i am basicaly looking for is something like the turing fork statment. (you can make a for loop and use the fork statment as many times as you want). Is this way that I described the best way?

*pseudo code*

letters class
make a line of random characters ex: fadsfafafe
add to the y value of the position of the line of characters (they fall down)

screen class
make the backbuffer and screen
call letters 60 times
[/b]
Aziz




PostPosted: Tue Oct 03, 2006 10:43 pm   Post subject: (No subject)

Turing's fork is (supposed to be) the threads of other languages. Except, thread's work. In java, this is the class Thread. Check out the API and the Java Tutorials on it.

Quickie about threads.

The constructor:

[syntax="java"]public Thread(Runnable r)[syntax]

Any object can be Runnable. So, say you make a Timer class, and implement the interface 'Runnable'. Runnable requires the 'run()' method. So you have a class:

Java:
public class TestThread {
   public static void main(String[] args) {
      TimerThing timer1 = new TimerThing();
      TimerThing timer2 = new TimerThing();

      Thread thread1 = new Thread(timer1);
      Thread thread2 = new Thread(timer2);

      // This will 'start' the thread, causing the thread to call the Runnable's 'run()' method
      thread1.start();

      // Wait 5 seconds (in Turing, this would be delay(5000), or close to it)
      try { Thread.sleep(4000); } catch (Exception e) {}

      // Start thread2
      thread2.start();

      // You could do some code that takes about 8 seconds, we'll just wait
      try { Thread.sleep(4000); } catch (Exception e) {}

      // Thread 1 should be at 8 seconds while thread 2 should be at 4 seconds
      System.out.println("Thread 1: " + timer1.getTime() + "s");
      System.out.println("Thread 2: " + timer2.getTime() + "s");

      //Kill the threads
      timer1.running = false;
      timer2.running = false;
   }
}


Java:
public class TimerThing implements Runnable {

   private int time;
   public boolean running;

   public TimerThing() {
      time = 0;
      running = true;
   }

   public void run() {
      while (running) {
               time++;

               //Don't worry about this, it just delays
               try {
                  Thread.sleep(1000);
               } catch (Exception e) {}
      }
   }

   public int getTime() {
      return time;
   }
}


But I don't think threads are you're problem here.

You need say, 10 lines of text (for example).

Use an array:

code:
String[] lines = new String[10];


Then set all the elements of lines to an empty string ("") (via a for loop).

Make a while loop:

Java:
while(running) {

 for (int i = 1; i < 10; i++) {
  lines[i] = lines[i - 1];
 }
 lines[0] = "ahdaskjdheadsdsnhkcihaosd"; //some randome generated letters
 //some code to find out when to stop, say after 10 seconds?
 //say nowTime calls a function to find out the time now, and beginTime was before the while loop, both in units of seconds)
 if (nowTime() - beginTime > 10) { running = false; }
}


I think that's pretty much all you'll need....
aussilia




PostPosted: Fri Oct 06, 2006 4:10 pm   Post subject: (No subject)

The trick is that i want all 60 of my lines to fall at the same time,
i dont think your second sugestion code does that, but the first might!
Thanks!
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Fri Oct 06, 2006 6:51 pm   Post subject: (No subject)

No, I don't think you understood what Aziz was trying to tell you. You should not be using threads for something this simple. Threads are designed for programs that have components that need to run separate of each other. Yours does not, or doesn't need to anyways. Even if you did implement threads in your program, your 60 lines of letters still won't be falling down at the same time, they'll only seem like it, because of the speed at which the computer processes at. Aziz's second method works just fine, if not better than the method involving threads.
aussilia




PostPosted: Fri Oct 20, 2006 11:18 pm   Post subject: (No subject)

I have finally finished stage one of my project (personal). This will let you all know what i was talking about during this entire time.

A big kudos to Aziz for seeing my madness and pointing me in the right way. (I have come far along since my last post, threads do seem like a stupid idea)

More improvments coming soon!

(If you see boxes instead of characters, you need to have Arial Unicode MS installed)

code:

// The "Matrix2" class.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;
import java.lang.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;

public class Matrix2 extends JWindow
{
    private static BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    private int bufferWidth; // Image buffer width
    private int bufferHeight; // Image buffer height
    private Image bufferImage; // entire Image buffer
    private Graphics bufferGraphics;
    int ANTIALIASING = 0;
    int LH = 1; //Letter Height
    int LA = 1000; // Letter Amount - 1
    int LLP = 0;
    int x = 0;
    int DS = 7;
    int scry = 10;
    int scrx = 10;
    static String file = "";
    int[] [] Letters = new int [LA + 1] [5];
    int[] [] Chars = new int [LA + 1] [7];
    public int i1 = 0x4E45; // letter values for the random letters
    public int i2 = 0x4E45;
    public int i3 = 0x4E45;
    public int i4 = 0x4E45;
    public int i5 = 0x4E45;
    public int i6 = 0x4E45;
    public int i7 = 0x4E45;
    public int i8 = 0x4E45; // end of letter values
    Random generator1 = new Random (); // random letter generator for all letter values

    public void initializer () throws IOException
    {
        BufferedReader in = new BufferedReader (new FileReader ("Matrix Settings.txt"));

        ANTIALIASING = Integer.parseInt (in.readLine ());
        LH = Integer.parseInt (in.readLine ());
        LA = Integer.parseInt (in.readLine ());
        DS = Integer.parseInt (in.readLine ());
        scry = Integer.parseInt (in.readLine ());
        scrx = Integer.parseInt (in.readLine ());
        file = in.readLine ();
    }


    public Matrix2 ()
    {
        try
        {
            initializer ();
        }
        catch (IOException e)
        {
            System.out.println ("Unable to initialize, wrong Settings");
        }
        Dimension dim = Toolkit.getDefaultToolkit ().getScreenSize ();
        setSize (dim);
        setBackground (Color.black);
        setVisible (true);
        for (int I = 0 ; I <= LA ; I++)
        {
            Letters [I] [0] = generator1.nextInt (scry);
        }
    }


    public static void music ()
    {
        try
        {
            // From file
            AudioInputStream stream = AudioSystem.getAudioInputStream (new File (file));

            AudioFormat format = stream.getFormat ();
            if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
            {
                format = new AudioFormat (
                        AudioFormat.Encoding.PCM_SIGNED,
                        format.getSampleRate (),
                        format.getSampleSizeInBits () * 2,
                        format.getChannels (),
                        format.getFrameSize () * 2,
                        format.getFrameRate (),
                        true);        // big endian
                stream = AudioSystem.getAudioInputStream (format, stream);
            }

            // Create the clip
            DataLine.Info info = new DataLine.Info (Clip.class, stream.getFormat (), ((int) stream.getFrameLength () * format.getFrameSize ()));
            Clip clip = (Clip) AudioSystem.getLine (info);

            // This method does not return until the audio file is completely loaded
            clip.open (stream);

            // Start playing
            clip.loop (Clip.LOOP_CONTINUOUSLY);
        }
        catch (java.net.MalformedURLException e)
        {
        }
        catch (IOException e)
        {
        }
        catch (LineUnavailableException e)
        {
        }
        catch (UnsupportedAudioFileException e)
        {
        }
    }


    public void draw (Graphics g, int x, int y, int LC, int LS, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
    {
        x = x * LH;
        Graphics2D g2;
        g2 = (Graphics2D) g;
        if (ANTIALIASING == 1)
        {

            g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            g2.setRenderingHint (RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
        }
        FontRenderContext frc = g2.getFontRenderContext ();
        Font f = new Font ("Arial Unicode MS", Font.PLAIN, LH);



        char c1 = (char) i1;
        String s1 = new String ();
        s1 = String.valueOf (c1);
        TextLayout tl = new TextLayout (s1, f, frc);
        char c2 = (char) i2;
        String s2 = new String ();
        s2 = String.valueOf (c2);
        TextLayout t2 = new TextLayout (s2, f, frc);
        char c3 = (char) i3;
        String s3 = new String ();
        s3 = String.valueOf (c3);
        TextLayout t3 = new TextLayout (s3, f, frc);
        char c4 = (char) i4;
        String s4 = new String ();
        s4 = String.valueOf (c4);
        TextLayout t4 = new TextLayout (s4, f, frc);
        char c5 = (char) i5;
        String s5 = new String ();
        s5 = String.valueOf (c5);
        TextLayout t5 = new TextLayout (s5, f, frc);
        char c6 = (char) i6;
        String s6 = new String ();
        s6 = String.valueOf (c6);
        TextLayout t6 = new TextLayout (s6, f, frc);
        char c7 = (char) i7;
        String s7 = new String ();
        s7 = String.valueOf (c7);
        TextLayout t7 = new TextLayout (s7, f, frc);
        char c8 = (char) i8;
        String s8 = new String ();
        s8 = String.valueOf (c8);
        TextLayout t8 = new TextLayout (s8, f, frc);


        if (LS == 2)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (60, 255 - (255 / LS) * 1, 60));
            t2.draw (g2, x, y - LH);
        }


        if (LS == 3)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
        }


        if (LS == 4)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 3, 0));
            t4.draw (g2, x, y - 3 * LH);
        }


        if (LS == 5)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 3, 0));
            t4.draw (g2, x, y - 3 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 4, 0));
            t5.draw (g2, x, y - 4 * LH);
        }


        if (LS == 6)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 3, 0));
            t4.draw (g2, x, y - 3 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 4, 0));
            t5.draw (g2, x, y - 4 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 5, 0));
            t6.draw (g2, x, y - 5 * LH);
        }


        if (LS == 7)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 3, 0));
            t4.draw (g2, x, y - 3 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 4, 0));
            t5.draw (g2, x, y - 4 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 5, 0));
            t6.draw (g2, x, y - 5 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 6, 0));
            t7.draw (g2, x, y - 6 * LH);
        }


        if (LS == 8)
        {
            g2.setColor (new Color (120, 255, 130));
            tl.draw (g2, x, y);
            g2.setColor (new Color (100, 255 - (255 / LS) * 1, 110));
            t2.draw (g2, x, y - LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 2, 0));
            t3.draw (g2, x, y - 2 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 3, 0));
            t4.draw (g2, x, y - 3 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 4, 0));
            t5.draw (g2, x, y - 4 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 5, 0));
            t6.draw (g2, x, y - 5 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 6, 0));
            t7.draw (g2, x, y - 6 * LH);
            g2.setColor (new Color (0, 255 - (255 / LS) * 7, 0));
            t8.draw (g2, x, y - 7 * LH);
        }

    }


    public void update (Graphics g)
    {
        paint (g);
    }


    public void paint (Graphics g)
    {
        //      checks the buffersize with the current panelsize
        //      or initialises the image with the first paint
        if (bufferWidth != getSize ().width ||
                bufferHeight != getSize ().height ||
                bufferImage == null ||
                bufferGraphics == null)
            resetBuffer ();

        if (bufferGraphics != null)
        {
            //      this clears the offscreen image, not the onscreen one
            bufferGraphics.clearRect (0, 0, bufferWidth, bufferHeight);

            //      calls the paintbuffer method with the offscreen graphics as a param
            paintBuffer (bufferGraphics);

            //      we finaly paint the offscreen image onto the onscreen image
            g.drawImage (bufferImage, 0, 0, this);
            //If a delay is needed to freeze the letters, insert it here
        }
    }


    private void resetBuffer ()
    {
        // always keep track of the image size
        bufferWidth = getSize ().width;
        bufferHeight = getSize ().height;

        //      clean up the previous image
        if (bufferGraphics != null)
        {
            bufferGraphics.dispose ();
            bufferGraphics = null;
        }
        if (bufferImage != null)
        {
            bufferImage.flush ();
            bufferImage = null;
        }
        System.gc ();

        //      create the new image with the size of the panel
        bufferImage = createImage (bufferWidth, bufferHeight);
        bufferGraphics = bufferImage.getGraphics ();
    }



    public void paintBuffer (Graphics g)
    {
        for (int I = 0 ; I <= LA ; I++)
        {
            Letters [I] [0] = Letters [I] [0] + DS;
            Letters [I] [3] = 0;
            if (Letters [I] [0] > scry + LH * Letters [I] [4])
            {
                Letters [I] [1] = generator1.nextInt ((scrx / LH));
                Letters [I] [4] = generator1.nextInt (6) + 2;
                Letters [I] [0] = 0;
                Letters [I] [2] = 0;
            }
            if (Letters [I] [0] > Letters [I] [2] + LH)
            {
                Letters [I] [3] = 1;
                Letters [I] [2] = Letters [I] [0];
            }
            if (Letters [I] [3] == 1)
            {
                for (int h = 6 ; h >= 1 ; h--)
                {
                    Chars [I] [h] = Chars [I] [h - 1];
                }
                Chars [I] [0] = generator1.nextInt ((0x9FA5 - 0x4E00)) + 0x4E00;
            }
        }
        for (int I = 0 ; I <= LA ; I++)
        {
            draw (g, Letters [I] [1], Letters [I] [0], Letters [I] [3], Letters [I] [4], Chars [I] [0], Chars [I] [1], Chars [I] [2], Chars [I] [3], Chars [I] [4], Chars [I] [5], Chars [I] [6], Chars [I] [6]);
        }
        repaint ();
    }


    public static void main (String[] args)
    {
        new Matrix2 ();
        //music ();
    }
} // Matrix2 class


Aziz




PostPosted: Sun Oct 22, 2006 11:04 pm   Post subject: (No subject)

Whoa, triple post Razz Lag problems? Mod, flag for you to fix that. Anyways, no time to run that, but look into how to 'jar' files. There's a good tut from Sun. It makes i a nice executable. Anyways, night.
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  [ 11 Posts ]
Jump to:   


Style:  
Search: