473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2610
BigDaddyLH
1,216 Recognized Expert Top Contributor
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 Contributor
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
1791
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 problems to display images. Anyone has seen this problem before? I use PHP, MySQL and Apache on WinXP. Thanks
0
1419
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. That was poorly explained to I'll give a brief demo <chapter> <section> <section id="My_outer_section"><title>Outer Section</title>
2
2746
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 is VERY difficult to copy images off the site. The first thing I was going to do was force folks to turn on JavaScript so that I could disable the right click pop up menu. To force this, I thought I would have all the default images say that...
4
2582
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 that is what I would like, the words around the picture would be separate image in the final: www.nyccure.com/goal.html This works:
3
2237
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 internet explorer is that sometimes it works, sometimes it loads part of the image and sometimes it doesn't load the image at all. is there any way I can get this to work without preloading the images (there are a lot of images on the page and it...
4
1192
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 the images doesnt get dowloaded every time rather if page load has 300 images then only distinct images get downloaded all other are getting displayed by taking images from cache. pls suggest me solution as early as possible..
2
1858
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 that may never happen in the majority of cases. What would be the best way to go about placing these images in a separate resource that gets compiled to a DLL? Additionally, once I've done that, how can I load the images from that external DLL? ...
6
1752
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 http://www.altmarvel.net/Liam/OCOC.css. Anyway, the problems are these: 1. The Search stuff underneath the menu and to the right should be
0
1718
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 swing; however, the pre-written code only allows for the insertion of solid colored objects, not images. The pre-written code was divided into Game (which was a child of JFrame) and GameObject (which was a child of JComponent). The original paint...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.