Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / JScript .NET

Star Trek "Galactic Conquest" Game Contest Submission (Java)

Rate me:
Please Sign up or sign in to vote.
4.27/5 (6 votes)
6 Aug 2008CPOL3 min read 53.3K   951   25  
Star Trek "Galactic Conquest" Game Contest Submission (Java)
import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.net.URL;
import java.util.StringTokenizer;
/**
 * QuadDraw JPanel c/w Graphical Draw Override. 
 * 
 * @author Robert Bettinelli 
 * @version 1.0.0
 */
public class QuadDraw extends JPanel
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Image img;
    private Image xyLegend;
    private Image bkgImage;
    private String drawText;
    private int[] shot; 
    
    /**
     * Constructor for objects of class quadDraw
     * Load Screen Graphicas.
     */
    public QuadDraw()
    {
        // Constructor Initialize
        img = null;
        xyLegend = loadGraphic("XY.jpg");    // Upper Left XY Graphic
        bkgImage = loadGraphic("BKG.jpg");   // Load Screen Splash Type Graphic.
        drawText = "";                              // Clear Special Graphic Text
        shot = new int[4];                          // Inialize Shot Array
        shotReset();                                // Clear Shot To & From Array.
    }
    /**
     * shotReset - Clear Shot Dimension Array
     */
    private void shotReset()                         // Reset all to -1. (Non-Zero #)
    {
        shot[0] = -1;
        shot[1] = -1;
        shot[2] = -1;
        shot[3] = -1;
    }
    
    /**
     * setShot - Graphical Shotting Graphic. 
     *
     *@param x1 - Shoot From X Location
     *@param y1 -            Y Location
     *@param x2 -       To   X Location
     *@param y2 -            Y Location
     */
    public void setShot(int x1,int y1, int x2, int y2)
    {
        // Set to current graphical interface. 
        Graphics g;
        g = getGraphics();
          
        // Create Multiline Graphic Effect. 
        Color[] colr;
        colr = new Color[5];
        colr[0] = Color.yellow;
        colr[1] = Color.red;
        colr[2] = Color.blue;
        colr[2] = Color.red;
        colr[4] = Color.black;
        
        // Init set graphic X & Y Locations. 
        shot[0] = 24+(x1*48); // Graphical  X Location.
        shot[1] = 26+(y1*48); //            Y Location.
        shot[2] = 24+(x2*48); //            X Location.
        shot[3] = 26+(y2*48); //            Y Location. 
        
        // Calculate Shot in 4 Segments. 
        int deltaX = (shot[0] - shot[2])/4;
        int deltaY = (shot[1] - shot[3])/4;
        
        // Set Increment for current and next segments
        int incX1,incX2;
        int incY1,incY2;
        // loop for 5 different color lines
        // (or else its too fast to see)
        for(int x=0; x <= 4; x++)
        {
            // Set initial From Point (attacking Ship)
            incX1=shot[1];
            incY1=shot[0];
            for (int y=0; y < 4; y++)
            {
                // Set end segment point.
                incX2 = incX1 - deltaY;
                incY2 = incY1 - deltaX;
                // Set shot color
                g.setColor(colr[x]);
                // draw shot segment
                g.drawLine(incX1,incY1,incX2,incY2);
                // take some time so it can be smooth and viewable.
                timeOut(55);
                // next start point is end of previous.
                incX1 = incX2;
                incY1 = incY2;
            }
        }
        // Reset screen back to normal. 
        repaint();
        // Clear Shot Request.  
        shotReset();
    } 
    /**
     * Load Graphic
     * 
     * @param fileName - Graphic to load.
     * 
     * @return Image - Return Image requested
     */
    private Image loadGraphic(String fileName)
    {
        // Set Empty Image
        Image loadImg = null;
        try 
        {
            //Load jpg file. 
        	URL url = DisplayData.class.getResource("/Images/"+fileName);  
        	loadImg = ImageIO.read(url);
        	
        	if(loadImg == null)
        	  {    	
        		  System.out.println("Image object is null");    
        	  }
        	  else
        	  {    	
        		  System.out.println("Loading.. Images/"+fileName);    
        	  }       	
            //Media Tracker to Force Load to memory. 
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(loadImg, 1);
            try 
            {
                // Wait for Graphic to Load. 
                mt.waitForAll();
            } 
            catch (Exception e) 
            {
                System.out.println("Exception while loading.");
            }
        } 
        catch (Exception e) 
        {
        }
        return loadImg;
    }
    /**
     * displayText - Displays Text on the Graphics Display Screen. 
     */
    public void displayText(String text)
    {
        drawText = text;
        repaint();
    }
    
    /**
     * drawImage - Transfer Image to Screen. 
     * 
     * @param  image   get Image to Draw
     */
    public void drawImage(Image image)
    {
        img = image;
        repaint();
    }
    
    /**
     * paintComponent Override (Draw All things Graphic to the Panel) 
     */
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        // Title! 
        g.drawImage(bkgImage,0,0,336,336,this);
        g.setColor(Color.yellow);
        g.setFont(new Font("Courier New", Font.BOLD, 20));
        g.drawString(" GALACTIC ",100,150);
        g.drawString("!CONQUEST!",100,175);
        // Draw Image!
        g.drawImage(img,0,0,this);
        // Set Boarder Numbers!
        g.setFont(new Font("Courier New", Font.PLAIN, 8));
        g.setColor(Color.yellow);
        for (int x = 0; x < 7;x++)
        {
             g.drawString(""+x,23+(x*48),8); 
        }
        for (int y = 0; y < 7;y++)
        {
            g.drawString(""+y,2,26+(y*48)); 
        }
        g.drawImage(xyLegend,0,0,this); 
        
        //Draw Text.
        if (drawText.length() > 0)
        {
            int y1 = 50;
          // Split text
          StringTokenizer st = new StringTokenizer(drawText,"$");
          while (st.hasMoreTokens())
          {
               g.setFont(new Font("Courier New", Font.BOLD, 10));
               // Paint Text.
               g.drawString(st.nextToken(),30,y1);
               y1 += 15;
          }
          // Clear text.
          drawText = "";
        }
    }
    
    /**
    * Delay function to create Scroll Effect.
    */  
    private void timeOut(int delay)
    {
        synchronized(this)
        {
            try
            {
                wait(delay);
            }
            catch (InterruptedException iel)
            {
                //bypass   
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Nottawasaga Valley Conservation Authority
Canada Canada
I have background in programming(many languages), web design, hardware and software. Currently I hold a lead database programming position for the Nottawasaga Valley Conservation Authority... Programming in vb.net and java comes 2nd nature to me and of course I do this for fun. I also used to design in cobol and pascal and run a BBS(anyone remember what this was?). Anyways, drop me a line maybe I can help you.

Comments and Discussions