Originally Posted by
Brandon
Ahh cool Idea. I think someone suggested that a while back but I didn't have too much free time.
If you're going to do that, don't forget that ClientPool access (if you're using one) should be synchronized if using multiple threads or Copy-Constructed in place such that changes to the original won't affect the copy (vice-versa).
Meh.. just came home from the engineering shop.. I'll think about it and I'll prob push an update for the current bugs later or tomorrow.. kinda tired lol.
My Client returns a copy of the buffer so it won't modify the actual buffer when resizing. I have it updating real time now, works smoothly. I still have to look in multiple threads, I hope you can answer my question in the tutorial thread :P
Has some unexpected behaviour though, game_crash seems to occur some times.. Not sure why.
java Code:
package org.obduro.loader.ui;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics2D;import java.awt.GridLayout;import java.awt.Image;import java.awt.image.BufferedImage;import java.util.HashMap;import java.util.Set;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JPanel;import org.obduro.loader.Client;import org.obduro.loader.utils.ClientPool;import org.obduro.loader.utils.Utilities;public class OverviewPanel
extends JPanel { private static final long serialVersionUID
= 5060519844144818836L
; private HashMap
<Integer, Client
> clients
; private HashMap
<Integer, JLabel
> labels
; private int width
= 1, height
= 1; public OverviewPanel
(){ setPreferredSize
(new Dimension(750,
570)); setBackground
(Color.
RED); setDoubleBuffered
(true); updateAll
(); } private ImageIcon resize
(BufferedImage image,
int number
){ Graphics2D g2d
= image.
createGraphics(); g2d.
setColor(Color.
WHITE); g2d.
setFont(new Font("Verdana",
Font.
BOLD,
40)); g2d.
drawString("Tab #" + number,
0,
50); Image resizedImage
= Utilities.
scaleImage(getWidth
()/width, getHeight
()/height, image
); return new ImageIcon(resizedImage
); } public void updateAll
(){ removeAll
(); clients
= ClientPool.
getClients(); labels
= new HashMap
<Integer, JLabel
>(); int size
= clients.
size(); while(width
* height
< size
){ if(width
< height
){ width
++; }else{ height
++; } } setLayout
(new GridLayout(width, height
)); Set
<Integer
> keys
= clients.
keySet(); int tabNumber
= 1; for(int key
: keys
){ Client client
= clients.
get(key
); ImageIcon icon
= resize
(client.
getGameBuffer(), tabNumber
++); JLabel label
= new JLabel(icon
); labels.
put(key, label
); add
(label
); } revalidate
(); } public void update
(int hashCode
){ Set
<Integer
> keys
= clients.
keySet(); int tabNumber
= 1; for(int key
: keys
){ if(key
== hashCode
){ JLabel label
= labels.
get(hashCode
); Client client
= clients.
get(hashCode
); ImageIcon icon
= resize
(client.
getGameBuffer(), tabNumber
++); label.
setIcon(icon
); label.
revalidate(); } tabNumber
++; } } }
And in the Client class
java Code:
/**
* Custom implementation of drawing the graphics
* @param g graphics to draw with
* @return graphics we have drawn
*/ public Graphics drawGraphics
(Graphics2D g
){ Graphics paintGraphics
= paintBuffer.
getGraphics(); paintGraphics.
drawImage(gameBuffer,
0,
0,
null); paintGraphics.
setColor(Color.
WHITE); paintGraphics.
drawString("Custom drawing hashcode #" + getCanvas
().
getClass().
getClassLoader().
hashCode(),
100,
100); paintGraphics.
dispose(); if(g
!= null){ g.
drawImage(paintBuffer,
0,
0,
null); } overviewPanel.
update(getCanvas
().
getClass().
getClassLoader().
hashCode()); return gameBuffer.
getGraphics(); }
I just give an instance of OverviewPanel and let it call the update method. Not the most efficient but it seems to get the job done..