Results 1 to 2 of 2
  1. #1
    NoNickName is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Unhappy Resizing Images in JPanel??

    Ok I have this Bean machine game that I edited. One problem I have encountered...when I resize the frame, the picture/game does not resize. I have tried the repaint() method, but am I doing it wrong? Here is the code, any help is greatly appreciated.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Exercise14_24 extends JFrame { 
      private BeanMachinePanel paintPanel = new BeanMachinePanel();
      
      public Exercise14_24() {
        add(paintPanel);
    
    		
      }
    
      static class BeanMachinePanel extends JPanel {
        final static int HGAP = 20;
        final static int VGAP = 20;
        final static int RADIUS = 5;
        final static int LENGTH_OF_SLOTS = 40;
        final static int LENGTH_OF_OPENNING = 15;
        final static int Y_FOR_FIRST_NAIL = 50;
        final static int NUMBER_OF_SLOTS = 9;
        final static int NUMBER_OF_ROWS = NUMBER_OF_SLOTS - 2;
        
        public BeanMachinePanel() {  
    	   
    	 
    
        }
    
        /** Paint message */
    	 
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
    
    
          int y = Y_FOR_FIRST_NAIL;
          int xCenter = getWidth() / 2;
          
          // Draw pegs in multiple lines
          g.setColor(Color.GREEN);
          for (int i = 0; i < NUMBER_OF_ROWS; i++) {
            y += VGAP;
            for (int k = 0; k <= i; k++) {
              g.fillOval(xCenter - i * HGAP / 2 + k * HGAP - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
            }
          }
          
          // Draw vertical lines for slots
          g.setColor(Color.BLACK);
          y = y + RADIUS;
          for (int i = 0; i < NUMBER_OF_SLOTS; i++) {
            int x = xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + (i - 1) * HGAP;
            g.drawLine(x, y, x, y + LENGTH_OF_SLOTS);
          }
          
          // Draw a horizontal line for bottom
          g.drawLine(xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y + LENGTH_OF_SLOTS, 
            xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y + LENGTH_OF_SLOTS);
          // Draw two side lines
          g.drawLine(xCenter + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y);
          g.drawLine(xCenter - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y);
          // Draw two vertical lines for the openning
          g.drawLine(xCenter - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING);
          g.drawLine(xCenter + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter + HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING);
    		
    		
    		
    		    }
      }
    
      public static void main(String[] args) {
        JFrame frame = new Exercise14_24();
        frame.setTitle("BEAN MACHINE");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
    	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
      
    
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    edit Didn't read the code carefully, sorry
    Last edited by DarrylBurke; 04-09-2009 at 10:22 PM.

Similar Threads

  1. JFrame resizing problem
    By Bluefox815 in forum AWT / Swing
    Replies: 7
    Last Post: 02-24-2011, 07:56 PM
  2. panel resizing
    By kumar_gemi in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:51 AM
  3. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 02:54 PM
  4. Image Resizing and DPi
    By Rob_ in forum Java Applets
    Replies: 4
    Last Post: 11-27-2008, 02:21 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •