473,396 Members | 1,968 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,396 software developers and data experts.

Displaying images in Java

I'm kinda new to programming but I usually figure things out by myself but I need help with this. I'm trying to display an image in the Center region of a JFrame and other icons/button in the other regions. So far, I have this:

Expand|Select|Wrap|Line Numbers
  1. public class RiskFrame extends JFrame implements ActionListener, MouseListener{
  2.  
  3.     private BufferedImage bImage;
  4.     private Image image;
  5.  
  6.     private JPanel topPanel = new JPanel();
  7.     private JPanel bottomPanel = new JPanel();
  8.     private JPanel centerPanel = new JPanel();
  9.     private JPanel rightPanel = new JPanel();
  10.  
  11.     private JButton saveButton = new JButton("Save", new ImageIcon("save.jpg"));
  12.     private JButton openButton = new JButton("Open", new ImageIcon("OpenIcon.gif"));
  13.     private static JButton newButton = new JButton("New");
  14.     private JButton addTerritory = new JButton("Add New");
  15.     private JButton removeTerritory = new JButton(" Remove");
  16.  
  17.     private JLabel goodStatus = new JLabel();
  18.     private JLabel badStatus = new JLabel();
  19.  
  20.     private JPanel territoryPanel = new JPanel();
  21.     private JLabel territoryInfo = new JLabel("Territory Info");
  22.     private JLabel country = new JLabel("Country: ");
  23.     private JTextField countryField = new JTextField(20);
  24.     private JPanel countryComp = new JPanel();
  25.     private JLabel continent = new JLabel("Continent: ");
  26.     private JTextField continentField = new JTextField(20);
  27.     private JPanel continentComp = new JPanel();
  28.     private JLabel cardType = new JLabel("Card Type: ");
  29.     private JTextField cardField = new JTextField(20);
  30.     private JPanel cardComp = new JPanel();
  31.     private JLabel redColor = new JLabel("R Color #: ");
  32.     private JTextField redColorField = new JTextField(20);
  33.     private JPanel redComp = new JPanel();
  34.     private JLabel greenColor = new JLabel("G Color #: ");
  35.     private JTextField greenColorField = new JTextField(20);
  36.     private JPanel greenComp = new JPanel();
  37.     private JLabel blueColor = new JLabel("B Color #: ");
  38.     private JTextField blueColorField = new JTextField(20);
  39.     private JPanel blueComp = new JPanel();
  40.  
  41.     public RiskFrame(){
  42.         layoutGUI();
  43.         centerPanel.addMouseListener(this);
  44.     }
  45.  
  46.  
  47.  
  48.     public void layoutGUI(){
  49.         topPanel.add(newButton);
  50.         topPanel.add(openButton);
  51.         topPanel.add(saveButton);
  52.         topPanel.add(addTerritory);
  53.         topPanel.add(removeTerritory);
  54.         bottomPanel.add(goodStatus);
  55.  
  56.         rightPanel.setLayout(new BorderLayout());
  57.         rightPanel.add(territoryInfo, BorderLayout.NORTH);
  58.         territoryPanel.setLayout(new BoxLayout(territoryPanel, BoxLayout.Y_AXIS));
  59.         territoryPanel.setAlignmentX(LEFT_ALIGNMENT);
  60.         territoryPanel.add(Box.createRigidArea(new Dimension(5,5)));
  61.  
  62.         countryComp.add(country);
  63.         countryComp.add(countryField);
  64.         continentComp.add(continent);
  65.         continentComp.add(continentField);
  66.         cardComp.add(cardType);
  67.         cardComp.add(cardField);
  68.         redComp.add(redColor);
  69.         redComp.add(redColorField);
  70.         blueComp.add(blueColor);
  71.         blueComp.add(blueColorField);
  72.         greenComp.add(greenColor);
  73.         greenComp.add(greenColorField);
  74.  
  75.         territoryPanel.add(countryComp);
  76.         territoryPanel.add(continentComp);
  77.         territoryPanel.add(cardComp);
  78.         territoryPanel.add(redComp);
  79.         territoryPanel.add(greenComp);
  80.         territoryPanel.add(blueComp);
  81.  
  82.         rightPanel.add(territoryPanel, BorderLayout.CENTER);
  83.         this.add(topPanel, BorderLayout.NORTH);
  84.         this.add(rightPanel, BorderLayout.EAST);
  85.         this.add(centerPanel, BorderLayout.CENTER);
  86.         this.add(bottomPanel, BorderLayout.SOUTH);
  87.  
  88.         newButton.addActionListener(this);
  89.         openButton.addActionListener(this);
  90.         saveButton.addActionListener(this);
  91.         addTerritory.addActionListener(this);
  92.         removeTerritory.addActionListener(this);
  93.     }
  94.  
  95.  
  96.  
  97.     public void actionPerformed(ActionEvent e){
  98.         String command = e.getActionCommand();
  99.         if (command.equals("New")){
  100.             newGame();
  101.         }
  102.         else if (command.equals("Save")){
  103.             save();
  104.         }
  105.         else if (command.equals("Open")){
  106.             open();
  107.         }
  108.     }
  109.  
  110.     public void newGame(){
  111.  
  112.         String inputValue = JOptionPane.showInputDialog("Please enter the name of the map you would like to use. (Map must already be saved in directory)");
  113.  
  114.         Icon icon = new ImageIcon(inputValue + ".png");
  115.         final JLabel imageLabel = new JLabel(icon); 
  116.         centerPanel.add(imageLabel);
  117.         repaint();
  118.  
  119.         try{
  120.             File file = new File(inputValue + ".board");
  121.             boolean success = file.createNewFile();
  122.             if (success){}
  123.             else {
  124.                 JOptionPane.showMessageDialog(null, "File already exists. Please try again");
  125.             }
  126.         }
  127.         catch (IOException e){}
  128.     }
  129.  
  130.     public void open(){
  131.  
  132.         String inputValue = JOptionPane.showInputDialog("Please enter the name of the board file you would like to use. (File must already be saved in directory)");
  133.  
  134.         try {
  135.         FileReader fr = new FileReader(inputValue + ".board");
  136.         BufferedReader br = new BufferedReader(fr);
  137.         }
  138.         catch(IOException e){}
  139.  
  140.         JLabel imageLabel = new JLabel();
  141.         ImageIcon icon = new ImageIcon(inputValue + ".png");
  142.         imageLabel.setIcon(icon);
  143.         centerPanel.add(imageLabel);
  144.         repaint();
  145.     }
  146.  
  147.     public void getColor(int x, int y){
  148.         Raster raster = bImage.getRaster();
  149.         Object data = raster.getDataElements(x, y, null);
  150.         ColorModel model = bImage.getColorModel();
  151.         int rgb = model.getRGB(data);
  152.         Color color = new Color(rgb);
  153.         String pixel = color.toString();
  154.         System.out.println(pixel);
  155.  
  156.     }
  157.  
  158.     public void mouseClicked(MouseEvent e) {
  159.         getColor(e.getX(), e.getY());
  160.     }
  161.  
  162.     public void save(){}
  163.  
  164.     public static void main(String[] args){
  165.  
  166.         RiskFrame frame = new RiskFrame();
  167.         frame.setSize(800,800);
  168.         frame.setVisible(true);
  169.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  170.     }
  171.  
It's not anywhere near done but everytime I run my program, instead of the image appearing in the center region, I will have to resize the window to get the image to appear even though I call the repaint method. Also, my getColor method doesn't work. I always get a nullPointerException when I try to click on the map.

Any help would be greatly appreciated. THANK YOU!!
Sep 30 '07 #1
1 2331
JosAH
11,448 Expert 8TB
You shouldn't add these components directly to the JFrame; first grab the frame's
'contentpane' and add all the components to that container. For an explanation
read the API documentation for the JFrame class.

kind regards,

Jos
Sep 30 '07 #2

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

Similar topics

11
by: Ian Davies | last post by:
Hello Im having problems displaying my images as thumbnails in a table. My code for producing the new width and height from the original image is as follows...
2
by: Woodmon | last post by:
I'm observing issue with linked images not displaying in either Firefox 1.5b2 or IE6 when running on Win XP (they display fine in either browser on W2k). Example problem HTML is: <a...
3
by: Dalan | last post by:
At first I was not certain what could cause Access 97 from displaying most jpeg images, but not all. After further testing, it seemed that all original images of less than 275 pixels per inch or...
1
by: David Lozzi | last post by:
Hello, I'm wondering whats the best method to use for displaying several photos' thumbnails. One method I know is to dynamically resize the photo at the time the page is loaded. What does this...
10
by: gnewsgroup | last post by:
I've googled and tried various approaches, but could not resolve this problem. The article at MSDN: Displaying Images in a GridView Column only presents a simple case where all data (including the...
1
by: ttamilvanan81 | last post by:
Hai everyone, I need to provide the slideshow for the images. I have upload the images into database. Then i will retrive all the images from the database and provide the slideshow for those...
5
by: Tom | last post by:
VS 2003/C# Have a axWebBrowser control that will not render images. Originally our app was just launching IE7 to display an HTML page. The bitmap images were not displaying - path was correct...
4
by: redpears007 | last post by:
Hi Again, Throwing this one out to you again as i am not getting anywhere and can find little to no information out there. I am currently displaying images (Jpegs) in access via the routine...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
1
by: flg22 | last post by:
Hi I am working on this Rock, paper, scissors java game and the program works, but I can not figure out how to get the images to load onto the program. So my question is how do I get the images to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.