Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: drentsoft (posted 2014-06-05 18:16:37, viewed 52 times)

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   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179  
Game Class:
[code]
private void initUI( boolean useUI ) {
        System.out.println("Loading UI");
        wnd = new JFrame( APP_TITLE );
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName() );
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
        // This bit shouldn't have anything to do with rendering but it's just here incase you can see anything I'm doing wrong
        if( useUI ) {
            lobby = new LobbyPanel( this );
            lobbyDlg = new JDialog( wnd, APP_TITLE + " Lobby", true );
            lobbyDlg.setContentPane(lobby);
            lobbyDlg.pack();
            lobbyDlg.setSize( lobby.getWidth() + 15, lobby.getHeight() + 15 );
            lobbyDlg.setLocationRelativeTo(null);
            lobbyDlg.setVisible(true);
        }
        pnl = new GamePanel( this );
        pnl.setBackground( Color.BLACK );
        wnd.setContentPane(pnl);
        wnd.pack();
        wnd.setSize( width, height );
        wnd.setLocationRelativeTo(null);
        wnd.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
        wnd.addKeyListener( new KeyManager(this) );
        MouseManager mm = new MouseManager(this);
        wnd.addMouseMotionListener( mm );
        wnd.addMouseListener( mm );
        wnd.addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                quit();
            }
            @Override
            public void windowClosed(WindowEvent e ) {
                quit();
            }
        });
        wnd.setVisible(true);
        wnd.createBufferStrategy(2);
        strat = wnd.getBufferStrategy();
        wnd.requestFocus();
    }

public void render( Graphics2D g ) {
        // pmr is just a time profiler.
        pmr.start();
        //g.scale( zoom, zoom );
        g.translate( -camera.x, -camera.y );
        g.setColor( Color.DARK_GRAY);
        g.fill(gameBounds);
        g.setColor(Color.lightGray);
        for( Rectangle obst : obstacles.values() ) {
            if( obst.intersects(camera) ) {
                g.fill(obst);
            }
        }
        for( Iterator<RenderableSplatter> i = splatters.iterator(); i.hasNext(); ) {            
            RenderableSplatter splat = i.next();
            if( splat.intersects(camera) ) {
                splat.render(g);
            }
        }
        if( !spectating ) {
            player.render(g);
        }
        for( RemoteRenderablePlayer p : players.values() ) {
            if( p.intersects(camera) ) {
                p.render(g);
            } else {
                p.renderIndicator(g);
            }
        }
        for( RemoteRenderablePlayer p : ghosts.values() ) {
            if( p.intersects(camera) ) {
                p.render(g);
            } else {
                p.renderIndicator(g);
            }
        }
        for( int i = 0; i < bullets.size(); i++ ) {
            if( bullets.get(i).intersects(camera) ) {
                bullets.get(i).render(g);
            }
        }
        if( !spectating ) {
            //Color[] colours = new Color[]{ new Color( 0.2f, 0.2f, 0.2f, 0.0f ), new Color( 0.3f, 0.3f, 0.3f, 1.0f ) };
            Color[] colours = new Color[]{ new Color( 0.0f, 0.0f, 0.0f, 0.0f ), new Color( 0.0f, 0.0f, 0.0f, 0.8f ) };
            RadialGradientPaint p;
            p = new RadialGradientPaint( new Point( (int) player.getCenterX(), (int) player.getCenterY() ), (float) camera.width * 2, new float[] { 0.1f, .25f}, colours );
            g.setPaint( p );
            g.fillRect(camera.x, camera.y, camera.width, camera.height);
        }
        g.translate( camera.x, camera.y );
        if( displayLeague ) {
            drawLeagueTable(g);
        }
        if( displayFPS ) {
            //System.out.println("Drawing FPS?");
            g.setColor( Color.WHITE );
            g.drawString("FPS: " + (int) (1000/pmr.getDelay()), 10, 20 );
        }
        pmr.end();
    }
    
    private void drawLeagueTable( Graphics2D g ) {
        int i = 0;
        g.setColor(Color.WHITE);
        g.drawString( "YOU", 200, 10 + 10 * 1 );
        g.drawString( "" + player.score, 300, 10 + 10 * 1 );
        for( RemoteRenderablePlayer p : ghosts.values() ) {
            g.drawString( "Player " + p.id, 200, 10 + 20 * 1 );
            g.drawString( "" + p.score, 300, 10 + 20 * 1 );
            i++;
        }
    }[/code]

JPanel Custom rendering code:
[code]@Override
    public void paintComponent( Graphics gs ) {
        //gs = parent.strat.getDrawGraphics();
        super.paintComponent(gs);
        Graphics2D g = (Graphics2D) gs;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
        g.scale( (double) parent.wnd.getWidth() / (double) parent.width, (double) parent.wnd.getHeight() / (double) parent.height );
        if( parent.running ) {
            parent.render(g);
        } else {
            g.setColor( Color.RED );
            g.setFont( new Font("Arial", Font.BOLD, 30 ) );
            String output = "";
            if( parent.client.failed ) {
                output = "Failed to connect. Nothing will happen.";
            } else {
                output = "Waiting to connect";
            }
            parent.metric = g.getFontMetrics();
            int width = parent.metric.stringWidth(output);
            int height = parent.metric.getHeight();
            g.drawString( output, (parent.width/2) - width/2, (parent.height/2) - height/2 );
        }
        g.dispose();
        //parent.strat.show();
    }
[/code]

Renderer class:
[code]    
    public void render() {
        Graphics2D g = (Graphics2D) game.strat.getDrawGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
        g.scale( (double) game.wnd.getWidth() / (double) game.width, (double) game.wnd.getHeight() / (double) game.height );
        g.setBackground( Color.BLACK );
        g.fillRect( 0, 0, game.width, game.height);
        if( game.running ) {
            game.render(g);
        } else {
            g.setColor( Color.RED );
            g.setFont( new Font("Arial", Font.BOLD, 30 ) );
            String output = "";
            if( game.client.failed ) {
                output = "Failed to connect. Nothing will happen.";
            } else {
                output = "Waiting to connect";
            }
            game.metric = g.getFontMetrics();
            int width = game.metric.stringWidth(output);
            int height = game.metric.getHeight();
            g.drawString( output, (game.width/2) - width/2, (game.height/2) - height/2 );
        }
        g.dispose();
        game.strat.show();
    }
[/code]





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!