Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    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.

    import java.awt.*;
    class GraphicsProgram extends Canvas{
        public GraphicsProgram(){
            setSize(200, 200);
        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
        public void paint(Graphics g)
        	map1 test = new map1();
            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?

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts