I’m creating a server/client mmo as a summer project before I head back to high school, and I chose to use Reddwarf and swing for my client side. I created a GameCanvas object that extends (you guessed it) Canvas, and overrode the paint method.
Here is my paint method:
/**
* Base for drawing the map, player, etc.
*/
@Override
public void paint(Graphics graphics) {
graphics.dispose();
if(strat == null) {
return;
}
Graphics2D g = (Graphics2D) strat.getDrawGraphics();
g.drawString(String.valueOf(System.currentTimeMillis()), 200, 200);
g.dispose();
strat.show();
}
Strat is a two layer bufferstrategy and is pulled from the containing JFrame (which has many many other components)
The GameCanvas is also inside of a JPanel that contains nothing else other than the GameCanvas itself.
I run my program and I can see my current time in millis with some messed up digits towards the end, and the entire JFrame and all containing components are gray and bugged out.
What is happening? O-o I have done a lot of stuff with canvas’ before and nothing like this ever happened. :/
If I had to guess whats happening I would say you are not painting the background before you paint the text.
You say this is a Swing question and yet you use a Canvas. Swing is double buffered by default and I see no reason to use AWT components with a buffer stategy.
I have never created an MMO before but I would guess that internet response time will be far slower than Swing painting time would ever be. So I would suggest you just use a JPanel if you need custom painting and override the paintComponent() method to do your painting. Don’t forget to invoke super.paintComponent() at the start.