Zu den Aufzeichnungen der tutorials.de-Live-Workshops
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
2759
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Souldrinker2004 Souldrinker2004 ist offline Grünschnabel
    Registriert seit
    Feb 2005
    Beiträge
    4
    Für die, die es nicht wissen
    Sprites sind kleine Ausschnitte aus einem Bild.

    Ich lade z.B. ein Image über

    Image player = new ImageIcon(getClass().getResource("player.png")).getImage();

    Nun möchte ich das Bild in einzelne Sprites teilen
    z.B.

    xpos = {0,32,64};
    ypos = {0,0,0};
    width = {32,32,32};
    height = {48,48,48};

    Jetzt sollte er 3 kleinere Bilder machen mit den open angegebenen Positionen und Höhen und Weiten.

    Ich habe bereits alles ausprobiert und bin nicht drauf gekommen

    Solange

    Danke
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.724
    Blog-Einträge
    29
    Hallo!

    Versuchs mal hiermit:

    Code :
    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    
    /*
     * Created on 06.02.2005@14:00:31
     *
     * TODO Licence info
     */
    package de.tutorials;
     
    import java.awt.Graphics2D;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.VolatileImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
     
    /**
     * @author Administrator
     *
     * TODO Explain me
     */
    public class TilesTest extends JFrame {
     
        int screenWidth, screenHeight;
     
        final int TILE_WIDTH = 32;
     
        final int TILE_HEIGHT = TILE_WIDTH;
     
        int tilePosX;
     
        int tilePosY;
     
        final int TILE_COLUMN_CNT = 7;
     
        final int TILE_ROW_CNT = 6;
     
        VolatileImage[] tiles;
     
        BufferStrategy strategy;
     
        private Thread renderer = new Thread() {
            public void run() {
                int i = 0;
                while (true) {
                    Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                    g.clipRect(0, 0, screenWidth, screenHeight);
                    g.drawImage(tiles[i], tilePosX, tilePosY, TilesTest.this);
                    i = ++i % tiles.length;
                    g.dispose();
                    strategy.show();
                    try {
                        Thread.sleep(100L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
     
        public TilesTest() {
            super("TilesTest");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            screenWidth = 320;
            screenHeight = 240;
     
            tilePosX = (screenWidth - TILE_WIDTH) / 2;
            tilePosY = (screenHeight - TILE_HEIGHT) / 2;
            setSize(screenWidth, screenHeight);
            setResizable(false);
            setVisible(true);
            initGFX();
            createBufferStrategy(2);
            strategy = getBufferStrategy();
        }
     
        /**
         * 
         */
        private void initGFX() {
            try {
                BufferedImage tilesMap = ImageIO
                        .read(new File("c:/dg_humans32.gif"));
                int width = tilesMap.getWidth();
                int height = tilesMap.getHeight();
     
                tiles = new VolatileImage[TILE_ROW_CNT * TILE_COLUMN_CNT];
     
                for (int i = 0, y = 0; i < TILE_ROW_CNT; i++) {
                    for (int j = 0, x = 0; j < TILE_COLUMN_CNT; j++) {
                        VolatileImage vImg = this.createVolatileImage(TILE_WIDTH,
                                TILE_HEIGHT);
                        vImg.getGraphics()
                                .drawImage(
                                        tilesMap.getSubimage(x, y, TILE_WIDTH,
                                                TILE_HEIGHT), 0, 0, this);
                        tiles[i * TILE_ROW_CNT + j] = vImg;
                        x += TILE_WIDTH;
                    }
                    y += TILE_HEIGHT;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        public static void main(String[] args) {
            new TilesTest().start();
        }
     
        /**
         * 
         */
        private void start() {
            renderer.start();
        }
    }

    hier: http://www.molotov.nu/?page=graphics findest du eine kleine Sammlung von frei verfügbaren Tiles.

    Gruß Tom
    Angehängte Grafiken Angehängte Grafiken  
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

  3. #3
    Souldrinker2004 Souldrinker2004 ist offline Grünschnabel
    Registriert seit
    Feb 2005
    Beiträge
    4
    Man könnte doch auch ein BufferedImage machen welches so groß ist wie man die GFX haben will z.B. 32x32 dann macht man ein Graphics zu den BufferedImage und malt da ein Bild hinein welches auf der position x y die man haben möchte im negativen bereich ist.

    Dann ist genau die Position die man will im BufferedImage

    mfg
    soul
     

Ähnliche Themen

  1. Array mit CSS-Sprites. Wie Syntax?
    Von Jan-Frederik Stieler im Forum PHP
    Antworten: 12
    Letzter Beitrag: 16.05.10, 13:51
  2. Programm :: Images to Sprites zusammenfassen
    Von Don_Pazo im Forum Sonstige Grafik-Programme
    Antworten: 6
    Letzter Beitrag: 03.04.10, 19:45
  3. Wie erstelle ich Sprites?
    Von n00ki3 im Forum Smalltalk
    Antworten: 1
    Letzter Beitrag: 10.07.06, 13:49
  4. [sprites] verschwimmen beim tracen
    Von razor-awy im Forum Flash Plattform
    Antworten: 5
    Letzter Beitrag: 29.05.06, 16:21
  5. sprites zeichnen
    Von coolerouny im Forum Java
    Antworten: 6
    Letzter Beitrag: 08.04.05, 18:16