Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Alternative to drawImage to draw a tile as a texture?  (Read 1906 times)
0 Members and 1 Guest are viewing this topic.
Offline qgpr

Senior Newbie




yawn


« Posted 2008-04-13 02:20:23 »

Hello, I am new to Java 2d and have been building a program to learn it. I have been drawing to a JPanel overriding the paint method. First a solid color, then an image, then a section of the image (I hear its called a tile), now I was trying to draw that section as a "texture" on a arbitrary section of the panel, but there is no drawImage function for that, becuase if the rectangle area is bigger than the tile then the tile gets scaled to fit instead of replicated.

I googled and found "TexturePaint" but also a lot of people saying its way too slow. Is there a better function for this or would I have to draw it manually calling drawImage several times? (yawn)

Hmm, what If I wanted to draw a tile on a rectangle section that would allow lets say 10 tiles horizontally, but I only want to draw it twice, is manual drawing the only way for that case? ( i was thinking on a if before the drawing that would reduce the rectangle area in this case, so that the texture filling could be used for this too)

Something like drawImage( image, dest x1, dest y1, dest  x2, dest y2,  src image x1, src image y1, src image x2, src image y2, repeat-x, repeat-y  ), where repeat-x/y can be set to a finite or infinite value (although if it is just infinite it would work for me).


EDIT: Yay I managed to code it "manually", this works as I want, although I believe the app was just a bit slower than usual, so if there's a better way to do this please tell me:



Vector3D and Vector2D hold x,y and z, 3D uses doubles (for 3d apps) and 2D uses long (for 2d apps).
"renderPos" already has the "point" where I want to render.and "size" the rectagular area's size I want to render to, both define the rectangular area that has to be textured.

"material" = sprite = wrapper for the texture, the texture is a tile image.
getRectStart and getRectEnd have the x1,y1 x2,y2 coordinates in the texture of the tile I want to draw.

I implemented features that remind me of CSS: repeat-x/y and x/y-offset, repeat-x is more like "draw this many times in x", so a value of zero is taken as do not draw, and "infinite" times is Integer.MAX_VALUE.

offset is the texture offset relative to the top left corner of the rectangle area, it doesn't have "standard" behavior but I'm liking the current behavior.
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  
53  
54  
55  
56  
   public void RenderQuad( java.awt.Graphics2D g, app.math.Vector3D renderPos, app.math.Vector3D size )
   {
      int timesRepeatedOnX = 0;
      int timesRepeatedOnY = 0;
     
      if( material.getRepeatOnX() == 0 || material.getRepeatOnY() == 0 ) return;
     
      app.math.Vector2D x1 = new app.math.Vector2D();
      x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
      x1.y = (int) Math.round( renderPos.y + material.getYOffset() );
      x1.z = 0;
     
      app.math.Vector2D x2 = new app.math.Vector2D();
      x2.x = x1.x + material.getTileWidth();
      x2.y = x1.y + material.getTileHeight();
      x2.z = 0;
     
      app.math.Vector2D finalx2 = new app.math.Vector2D();
      finalx2.x = (int)Math.round(renderPos.x + size.x);
      finalx2.y = (int)Math.round(renderPos.y + size.y);
      finalx2.z = 0;
     
      java.awt.Rectangle r = g.getClipBounds();
      g.setClip( (int) Math.round(renderPos.x), (int) Math.round(renderPos.y), (int)Math.round(size.x), (int)Math.round(size.y) );
     
      Texture t = getTextureToRender();
      while( true )
      {
         g.drawImage( t.get(), (int)x1.x, (int)x1.y, (int)x2.x, (int)x2.y, (int)getRectStart().x, (int)getRectStart().y, (int)getRectEnd().x, (int)getRectEnd().y, null );
         
         x1.x = x2.x;
         //x1.y = x2.y;
         //x1.z = 0;
         
         x2.x += material.getTileWidth();
         
         ++timesRepeatedOnX;
         if( x1.x >= finalx2.x || timesRepeatedOnX >= material.getRepeatOnX() )
         {
            x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
            x1.y = x2.y;
            x2.x = x1.x + material.getTileWidth();
            x2.y += material.getTileHeight();
            //x2.z = 0;
           
            timesRepeatedOnX = 0;
            ++timesRepeatedOnY;
         }
         if( x1.y >= finalx2.y || timesRepeatedOnY >= material.getRepeatOnY() )
         {
            break;
         }
      }
     
      g.setClip(r);
   }
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #1 - Posted 2008-04-14 22:22:57 »

FYI: in 6u10 (http://jdk6.dev.java.net) texture paint is hardware accelerated, and
is very fast.

Dmitri
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!