Results 1 to 6 of 6
  1. #1
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Need Help putting 3 text strings on a jpeg.

    Firstly, I am really new to java, so my comprehension of the jargon is very weak. Long story short, I had an assignment to create a random credit card number with various checks built in. I accomplished this. Then I decided that I wanted to print the information on a jpeg, to make it actually look like a credit card. I have been around the internet hunting and pecking for solutions. At this stage, I have problems with
    > when I run this code as a stand alone program, the font does not change on the text that ends up on the screen, but other components seem to work. This makes me think I'm close to finding a solution.
    > when I run this code as part of my larger program, I get an error message related to the use of "this" in the MediaTracker and create graphics lines (exact error message is: non static variable this cannot be refernced from a static context)

    I have cut and pasted a bunch of solutions, so the code below may be really wonky.... the good news is I am learning..... any help in not too much jargon, would be greatly appreciated.

    James
    Java Code:
                 cardHolderName = firstName + " " + lastName;
                 cardNumber = fourForm.format(firstFour) + " "
                                + fourForm.format(secondFour) + " "
                                + fourForm.format(thirdFour) + " "
                                + fourForm.format(fourthFour);
                 expiryDate = thisMonth + "/" + expiryYear;
                 Font f = new Font("Serif",Font.BOLD,20);
                 label1 = new JLabel("Credit Card");
                 label1.setFont(f);
            
                 MediaTracker mt = new MediaTracker(this);
                 image = Toolkit.getDefaultToolkit().createImage("Bank of Yumol credit card.jpg");
                 mt.addImage(image,0);
                 try{mt.waitForID(0);}catch(InterruptedException ie){}
                 width = image.getWidth(null);
                 height = image.getHeight(null);
                
                 BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                 bimg.createGraphics().drawImage(image, 0, 0, this);
                 bimg.getGraphics().setFont(f);
                 bimg.getGraphics().drawString(cardNumber,250,130);
                 bimg.getGraphics().drawString(expiryDate,100,250);
                 bimg.getGraphics().drawString(cardHolderName,250,190);
                 
                 ImageIcon img = new ImageIcon(bimg);
                 label1 = new JLabel(img);
                 JPanel p = new JPanel();
                 p.add(label1);
                 getContentPane().add(p);
         
                 Test tt = new Test();
                 tt.setDefaultCloseOperation(EXIT_ON_CLOSE);
                 tt.setSize(560,320);
                 tt.setVisible(true);
    Last edited by Jadeclan; 10-24-2011 at 05:51 AM.

  2. #2
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need Help putting 3 text strings on a jpeg.

    I replaced "this" with "null" in the MediaTracker and createGaphics lines. This eliminated the error message, but created a new one at the getContentPane().add(p) line (5th from bottom). Message is "non static method getContentPane() can not be referenced from a static context".

    I still am having no luck figuring out the problem with the font size.
    Last edited by Jadeclan; 10-24-2011 at 04:47 AM.

  3. #3
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need Help putting 3 text strings on a jpeg.

    Turns out I had serious confusion over creating a class, using it and testing it all within the same project. I think I have that sorted out now.

    Next challenge is to get the font to work. It was a minor problem, so I haven't tried to find a solution. I am on my way to find a solution, but any thoughtts that will help me shortcut my search would be appreciated. lines 21 to 23 is where I really want the font to be working.

  4. #4
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need Help putting 3 text strings on a jpeg.

    Here is a cleaned up version of the Credit_Card_Printer class I have stolen/created: Font still not working......

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    
    public class Credit_Card_Printer extends JFrame
        {
         int width;             // width of background image
         int height;            // height of background image
         
         String s;              // card number
         String s1;             // expiry date
         String s2;             // persons name
         ImageIcon img;         // image of the painted credit card
         Image image;           // background image to be used for credit card
         JPanel p;              // panel created to display credit card
         JLabel label1;         // JLabel created to display credit card
       
         public Credit_Card_Printer( String cardHolderName, String cardNumber, String expiryDate)
         {
             s = cardNumber;
             s1 = "Expiry Date " + expiryDate;
             s2 = cardHolderName;
             
             Font f = new Font("Arial",Font.PLAIN,30);
            
             MediaTracker mt = new MediaTracker(this);
             image = Toolkit.getDefaultToolkit().createImage("Bank of Yumol credit card.jpg");
             mt.addImage(image,0);
             try{mt.waitForID(0);}catch(InterruptedException ie){}
             width = image.getWidth(null);
             height = image.getHeight(null);
             BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
             bimg.createGraphics().drawImage(image, 0, 0, this);
             bimg.getGraphics().setFont(f);
             bimg.getGraphics().drawString(s,250,130);
             bimg.getGraphics().drawString(s1,100,250);
             bimg.getGraphics().drawString(s2,250,190);
                    
             img = new ImageIcon(bimg);
             label1 = new JLabel(img);
             p = new JPanel();
             p.add(label1);
             getContentPane().add(p);
        }
    }

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Need Help putting 3 text strings on a jpeg.

    I believe a new Graphics object is returned by getGraphics(). Try:
    Java Code:
    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphpics2D g = bimp.createGraphics();
    g.drawImage(image, 0, 0, this);          
    g.setFont(f);          
    g.drawString(s,250,130);          
    g.drawString(s1,100,250);          
    g.drawString(s2,250,190);

  6. #6
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need Help putting 3 text strings on a jpeg.

    A couple typos in your reply, but YAHOOOOOOO it worked.
    thanks a bunch

Similar Threads

  1. Replies: 2
    Last Post: 10-21-2010, 02:30 PM
  2. Replies: 3
    Last Post: 06-02-2010, 06:58 PM
  3. Extracting the text between two strings
    By vidya in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 11:04 PM
  4. searchin for more Strings in a Text
    By gazale_m in forum New To Java
    Replies: 8
    Last Post: 12-09-2008, 08:51 AM
  5. extracting text from jpeg
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 10-05-2008, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •