de en es fr
Let the machine help
Light teasing, exhibition, BDSM, sissyfication, watersports... with sounds and pictures


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 SOLVED: Use of java to write a string
PostPosted: Thu Oct 22, 2015 6:35 pm 
Offline
Veteran
Hello Doti.

I saw your very nice code for using java to draw the images of cards dynamically in the script 'spankblackjack'.
This is a bit outside my current programming-knowledge, so I hope you can answer my question.
How should the code look like if I were to draw a string instead of an image?

I was thinking of using this to display a countdown timer/clock, at a place a bit more appropriate than with the rest of the text.

Hope you can help:)

- cd228

Can be done by the following code
Code:
def image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_4BYTE_ABGR)
   def graphics = image.getGraphics()
graphics.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 20));
   graphics.setPaint(java.awt.Color.BLACK);
   graphics.drawString("Hello", 20, 50);


Last edited by cd228 on Thu Oct 22, 2015 8:30 pm, edited 1 time in total.

Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 7:11 pm 
Offline
Site Admin
User avatar
Here is an excerpt of the white queen, with some text added at the end :
Code:
def showCards= { cards ->
   def width = 540
   def height = 500   
   def image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_4BYTE_ABGR)
   def graphics = image.getGraphics()
   def sampleImage = java.awt.Toolkit.getDefaultToolkit().getImage(
         "images/whitequeen/0.png")
   def oneWidth = sampleImage.getWidth()
   def oneHeight = sampleImage.getHeight()
   for(def i = 0;i<cards.size;i++)
      for(def j = 0;j<cards[i].size;j++) {
         def x = j * (width - oneWidth) / (Math.max(2,cards[i].size-1))
         def delta = (i*j + i * 5 + j * 7) % 4
         def y = i * (height - oneHeight - 3) / (Math.max(1,cards.size-1)) + delta
         def thisImage = javax.imageio.ImageIO.read(new File("images/whitequeen/"+cards[i][j]+".png"))
         if(i==0) {
            graphics.rotate(Math.PI, width/2, (oneHeight + 3)/2)
         }
         graphics.drawImage(thisImage, (int)x, (int)y, null)
         if(i==0) {
            graphics.rotate(-Math.PI, width/2, (oneHeight + 3)/2)
         }
      }

   //add text
   graphics.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16 ));
   graphics.drawString("Cute !", 20, 100);

   setImage(image, false)
}


This is Swing/Java, like in this documentation : http://docs.oracle.com/javase/7/docs/ap ... ics2D.html . This wont works with Android.


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 7:30 pm 
Offline
Veteran
This is awesome!, thank you.

If I were to change the font color, how would I do that?

I tried with:
Code:
graphics.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 20 ));
graphics.setColor(new java.awt.Color("BLACK"));
graphics.drawString("Cute !", 20, 100);


but it doesn't seem to work.


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 7:41 pm 
Offline
Site Admin
User avatar
I think it is the paint
Code:
graphics.setPaint(new java.awt.Color("BLACK"))


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 7:48 pm 
Offline
Veteran
doti wrote:
I think it is the paint
Code:
graphics.setPaint(new java.awt.Color("BLACK"))


Then I get the following error:
Error in simulation (maybe not a real error) groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.awt.Color(java.lang.String)(@ line 405)


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 8:09 pm 
Offline
Site Admin
User avatar
Code:
graphics.setPaint(java.awt.Color.BLACK)


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 8:19 pm 
Offline
Veteran
Hmm.. still doesn't work.


graphics.setPaint(java.awt.Color.BLACK);

produces

Error in simulation (maybe not a real error) groovy.lang.MissingPropertyException: No such property: java for class: ss.FullScript_Banjo_spankblackjack(@ line 404)



graphics.setPaint(new java.awt.Color.BLACK);

produces

Error in simulation (maybe not a real error) org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script14455378938321490595609.groovy: 405: unexpected token: ) @ line 405, column 44.
Paint(new java.awt.Color.BLACK);
^

1 error


Top
 Profile Send private message 
 
 Re: Use of java to write a string
PostPosted: Thu Oct 22, 2015 8:27 pm 
Offline
Veteran
Oops. My fault.

It does work:)

Thanks a lot.


Top
 Profile Send private message 
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010 - 2020
This website uses session cookies only.