473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problems with placing images on existing jframe. (screenshot included))

Hey there, ive been having difficulty placing an image, this is my screenshot, and what ive done so far:

http://img341.imageshack.us/img341/9894/gridwg2.jpg

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * ConnectFourGUI
  3.  * Provide the GUI for the Connect Four game
  4.  */
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7. import java.awt.*;
  8. import java.io.*;
  9.  
  10. public class ConnectFourGUI {
  11.  
  12.   private JLabel[][] slots;
  13.   private JFrame mainFrame;
  14.   private JTextField[] playerScore;
  15.   private ImageIcon[] playerIcon;
  16.   private JLabel nextPlayerIcon;
  17.  
  18.   private Color background = new Color(0, 0, 0);
  19.   private String gridimage;
  20.   private String logoIcon;
  21.   private String[] iconFile;
  22.  
  23.   private final String CONFIG = "config.txt"; 
  24.   /**
  25.    * Number of players
  26.    */
  27.   public final int NUMPLAYER = 2;
  28.  
  29.   /**
  30.    * Number of rows on the game board
  31.    */
  32.   public final int NUMROW = 6;//6
  33.  
  34.   /**
  35.    * Number of colums on the game board
  36.    */
  37.   public final int NUMCOL = 7;//7
  38.  
  39.   /**
  40.    * Number of games needed to be won to win the match
  41.    */
  42.   public int MAXGAME;
  43.  
  44.   private final int PIECESIZE = 70;
  45.   private final int PLAYPANEWIDTH = NUMCOL * PIECESIZE;
  46.   private final int PLAYPANEHEIGHT = NUMROW * PIECESIZE;
  47.  
  48.   private final int INFOPANEWIDTH = 2 * PIECESIZE;
  49.   private final int INFOPANEHEIGHT = PLAYPANEHEIGHT;
  50.  
  51.   private final int LOGOHEIGHT = 2 * PIECESIZE;
  52.   private final int LOGOWIDTH = PLAYPANEWIDTH + INFOPANEWIDTH;
  53.  
  54.   private final int FRAMEWIDTH = (int)(LOGOWIDTH * 1.02);
  55.   private final int FRAMEHEIGHT = (int)((LOGOHEIGHT + PLAYPANEHEIGHT) * 1.1);
  56.  
  57.   // Constructor:  ConnectFourGUI
  58.   // - intialize variables from config files
  59.   // - initialize the imageIcon array
  60.   // - initialize the slots array
  61.   // - create the main frame
  62.   public ConnectFourGUI () {
  63.     initConfig();
  64.     initImageIcon();
  65.     initSlots();
  66.     createMainFrame();
  67.   }
  68.  
  69.   private void initConfig() {
  70.     /*         - MAXGAME k
  71.      *         - logoIcon k
  72.      *         - iconFile k
  73.      */
  74.  
  75.     iconFile = new String[NUMPLAYER];
  76.     try { 
  77.       String line; 
  78.  
  79. //  FileReader read;
  80. //  BufferedReader in; 
  81. //  
  82. //  read = new FileReader (CONFIG);
  83. //  in = new BufferedReader (read);
  84.  
  85.       BufferedReader in = new BufferedReader(new FileReader( CONFIG ) ); 
  86.       line = in.readLine();
  87.       this.MAXGAME=Integer.parseInt(line);
  88.       line = in.readLine();
  89.       this.logoIcon = line;
  90.  
  91.       for(int i=0;i<NUMPLAYER;i++){
  92.         line = in.readLine();
  93.         this.iconFile[i] = line;
  94.       } 
  95.       in.close(); 
  96.     } catch ( IOException iox ) { 
  97.       showMessage("Config. file problem", CONFIG + " is missing");
  98. //  System.out.println("Problem reading " + fileName); 
  99.     } 
  100.  
  101.     // bACKUP
  102. //    logoIcon = "auraa.jpg";
  103. //    iconFile = new String[] {"alien pingu.jpg","rofl.jpg"};
  104. gridimage = "gridimage.jpg";
  105.   }
  106.  
  107.  
  108.   // initImageIcon
  109.   // Initialize playerIcon arrays with graphic files
  110.   private void initImageIcon() {
  111.     playerIcon = new ImageIcon[NUMPLAYER];
  112.     for (int i = 0; i < NUMPLAYER; i++) {
  113.       playerIcon[i] = new ImageIcon(iconFile[i]);
  114.     }
  115.   }
  116.  
  117.   // initSlots
  118.   // initialize the array of JLabels
  119.   private void initSlots() {
  120.     slots = new JLabel[NUMROW][NUMCOL];
  121.     for (int i = 0; i < NUMROW; i++) {
  122.       for (int j = 0; j < NUMCOL; j++) {
  123.         slots [i] [j] = new JLabel ();
  124.         // slots[i][j].setFont(new Font("SansSerif", Font.BOLD, 18));
  125.         slots[i][j].setPreferredSize(new Dimension(PIECESIZE, PIECESIZE));
  126.         slots [i] [j].setHorizontalAlignment (SwingConstants.CENTER);
  127.         slots [i] [j].setBorder (new LineBorder (Color.red));        
  128.       }
  129.     }
  130.   }
  131.  
  132.   // createPlayPanel
  133.   private JPanel createPlayPanel() {
  134.     JPanel panel = new JPanel(); 
  135.     panel.setPreferredSize(new Dimension(PLAYPANEWIDTH, PLAYPANEHEIGHT));
  136. //    panel.setBackground(background);
  137.     panel.setLayout(new GridLayout(NUMROW, NUMCOL));
  138.  
  139.     JLabel logo2 = new JLabel();
  140.     logo2.setIcon(new ImageIcon(gridimage));    
  141.  
  142.  
  143.  
  144.     for (int i = 0; i < NUMROW; i++) {
  145.       for (int j = 0; j < NUMCOL; j++) {
  146.         panel.add(slots[i][j]);
  147.       }
  148.     }
  149.     return panel;    
  150.   }
  151.  
  152.   // createInfoPanel
  153.   private JPanel createInfoPanel() {
  154.  
  155.     JPanel panel = new JPanel();
  156.     panel.setPreferredSize(new Dimension(INFOPANEWIDTH, INFOPANEHEIGHT));
  157.     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  158.     panel.setBackground (background);
  159.  
  160.     Font headingFont = new Font ("Serif", Font.PLAIN, 17);
  161.     Font regularFont = new Font ("Serif", Font.BOLD, 15);
  162.  
  163.     // Create a panel for the scoreboard
  164.     JPanel scorePanel = new JPanel();
  165.     scorePanel.setBackground(background);
  166.  
  167.     // Create the label to display "SCOREBOARD" heading
  168.     JLabel scoreLabel = new JLabel ("SCOREBOARD", JLabel.CENTER);
  169.     scoreLabel.setFont(headingFont);
  170.     scoreLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
  171.     scoreLabel.setForeground(Color.pink);
  172.  
  173.     // Create JLabels for players
  174.     JLabel[] playerLabel = new JLabel[NUMPLAYER];
  175.     for (int i = 0; i < NUMPLAYER; i++) {
  176.       playerLabel[i] = new JLabel(playerIcon[i]);
  177.     }
  178.  
  179.     // Create the array of textfield for players' score
  180.     playerScore = new JTextField[NUMPLAYER];
  181.     for (int i = 0; i < NUMPLAYER; i++) {
  182.       playerScore[i] = new JTextField();
  183.       playerScore[i].setFont(regularFont);
  184.       playerScore[i].setText("0");
  185.       playerScore[i].setEditable(false);
  186.       playerScore[i].setHorizontalAlignment (JTextField.CENTER);
  187.       playerScore[i].setPreferredSize (new Dimension (INFOPANEWIDTH - PIECESIZE - 10, 30));
  188.       playerScore[i].setBackground(background);
  189.       playerScore[i].setForeground(Color.pink);
  190.     }
  191.  
  192.     scorePanel.add(scoreLabel);
  193.     for (int i = 0; i < NUMPLAYER; i++) {
  194.       scorePanel.add(playerLabel[i]);
  195.       scorePanel.add(playerScore[i]);
  196.     }
  197.  
  198.     JPanel nextPanel = new JPanel();
  199.     nextPanel.setBackground(background);
  200.  
  201.     // Create the label to display "NEXT TURN" heading
  202.     JLabel nextLabel = new JLabel ("PLAYER TURN", JLabel.CENTER);
  203.     nextLabel.setFont(headingFont);
  204.     nextLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
  205.     nextLabel.setForeground(Color.pink);
  206.  
  207.     // Create the JLabel for the nextPlayer
  208.     nextPlayerIcon = new JLabel();
  209.     System.out.println(nextPlayerIcon.getAlignmentX());
  210.     nextPlayerIcon.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  211.     nextPlayerIcon.setIcon(playerIcon[0]);
  212.  
  213.     nextPanel.add(nextLabel);
  214.     nextPanel.add(nextPlayerIcon);
  215.  
  216.     panel.add(scorePanel);
  217.     panel.add(nextPanel);
  218.  
  219.     return panel;
  220.   }
  221.  
  222.   // createMainFrame
  223.   private void createMainFrame() {
  224.  
  225.     // Create the main Frame
  226.     mainFrame = new JFrame ("Connect Four");
  227.     JPanel panel = (JPanel)mainFrame.getContentPane();
  228.     panel.setLayout (new BoxLayout(panel,BoxLayout.Y_AXIS));
  229.  
  230.     // Create the panel for the logo
  231.     JPanel logoPane = new JPanel();
  232.     logoPane.setPreferredSize(new Dimension (LOGOWIDTH, LOGOHEIGHT));
  233.     JLabel logo = new JLabel();
  234.     logo.setIcon(new ImageIcon(logoIcon));
  235.     logoPane.add(logo);
  236.  
  237. //    JPanel gridPane = new JPanel();
  238. //    gridPane.setPreferredSize(new Dimension (490, 439));
  239. //    JLabel gridImage = new JLabel();
  240. //    gridImage.setIcon(new ImageIcon(gridimage));
  241. //    gridPane.add(gridImage);
  242.  
  243.  
  244.     // Create the bottom Panel which contains the play panel and info Panel
  245.     JPanel bottomPane = new JPanel();
  246.     bottomPane.setLayout(new BoxLayout(bottomPane,BoxLayout.X_AXIS));
  247.     bottomPane.setPreferredSize(new Dimension(PLAYPANEWIDTH + INFOPANEWIDTH, PLAYPANEHEIGHT));
  248.     bottomPane.add(createPlayPanel());
  249.     bottomPane.add(createInfoPanel());
  250.  
  251.     // Add the logo and bottom panel to the main frame
  252.     panel.add(logoPane);
  253.     panel.add(bottomPane);
  254. //    panel.add(gridPane);
  255.  
  256.     mainFrame.setContentPane(panel);
  257.     //   mainFrame.setPreferredSize(new Dimension(FRAMEWIDTH, FRAMEHEIGHT));
  258.     mainFrame.setSize(FRAMEWIDTH, FRAMEHEIGHT);
  259.     mainFrame.setVisible(true);
  260.   }
  261.  
  262.   /**
  263.    * Returns the column number of where the given JLabel is on
  264.    * 
  265.    * @param  label the label whose column number to be requested
  266.    * @return the column number
  267.    */
  268.   public int getColumn(JLabel label) {
  269.     int result = -1;
  270.     for (int i = 0; i < NUMROW && result == -1; i++) {
  271.       for (int j = 0; j < NUMCOL && result == -1; j++) {
  272.         if (slots[i][j] == label) {
  273.           result = j;
  274.         }
  275.       }
  276.     }
  277.     return result;
  278.   }
  279.  
  280.   public void addListener (ConnectFourListener listener) {
  281.     for (int i = 0; i < NUMROW; i++) {
  282.       for (int j = 0; j < NUMCOL; j++) {
  283.         slots [i] [j].addMouseListener (listener);
  284.       }
  285.     }
  286.   }
  287.  
  288.   /**
  289.    * Display the specified player icon on the specified slot
  290.    * 
  291.    * @param row row of the slot
  292.    * @param col column of the slot
  293.    * @param player player to be displayed
  294.    */
  295.   public void setPiece(int row, int col, int player) {
  296.     slots[row][col].setIcon(playerIcon[player]);
  297.   }
  298.  
  299.   /**
  300.    * Display the score on the textfield of the corresponding player
  301.    * 
  302.    * @param player the player whose score to be displayed
  303.    * @param score the score to be displayed
  304.    */
  305.   public void setPlayerScore(int player, int score) {
  306.     playerScore[player].setText(score+"");
  307.   }
  308.  
  309.   /**
  310.    * Display the appropriate player icon under"Next Turn"
  311.    * 
  312.    * @param player the player number of the next player; its corresponding icon will be displayed under "Next Turn"
  313.    */
  314.   public void setNextPlayer(int player) {
  315.     nextPlayerIcon.setIcon(playerIcon[player]);
  316.   }
  317.  
  318.   /**
  319.    * Reset the game board (clear all the pieces on the game board)
  320.    * 
  321.    */
  322.   public void resetGameBoard() {
  323.     for (int i = 0; i < NUMROW; i++) {
  324.       for (int j = 0; j < NUMCOL; j++) {
  325.         slots[i][j].setIcon(null);
  326.       }
  327.     }
  328.   }
  329.  
  330.   /**
  331.    * Display a pop up window displaying the message about a tie game
  332.    * 
  333.    */
  334.   public void showTieGameMessage(){
  335.     JOptionPane.showMessageDialog(null, " This game is tie.", "Tie Game", JOptionPane.PLAIN_MESSAGE, null); 
  336.   }
  337.  
  338.   /**
  339.    * Display a pop up window specifying the winner of this game
  340.    * 
  341.    * @param player the player number of the winner of the game
  342.    */
  343.   public void showWinnerMessage(int player){
  344.     JOptionPane.showMessageDialog(null, " won this round!", "This round has a winner!", JOptionPane.PLAIN_MESSAGE, playerIcon[player]); 
  345.   }
  346.  
  347.   /**
  348.    * Display a pop up window specifying the winner of the match
  349.    * 
  350.    * @param player the player number of the winner of the match
  351.    */
  352.   public void showFinalWinnerMessage(int player){
  353.     JOptionPane.showMessageDialog(null, " won the game with " + MAXGAME + " wins", "The game is finished", JOptionPane.PLAIN_MESSAGE, playerIcon[player]); 
  354.     System.exit (0);
  355.   }
  356.  
  357.   /**
  358.    * Display a pop up window displaying a desired message
  359.    * 
  360.    */
  361.   public void showMessage(String message,String title){
  362.     JOptionPane.showMessageDialog(null, message,title,2); 
  363.   }
  364.  
  365.   public static void main (String[] args) {
  366.     ConnectFourGUI gui = new ConnectFourGUI ();
  367.     ConnectFour game = new ConnectFour (gui);
  368.     ConnectFourListener listener = new ConnectFourListener (game, gui);
  369.  
  370.   }
  371.  
  372. }
Jan 19 '08 #1
2 2586
BigDaddyLH
1,216 Expert 1GB
What is you question? Also, I bet 90% of that code is irrelevant to whatever your question is. Post a SSCCE: http://mindprod.com/jgloss/sscce.html
Jan 19 '08 #2
lotus18
866 512MB
A very nice "Alien" image : )

Rey Sean
Jan 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phong Ho | last post by:
Hi everyone, I am using PHP and MySQL to create a dynamic website. Using MS Internet Explorer to test my website, everything works fine. When I use Netscape and Opera to test my work, I have...
0
by: Duncan Jones | last post by:
I'm having some problems placing a table in a section. The problem seems to arise when I have a small section within a section, and then try to place a table in the outer section afterwards. ...
2
by: Sam Carleton | last post by:
I feel like a complete fool! I should know the answer to the Q: How do I load an image with JS and replace the default image? Some background: My final objective is to have a web site where it...
4
by: Matt Silberstein | last post by:
I would like to have a page that has some images arrange around the center. The idea is to design it for 800 wide and just allow the margins to grow on larger displays. Here is a link to an image...
3
by: Ben | last post by:
Hi all I have a problem with a page I'm working on. It's a cateloge with a collection of thumbnails which, when clicked on display a larger picture in the middle. The problem that I have with...
4
by: Navu | last post by:
hi i have written a code in javascript and this code create lots of images at run time. Whenever page loads all these images get downloaded as 90% of images are same So is there any way so that...
2
by: Brian A. Cline | last post by:
After placing several images in an embedded resource file, the size of my program's executable has jumped considerably. I don't need these images until a specific dialog pops up, and it's possible...
6
by: Liam Gibbs | last post by:
Hello everyone, I'm trying to program a church web site and I'm having a number of problems with the layout. The html is at http://www.altmarvel.net/Liam/index.html and the css is at...
0
by: Corey S | last post by:
Hey, I'm trying to make a game for a school project that uses image files. I've been working on changing some pre-written code (I believe from IBM) that we used for pong as I'm not yet an expert on...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.