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

    Default Java Paint Method?

    Hi, my code works fine but I want to put some graphics in my code and I already wrote a method and with joption it works fine but I want to use this code in jcombo box and I can't figure it out where to put because if I put under action performed it give me errors take a look at this code
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    
    // using JPanel as a drawing panel
    public class JPanelDemo1 extends JFrame implements ActionListener, ItemListener
    {
    
    
        JPanel southPanel;
        JPanel centerPanel;
        JComboBox myBox;
        public final int CIRCLE = 0, SQUARE = 1, RECT = 2, OVAL = 3;
     
    
            int red, green, blue;
            int x, y, width, height;
            int shape;
            
            Random rand = new Random();
    
      	 Container container;
        
        
        public JPanelDemo1()
        {
            
            container = getContentPane();
        	String shapesList[] = {"CIRCLE","SQUARE","OVAL","RECTANGLES"};    
         	
            southPanel = new JPanel();
            myBox = new JComboBox(shapesList);
    	myBox.addActionListener(this);
          	
            container.add( southPanel, BorderLayout.SOUTH );
            container.add(myBox,BorderLayout.SOUTH);
    	 
    
      
            setSize( 600, 500 );
            setVisible( true );
            setLocation( 100, 100 );
            
        }
    
    
    
     public void actionPerformed( ActionEvent ae )
         {
    
    	
              if (myBox.getSelectedItem().equals("CIRCLE"))
    		{
    		
    		System.out.println("Circles");
    		
    		}
    
    
    	  if (myBox.getSelectedItem().equals("SQUARE"))
    		{
    			System.out.println("Squares");
    		}
    
              if (myBox.getSelectedItem().equals("OVAL"))
    		{
    			System.out.println("OVAL");
    		}
    	  if (myBox.getSelectedItem().equals("RECTANGLES"))
    		{
    			System.out.println("Rectangles");
    		}		
    
    	 	
         }
    
    
    
      public void itemStateChanged( ItemEvent ie )
        {
     
             // actionPerformed(e);
        }
       
     
       
        public static void main( String[] arg )
        {
            JPanelDemo1 demo = new JPanelDemo1();
            demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            
        }
    }
    and the code I want to replace where it says console commands SYSTEM.OUT.PRINTLN

    Java Code:
     
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    
    // more efficient version of RandomShapes2
    public class RandomShapes3 extends JApplet
    {
        public final int CIRCLE = 0, SQUARE = 1, RECT = 2, OVAL = 3;
        
        
        public void paint( Graphics g )
        {
            int red, green, blue;
            int x, y, width, height;
            int shape;
            
            Random rand = new Random();
            
            super.paint( g );
            
            shape = Integer.parseInt( JOptionPane.showInputDialog( 
                            "Please choose the shape: \n" +
                            "0 - Circles\n" +
                            "1 - Squares\n" +
                            "2 - Rectangles\n" +
                            "3 - Ovals\n" +
                            "-1 - Quit" ) );
            
            while ( shape != -1 )
            {
                super.paint( g );
                
                switch( shape )
                {
                    case CIRCLE: 
                        for( int i = 0; i < 10; i ++ )
                        {
                            //super.paint( g );
                            
                            // set the values for the 7 variables
                            red = rand.nextInt( 256 );  // 0 - 255
                            green = rand.nextInt( 256 );
                            blue = rand.nextInt( 256 );
                            
                            x = 20 + rand.nextInt( 181 );  // 20 - 200
                            y = 20 + rand.nextInt( 181 );  // 20 - 200
                            width = 20 + rand.nextInt( 181 );  // 20 - 200
                            height = 20 + rand.nextInt( 181 );  // 20 - 200
                            
                            // set the color
                            g.setColor( new Color( red, green, blue ) );
                            g.fillOval( x, y, width, width );
                            
                            // put it to sleep for 100 ms
                            try
                            {
                                Thread.sleep( 100 );
                            }
                            catch ( Exception e ) {}
                        } // for
                        break;
                        
                    case OVAL: 
                        for( int i = 0; i < 10; i ++ )
                        {
                            //super.paint( g );
                            
                            // set the values for the 7 variables
                            red = rand.nextInt( 256 );  // 0 - 255
                            green = rand.nextInt( 256 );
                            blue = rand.nextInt( 256 );
                            
                            x = 20 + rand.nextInt( 181 );  // 20 - 200
                            y = 20 + rand.nextInt( 181 );  // 20 - 200
                            width = 20 + rand.nextInt( 181 );  // 20 - 200
                            height = 20 + rand.nextInt( 181 );  // 20 - 200
                            
                            // set the color
                            g.setColor( new Color( red, green, blue ) );
                            g.fillOval( x, y, width, height );
                            
                            try
                            {
                                Thread.sleep( 100 );
                            }
                            catch ( Exception e ) {}
                        }
                        break;
                        
                    
                    case SQUARE: 
                        for( int i = 0; i < 10; i ++ )
                        {
                            //super.paint( g );
                            
                            // set the values for the 7 variables
                            red = rand.nextInt( 256 );  // 0 - 255
                            green = rand.nextInt( 256 );
                            blue = rand.nextInt( 256 );
                            
                            x = 20 + rand.nextInt( 181 );  // 20 - 200
                            y = 20 + rand.nextInt( 181 );  // 20 - 200
                            width = 20 + rand.nextInt( 181 );  // 20 - 200
                            height = 20 + rand.nextInt( 181 );  // 20 - 200
                            
                            // set the color
                            g.setColor( new Color( red, green, blue ) );
                            g.fillRect( x, y, width, width );
                            
                            try
                            {
                                Thread.sleep( 100 );
                            }
                            catch ( Exception e ) {}
                        }
                        break;
                    
                    case RECT:  
                        for( int i = 0; i < 10; i ++ )
                        {
                            //super.paint( g );
                            
                            // set the values for the 7 variables
                            red = rand.nextInt( 256 );  // 0 - 255
                            green = rand.nextInt( 256 );
                            blue = rand.nextInt( 256 );
                            
                            x = 20 + rand.nextInt( 181 );  // 20 - 200
                            y = 20 + rand.nextInt( 181 );  // 20 - 200
                            width = 20 + rand.nextInt( 181 );  // 20 - 200
                            height = 20 + rand.nextInt( 181 );  // 20 - 200
                            
                            // set the color
                            g.setColor( new Color( red, green, blue ) );
                            g.fillRect( x, y, width, height );
                            
                            try
                            {
                                Thread.sleep( 100 );
                            }
                            catch ( Exception e ) {}
                        }
                        break;
                                    
                        //default:                                
                    }  // switch             
                    
                    
                
                shape = Integer.parseInt( JOptionPane.showInputDialog( 
                            "Please choose the shape: \n" +
                            "0 - Circles\n" +
                            "1 - Squares\n" +
                            "2 - Rectangles\n" +
                            "3 - Ovals\n" +
                            "-1 - Quit" ) );
            
            } // while
        }
            
        
        
    }
    so basically I want this joption version replace with Jcombo Box any inputs will be helpful thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Now you've got a whole program's worth of logic in the paint method. Again, don't do this unless you want your GUI's graphics to be as slow as molasses. Also, you cannot fully control when paint/paintComponent gets called and so this logic will recur erratically.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Also read the Swing Tutorials on painting: a Swing JComponent should call paintComponent( ... ), not paint( ... ), that's for AWT Components. The previous advice is very valid: don't put any program logic in that method because you don't know when it's called.

    kind regards,

    Jos

Similar Threads

  1. Something like "paint" in java
    By Antonio_Rodriguez in forum New To Java
    Replies: 7
    Last Post: 11-23-2009, 08:07 PM
  2. an error in paint method
    By hopey in forum Java 2D
    Replies: 7
    Last Post: 04-24-2009, 10:12 PM
  3. Some confusions with paint()
    By kendaop in forum Java Applets
    Replies: 1
    Last Post: 01-24-2009, 12:23 AM
  4. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 10:33 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 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
  •