Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30421

NEED JAVA GRAPHICS HELP

Newsgroups comp.lang.java.programmer
Date 2014-11-26 09:37 -0800
Message-ID <3acddbde-4150-4384-9610-3622c67dccd9@googlegroups.com> (permalink)
Subject NEED JAVA GRAPHICS HELP
From bluebeatbang@gmail.com

Show all headers | View raw


I have 3 classes and everything seems to be right but the program doesn't show anything!
WHen I compile it, a new 1000 x 700 panel is shown, but nothing is drawn on the screen!
(My paint method is render() )
Thanks for help! :)


Main class:




	public class Main extends Canvas implements Runnable {
		
		public static final int WIDTH = 1000, HEIGHT = 700;
		public static boolean running = false;
		public Thread gameThread;
		
		public int grassX = -2000, grassY = -1400;

		
		//private Object obj
		private Background bg;
		
		public void init()
		{
			//obj = new Object();
			//this.addKeyListener(obj);
			bg = new Background();
			
		}
		
		public synchronized void start()
		{
			if(running)return;
			running = true;
			gameThread = new Thread(this);
			gameThread.start();
		}
		
		public synchronized void stop()
		{
			if(!running)return;
			running = false;
			try {
				gameThread.join();
			} catch (InterruptedException e) {e.printStackTrace();}
		}
		
		public void run()
		{
			init();
			long lastTime = System.nanoTime();
			final double amountOfTicks = 60D;
			double ns = 1000000000 / amountOfTicks;
			double delta = 0;
			
			while(running)
			{
				long now = System.nanoTime();
				delta += (now - lastTime) / ns;
				lastTime = now;
				if(delta >= 1)
				{
					tick();
					//COUNTERS HERE
					
					//COUNTERS END
					delta--;
				}
				render();
			}
		}
		public void tick()
		{
			
		}
		
		public void render()
		{
			BufferStrategy bs = this.getBufferStrategy();
			if(bs == null)
			{
				createBufferStrategy(3);
				return;
			}
			
			Graphics g = bs.getDrawGraphics();
			//RENDER
			bg.render(g);
			//END RENDER
			g.dispose();
			bs.show();
		}
		
		public static void main(String[] args)
		{
			Main game = new Main();
			game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
			game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
			game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
			
			JFrame frame = new JFrame("Game");
			frame.setSize(WIDTH, HEIGHT);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setResizable(false);
			frame.add(game);
			frame.setVisible(true);
			
			game.start();
			
			
		}
		
	}


Background class:





public class Background {
	
	public static int grassX = -2000, grassY = -1400;
	
	public void render(Graphics g)
	{
		Tiles.grass(g);
	}
	
}


Tiles Class:




public class Tiles {
	
	
	public static void grass(Graphics g)
	{
		while(Background.grassY <= 2800 + 700)
		{
			while(Background.grassX <= 5000)
			{
				g.setColor(new Color(0, 232, 0));
				g.fillRect(Background.grassX, Background.grassY, 10, 10);
				g.setColor(new Color(0, 128, 0));
				g.drawRect(Background.grassX, Background.grassY, 10, 10);
				Background.grassX += 10;
			}
			Background.grassY += 10;
		}
	}

}

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

NEED JAVA GRAPHICS HELP bluebeatbang@gmail.com - 2014-11-26 09:37 -0800
  Re: NEED JAVA GRAPHICS HELP Knute Johnson <eternal@knutejohnson.com> - 2014-11-26 15:43 -0800

csiph-web