Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: calculate FontSize on: 2010-06-28 00:11:55
thx for fast reply.
i programm the method now with the bisektion method. I know this is not the best solution but thats why i asks first ^^.
here is the code (not efficient if using a loop!!!!!).
if other ideas...tell me ^^
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
private void getFontSize(Graphics g,int xres,float size){
        boolean ende=false;
        float calcSize=32;
        FontMetrics metrics;
        do{
            font=font.deriveFont(calcSize);
            metrics = g.getFontMetrics(font);
            if((metrics.stringWidth(Text)>xres*(size-0.01f))&&(metrics.stringWidth(Text)<xres*(size+0.01f))){
                ende=true;
            }else{
                if(metrics.stringWidth(Text)>xres*size){
                    calcSize=calcSize-calcSize/2;
                }else{
                    calcSize=calcSize+calcSize/2;
                }
            }
        }while(ende==false);
    }


Font font and
String Text are global
size is the "percentually" size from xres to fits the Text
2  Game Development / Newbie & Debugging Questions / calculate FontSize on: 2010-06-27 21:16:49
Hello Java Fans,
my question is, if there is an method or something equals, to calculate a FontSize to draw a text which must fit to a given Width or Height?

For example, if we have a area which is 200px width and we have a text like "This is a text" which we will draw with the font (yx) and these text should fit in the area with 200px width.

I haven't found something in FontMetrics but maybe there a other solutions?
The first witch i think is the bisection method to set a Size and get the Width witch FontMetrics' getStringWidth..., but i think this is very inefficient.


thx and sorry for my bad English
3  Game Development / Shared Code / Re: Image Encrypter/Decrypter on: 2009-01-24 03:12:35
Now here is a working code:

encrypt:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Encrypting {
        Cipher ecipher;
        Encrypting(Key key) {
            byte[] iv = new byte[]{
                (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
                (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
            };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            try {
                ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
                ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            }
            catch (java.security.InvalidAlgorithmParameterException e) {}
            catch (javax.crypto.NoSuchPaddingException e) {}
            catch (java.security.NoSuchAlgorithmException e) {}
            catch (java.security.InvalidKeyException e) {}
        }
        byte[] buf = new byte[1024];
        public void encrypt(InputStream in, OutputStream out) {
            try {
                out = new CipherOutputStream(out, ecipher);
                int numRead = 0;
                while ((numRead = in.read(buf)) >= 0) {
                    out.write(buf, 0, numRead);
                }
                out.close();
            }
            catch (java.io.IOException e) {}
        }
        public static void main(String args[]){
            try {
                Key k = new SecretKeySpec( "01234567".getBytes(), "DES" );
                Encrypting encrypter = new Encrypting(k);
                encrypter.encrypt(new FileInputStream("input.jpg"),
                new FileOutputStream("output.jpg"));
            } catch (Exception e) {}
        }
}


and decrypting:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Decrypting {
Cipher dcipher;
        Decrypting(Key key){
                byte[] iv = new byte[]{
                (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
                (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
            };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            try {
                dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
                dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            }
            catch (java.security.InvalidAlgorithmParameterException e) {}
            catch (javax.crypto.NoSuchPaddingException e) {}
            catch (java.security.NoSuchAlgorithmException e) {}
            catch (java.security.InvalidKeyException e) {}
        }
        byte[] buf = new byte[1024];
        public void decrypt(InputStream in, OutputStream out) {
            try {
                // Bytes read from in will be decrypted
               in = new CipherInputStream(in, dcipher);
   
                // Read in the decrypted bytes and write the cleartext to out
               int numRead = 0;
                while ((numRead = in.read(buf)) >= 0) {
                    out.write(buf, 0, numRead);
                }
                out.close();
            } catch (java.io.IOException e) {
            }
        }
        public static void main(String args[]){
            try {
                Key k = new SecretKeySpec( "01234567".getBytes(), "DES" );
                Decrypting decrypter = new Decrypting(k);
                decrypter.decrypt(new FileInputStream("output.jpg"),
                new FileOutputStream("outputDEC.jpg"));
            } catch (Exception e) {}
        }
}

Now my input Pic has
8460668 Bytes,....
my encrypted Pic has
8460672 Bytes.....
and the decrypted Pic has
8460668 Bytes

What do you think?
Is this encryption efficient enough to hold gamers modify games?

Sorry for my wrong Post!
could any mod please move this topic to Newless Clubies?
4  Game Development / Shared Code / Image Encrypter/Decrypter on: 2009-01-23 02:16:35
Hope Im right here.
i try to encrypt/decrypt images but something is going wrong.
Example:
my Input.png Image has a size of 8,1 KB and the encrypted Output.png has only a size of 7,4 KB.

if you decrypt the Output.png in another "working" image, the size is always 7,4 KB.
With small Images the difference of qualitylost isnt big, but with bigger Files, u can see the difference .
hope someone could help me.

here the code for encrypting:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
                (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
        {
         SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
         PBEKeySpec pbeKeySpec = new PBEKeySpec("THISSHOULDNOBODYREAD".toCharArray());
         PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
         SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
         Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
         pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
         
         File inputFile = new File("input.png");
         BufferedImage input = ImageIO.read(inputFile);
         FileOutputStream output = new FileOutputStream("inputENC.png");
         CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
         ImageIO.write(input,"png",cos);
         cos.close();
         }
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!