Results 1 to 5 of 5

Thread: A little Help

  1. #1
    loggen is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default A little Help

    i am new to java graphics.. i am starting with simple code.. i have a simple code, according to my code there is a frame and the frame also has abutton.When i clik the button the drawpanel in center of frmae should repaint itself. but thats not working. when i clik the button it remains the same colour. Here is my code...


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SimpleGui3C implements ActionListener{
    	JFrame frame;
    
    	public static void main (String[] args) {
    
    		SimpleGui3C gui=new SimpleGui3C();
    		gui.go();
    }
    
    public void go(){
    
    	frame =new JFrame();
    
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	JButton button=new JButton("Changes Color");
    
    	button.addActionListener(this);
    
    	MyDrawPanel drawPanel=new MyDrawPanel();
    
    	frame.getContentPane().add(BorderLayout.SOUTH, button);
    
    	frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    
    	frame.setSize(300,300);
    	frame.setVisible(true);
    
    }
    
    public void actionPerformed(ActionEvent event){
    
    	frame.repaint();
    
    }
    
    }
    Last edited by loggen; 01-03-2009 at 05:01 PM.

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

    Default

    Problems:
    • I don't see the code for your DrawPanel class.
    • How will repainting the drawpanel make it change color? (again code would help here).
    • Your code is posted without code tags, and so it looses its formatting. You'll be far better off using these tags.


    To use code tags, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]

  3. #3
    loggen is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    sory for the posting. nw its ok.

  4. #4
    loggen is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    I got this code from a book. According to the book it should work. in the they says "The Drawing Panel's paintComponent()
    method is called every time the user clicks"

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

    Default

    I still haven't seen your MyDrawPanel code. With my MyDrawPanel class, your code works fine:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class SimpleGui3C implements ActionListener
    {
      JFrame frame;
    
      public static void main(String[] args)
      {
        SimpleGui3C gui = new SimpleGui3C();
        gui.go();
      }
    
      public void go()
      {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Changes Color");
        button.addActionListener(this);
        MyDrawPanel drawPanel = new MyDrawPanel();
        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.setSize(300, 300);
        frame.setVisible(true);
      }
    
      public void actionPerformed(ActionEvent event)
      {
        frame.repaint();
      }
    }
    
    class MyDrawPanel extends JPanel
    {
      private Color[] colors =
      {
        Color.black, Color.white, Color.blue, Color.cyan, Color.darkGray,
        Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink,
        Color.red, Color.yellow
      };
      private Random random = new Random();
      
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        g.setColor(colors[random.nextInt(colors.length)]);
        g.fillRect(0, 0, getWidth(), getHeight());
      }
    }

Posting Permissions

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