7 Replies - 675 Views - Last Post: 28 August 2013 - 09:10 PM Rate Topic: -----

#1 Evillium  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 18-January 13

Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 11:17 AM

Hi there, I have an array of objects. Array is called fleet, of the spaceShip object. Problem is that I want to add a space ship on mouse click that intializes an array slot and paints the image. I can add however many spaceships I want by increasing the amount in deployedShips, but for some reason I am having problems getting a new one to appear on mouse click.

I think this is a problem because it's an array, not an arrayList? Or it may be a problem with the way I'm trying to draw the images.

-Thank you for any help or replies!

Here are the important pieces:

Paints the spaceship

 public void drawSpaceships(Graphics g)
    {
       for(int i = 0; i < fleet.length; i++)
      {
         g.drawImage(spaceshipSprite, fleet[i].xpos, fleet[i].ypos, this);
         repaint();
      }
    }

method to initialize my fleet[] array with objects

 public void deployFleet()
    {
      spaceship.xpos = 350;
      spaceship.ypos = 350;
      shipsDeployed++;
      for(int i = 0; i < fleet.length; i++)
      {  
         fleet[i] = new SpaceShip(spaceship.xpos+=30, spaceship.ypos-=20,spaceship.pressed);
          
      }
      System.out.println(shipsDeployed);
    }

Mouse clicked method

public void mouseClicked(MouseEvent e) 
   {
      for(int i = 0; i < yp.length; i++)
      {
         if(new Rectangle(yp[i].xpos, yp[i].ypos, yp[i].height,yp[i].width).contains(x, y)) 
		   {
		      showTooltip = true;
            
            System.out.println(i);
		   }
      }         
      for(int i = 0; i < fleet.length; i++)
      {
         if(new Rectangle(fleet[i].xpos, fleet[i].ypos, 30,30).contains(x, y)) 
		   {      
            System.out.println("Ship: "+i);
            shipsDeployed++;
		   } 
      }
      
   }


Entire Source Code if needed.
import java.applet.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.Timer;
import java.util.*;


public class playingField extends Applet implements MouseListener, MouseMotionListener, ActionListener
{	
	//--- sprites
   
   private int x;
	private int y;
   private int time;
   private String version;
   private YellowPlanet[] yp;
   private BluePlanet[] bp;
   private AmethystPlanet[] amp;
   private SpaceShip[] fleet;
   
   private boolean showTooltip;
   
	private Image background;
   private int j;
   private Timer t;
   private Timer s;
   private Image[] redplanet;
   
   
   private Image ypSprite;
   private Image bpSprite;
   private Image ampSprite;
   private Image spaceshipSprite;
   
   private int yellowAmount;
   private int blueAmount;
   private int amethystAmount;
   
   private int redQuartz;
   private int yellowQuartz;
   private int blueQuartz;
   private int amethyst;
   private int credits;
   
   private int shipParts;
   private int health;
   private int multipler;
   private int shipsDeployed;
   
   private int totalPlanets;
    
   private Image backBuffer;
	private Graphics doubleG;
   private YellowPlanet yellowPlanet;
   private BluePlanet bluePlanet;
   private AmethystPlanet amethystPlanet;
   private SpaceShip spaceship;
  
	public void init()
   {
      version = "0.0.1";
      j = 0;
      time = 0;
      health = 100;
      
      
      //--Planet Gen---
      yellowAmount = 3;
      blueAmount = 3;
      amethystAmount = 2;
      //--------------
      shipsDeployed = 3;
      credits = 1500;
      blueQuartz = 0; 
      redQuartz = 0;
      yellowQuartz = 0;
      //showTooltip = false;
      
      
      setSize(1080,720);
      t = new Timer(1000, this);
      //s = new Timer(100, this);
      
      //---intialize object arrays-----
      yp = new YellowPlanet[yellowAmount];
      bp = new BluePlanet[blueAmount];
      amp = new AmethystPlanet[amethystAmount];
      fleet = new SpaceShip[shipsDeployed];
      //-------------------------------------

      yellowPlanet = new YellowPlanet(0,0,0,0,0,0, ypSprite);
      bluePlanet = new BluePlanet(0,0,0,0,0,0, bpSprite);
      amethystPlanet = new AmethystPlanet(0,0,0,0,0,0, ampSprite);
      spaceship = new SpaceShip(0,0,false);
      
      generateYellows();
      generateBlues();
      generateAmethysts(); 
      deployFleet();
            
      background = getImage(this.getDocumentBase(), "background2.png");
      ypSprite = getImage(this.getDocumentBase(), "yp1.png");
      bpSprite = getImage(this.getDocumentBase(), "bp1.png");
      ampSprite = getImage(this.getDocumentBase(), "amp1.png");
      spaceshipSprite = getImage(this.getDocumentBase(), "spaceship.png");
      
      //yellowPlanet = new Image[1];
      redplanet = new Image[10];
      loadImages();
      
      
      addMouseMotionListener(this);
      addMouseListener(this);   
	}
	public void paint(Graphics g)
   {
      if(j == 10)
      j = 0;

      Graphics2D g2d=(Graphics2D)g;
      g.drawImage(background, 0, 0, this);                 
      g.drawImage(redplanet[j], 150, 150, this);
     
      //g.drawImage(spaceshipSprite,180,150, this);
       
      //paint all yellow planets
      for(int i = 0; i < yellowAmount; i++)
      {  
         g2d.drawImage(ypSprite, yp[i].xpos, yp[i].ypos, yp[i].height, yp[i].height, this);
         repaint();  
      }
      //paint all blue planets
      for(int i = 0; i < blueAmount; i++)
      {
         g2d.drawImage(bpSprite, bp[i].xpos, bp[i].ypos, bp[i].height, bp[i].height, this);
         
         repaint();
      }
      //paint all amethyst planets
      for(int i = 0; i < amethystAmount; i++)
      {
         g2d.drawImage(ampSprite, amp[i].xpos, amp[i].ypos, amp[i].height, amp[i].height, this);
         
         repaint();
      }
      drawSpaceships(g);
      
      drawValues(g);
      mapInfo(g);
      if(showTooltip == true)
      {
         paintTooltip(g);
      }     
      else
      showTooltip = false;          	 
	}
   public void start() 
   {
      t.start();
      //s.start();
   }
   public void actionPerformed(ActionEvent actionEvent) 
   {
      j++;
      time++;
      repaint();
   }
	public void mouseClicked(MouseEvent e) 
   {
      for(int i = 0; i < yp.length; i++)
      {
         if(new Rectangle(yp[i].xpos, yp[i].ypos, yp[i].height,yp[i].width).contains(x, y)) 
		   {
		      showTooltip = true;
            
            System.out.println(i);
		   }
         
      }         
      for(int i = 0; i < fleet.length; i++)
      {
         if(new Rectangle(fleet[i].xpos, fleet[i].ypos, 30,30).contains(x, y)) 
		   {      
            System.out.println("Ship: "+i);
            shipsDeployed++;
		   }
         //if(!(new Rectangle(yp[i].xpos, yp[i].ypos, yp[i].height,yp[i].width).contains(x, y))) 
		   //{
		     // showTooltip = false;
         //   System.out.println("Not working?");      
		   //}
      }
      
   }
	public void mouseEntered(MouseEvent e)
   {
	}
   public void mouseExited(MouseEvent e)
   {
   }
   public void mousePressed(MouseEvent e)
   {
	}
   public void mouseReleased(MouseEvent e)
   {   
   }
   public void mouseDragged(MouseEvent e)
   {
   }
   public void mouseMoved(MouseEvent e)
   {
      x = e.getX();
      y = e.getY();
      showStatus("Mouse at (" + ( + e.getX() + "," + e.getY() + ")"));
   }
	 @Override
	 public void update(Graphics g) //double buffering
	 {
	 	if(backBuffer == null)
		{
			backBuffer = createImage(this.getSize().width, this.getSize().height);	
			doubleG = backBuffer.getGraphics();
		}
		doubleG.setColor(getBackground());
		doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
		doubleG.setColor(getForeground());
		paint(doubleG);
		
		g.drawImage(backBuffer, 0 ,0, this);
	 }
    public void loadImages()
    {
      for( int i = 0; i < redplanet.length; i++)
      {
         redplanet[i] = getImage(this.getDocumentBase(), "p" +(i+1) + ".png");  
      }
    }
    public void generateYellows()
    {
      Random r = new Random();
   
      for(int i = 0; i < yellowAmount; i++)
      {
         //if(yellowPlanet.xpos && yellowPlanet.ypos == )
         //{
            yellowPlanet.xpos = r.nextInt(650)+50;
            yellowPlanet.ypos = r.nextInt(650)+50;
            yellowPlanet.angle = r.nextInt(179)+1;
         //}
         totalPlanets++;
         yellowPlanet.height=r.nextInt(100)+20;
         yellowPlanet.resourceAmount=r.nextInt(100)+15;
         yellowPlanet.width=r.nextInt(100)+20;
         
         //(x, y, height, width, size, resourceAmount, image)
         yp[i] = new YellowPlanet(yellowPlanet.xpos,yellowPlanet.ypos,yellowPlanet.height,yellowPlanet.width,yellowPlanet.angle,yellowPlanet.resourceAmount,ypSprite);  
      }      
    }
    public void generateBlues()
    {
      Random r = new Random();
   
      for(int i = 0; i < blueAmount; i++)
      {
         //if(yellowPlanet.xpos && yellowPlanet.ypos == )
         //{
            bluePlanet.xpos = r.nextInt(650)+50;
            bluePlanet.ypos = r.nextInt(650)+50;
         //}
         totalPlanets++;
         bluePlanet.height=r.nextInt(100)+20;
         bluePlanet.resourceAmount=r.nextInt(100)+15;
         bluePlanet.width=r.nextInt(100)+20;
         
         //(x, y, height, width, size, resourceAmount, image)
         bp[i] = new BluePlanet(bluePlanet.xpos,bluePlanet.ypos,bluePlanet.height,
         bluePlanet.width,
         0,bluePlanet.resourceAmount,bpSprite);
         
      }      
    }
    public void generateAmethysts()
    {
      Random r = new Random();
   
      for(int i = 0; i < amethystAmount; i++)
      {
         //if(yellowPlanet.xpos && yellowPlanet.ypos == )
         //{
            amethystPlanet.xpos = r.nextInt(650)+50;
            amethystPlanet.ypos = r.nextInt(650)+50;
         //}
         totalPlanets++;
         amethystPlanet.height=r.nextInt(35)+20;
         amethystPlanet.resourceAmount=r.nextInt(70)+20;
         amethystPlanet.width=r.nextInt(70)+20;
         
         //(x, y, height, width, size, resourceAmount, image)
         amp[i] = new AmethystPlanet(amethystPlanet.xpos,amethystPlanet.ypos,
         amethystPlanet.height,amethystPlanet.width,
         0,amethystPlanet.resourceAmount,ampSprite);
         
      }      
    }
    public void drawValues(Graphics g)
    {
      g.setColor(Color.WHITE);
      Font font = new Font ("Serif", Font.PLAIN, 10);
      g.setFont (font);
      g.drawString("" +health, 866,337);
      g.drawString(""+redQuartz,909,227);
      g.drawString(""+blueQuartz,909,245);
      g.drawString(""+yellowQuartz,920,263);
      g.drawString(""+credits,909,190);
      g.drawString(""+shipsDeployed,913,410);

    }
    public void paintTooltip(Graphics g)
    {
      Font font = new Font ("Serif", Font.PLAIN, 11);
      g.setFont (font);
      
      
      for(int i = 0; i < yellowAmount; i++)
      {
         g.drawString("Planet weight: ",839,510);
         g.drawString("Resources: " +yp[i].resourceAmount,839,525);
         g.drawString("Years to mine: ",839,540);
         //System.out.println(yp[i].resourceAmount);  
      }
      repaint();
    }
    public void mapInfo(Graphics g)
    {
      g.setColor(Color.WHITE);
      Font font = new Font ("Serif", Font.PLAIN, 10);
      g.setFont (font);
      g.drawString("Planets:"+totalPlanets,10,10);
      g.drawString("Time elapsed:"+time,9,20);
      g.drawString("Version:"+version,9,30);
    }
    public void deployFleet()
    {
      spaceship.xpos = 350;
      spaceship.ypos = 350;
      shipsDeployed++;
      for(int i = 0; i < fleet.length; i++)
      {  
         fleet[i] = new SpaceShip(spaceship.xpos+=30, spaceship.ypos-=20,spaceship.pressed);
          
      }
      System.out.println(shipsDeployed);
    }
    public void drawSpaceships(Graphics g)
    {
       for(int i = 0; i < fleet.length; i++)
      {
         g.drawImage(spaceshipSprite, fleet[i].xpos, fleet[i].ypos, this);
         repaint();
      }
    } 
}


This post has been edited by Evillium: 28 August 2013 - 11:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Image painting on mouse click [Java Game/Applet]

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 11:22 AM


      g.drawImage(spaceshipSprite, fleet[i].xpos, fleet[i].ypos, this);
      repaint();



repaint() triggers a call to the paint() method
The first thing the paint() method does is to redraw the whole backgound based on the component background color.
So you should never call epaint() after having draw something
Was This Post Helpful? 1
  • +
  • -

#3 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 156
  • Joined: 19-July 13

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 11:25 AM

You should have a Timer that calls repaint() at set intervals. However, consider using a BufferStrategy to eliminate flickering.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 11:30 AM

View PostStrongJoshua, on 28 August 2013 - 02:25 PM, said:

However, consider using a BufferStrategy to eliminate flickering.

Not really needed... since 1.4 JPanel are double buffered
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 01:28 PM

Yup. The only times I would use a bufferstrategy is when I was doing something full screen and I wanted to take advantage of page flipping or triple buffering, or if I was doing something that required accuracy that a swing timer simply could not provide.
Was This Post Helpful? 1
  • +
  • -

#6 Evillium  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 18-January 13

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 05:26 PM

Never mind, I found a solution. I turn my array of objects into an arrayList so I didn't have a set number of spaceships and could paint them one at a time other than all at once.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 06:09 PM

View PostEvillium, on 28 August 2013 - 08:26 PM, said:

Never mind, I found a solution. I turn my array of objects into an arrayList so I didn't have a set number of spaceships and could paint them one at a time other than all at once.

Array/ArrayList has nothing to do... your problem ws to call repaint() in the paint() or in a method called by paint()

View Postfarrell2k, on 28 August 2013 - 04:28 PM, said:

Yup. The only times I would use a bufferstrategy is when I was doing something full screen and I wanted to take advantage of page flipping or triple buffering, or if I was doing something that required accuracy that a swing timer simply could not provide.

Or like in here :)

http://www.dreaminco...way-to-repaint/
Was This Post Helpful? 1
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Image painting on mouse click [Java Game/Applet]

Posted 28 August 2013 - 09:10 PM

I gave you a+1 for the effort in that post, but that is most certainly not the fastest way to paint with swing. you have to be in full screen exclusive mode where you can do page flipping. Blitting to the screen in windowed mode is not only shower, but also brings up the possibility of vsync issues with only two buffers. then there is the inaccuracy of the swing timer...

This post has been edited by farrell2k: 28 August 2013 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1