Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-27-2012, 12:35 AM   PM User | #1
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
Drawing images in Java

Hi all.

I'm trying to use Java to make a map designer for a tabletop rpg. So far, its going fairly well, but I've got one big problem: When I display the map, if I manipulate the image box (make it bigger/smaller), it re-runs the entire program, including the methods that built the map in the first place- meaning that the entire map changes completly.

Code:
import java.awt.*;

class GraphicsProgram extends Canvas{

    public GraphicsProgram(){
        setSize(200, 200);
        setBackground(Color.white);
    }

    public static void main(String[] argS){

         //GraphicsProgram class is now a type of canvas
          //since it extends the Canvas class
          //lets instantiate it
        GraphicsProgram GP = new GraphicsProgram();  

        //create a new frame to which we will add a canvas
        Frame aFrame = new Frame();
        aFrame.setSize(1000, 1000);
        
        //add the canvas
        aFrame.add(GP);
        
        aFrame.setVisible(true);
    }

    public void paint(Graphics g)
    {
    	map1 test = new map1();
    	test.make1();
    	test.setElevation(1);
     
        g.setColor(Color.blue);
        g.drawLine(30, 30, 80, 80);
        g.drawRect(20, 150, 100, 100);
        g.fillRect(20, 150, 100, 100);
        g.fillOval(150, 20, 100, 100); 
  	  {	    
  	    for(int i = 0; i < 99; i++)
  	    {
  	    	for(int i2 = 0; i2 < 99; i2++)
  	    	{
  	    		int [] temp =test.cellArray[i][i2].getCol();
  	    		g.setColor(new Color(temp[0], temp[1], temp[2]));
  	    		g.fillRect((i*10), (i2*10), 10, 10);
  	    	}
  	    }
  	  }        
    }
}
So it runs the bolded section everytime it redraws the image. Is there a different way of doing images so it avoids this?
Atlan is offline   Reply With Quote
Old 07-27-2012, 03:46 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Depending on what is the construction for the Map1, then I'd also agree that the problem is what you have bold there.
Assign a member property for map1 for the GraphicsProgram class. Then it can be used without constructing a new object. Alternatively create a singleton for the map1 class.
As for better, you can write a class that takes care of the zooming for you. You may want to google that up for a pre-existing panel.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 PM.


Advertisement
Log in to turn off these ads.