Jump to content

paint(Graphics g)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
mr.b

mr.b

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I faced a problem when I tried to solve this Exercis:

Quote

Design a Program “Shape Factory” that has Buttons to produce:
a.Random Rectangles with random colors.
b.Random Ellipses with random colors.
c.Random Lines with random colors.

I write this code and stopped

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


public class E1 extends JFrame implements ActionListener

{

    public static final int WIDTH = 500;

    public static final int HEIGHT = 300;

	Color c; 

    private JPanel mainPanel;


    public static void main(String[] args)

    {

        E1 gui = new E1( );

        gui.setVisible(true);

        

    }


    public E1( )

    {

        super("E1");

        setSize(WIDTH, HEIGHT);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout( ));


        JPanel biggerPanel = new JPanel( );

        biggerPanel.setLayout(new GridLayout(1, 1));


        mainPanel = new JPanel( );

        mainPanel.setBackground(Color.WHITE);

        biggerPanel.add(mainPanel);

        

        add(biggerPanel, BorderLayout.CENTER);


        JPanel buttonPanel = new JPanel( );

        buttonPanel.setBackground(Color.WHITE);

        buttonPanel.setLayout(new FlowLayout( ));



        JButton R = new JButton("Random Rectangles");

        R.addActionListener(this);

        buttonPanel.add(R);

        

        JButton E = new JButton("Random Ellipses");

        E.addActionListener(this);

        buttonPanel.add(E);


        JButton L = new JButton("Random Lines");

        L.addActionListener(this);

        buttonPanel.add(L);

        

        add(buttonPanel, BorderLayout.SOUTH);

    }

    public void paint(Graphics g)

	{

		int x,y,w,h;

		x = (int)(Math.random() * getWidth());

		y = (int)(Math.random() * getHeight());

		w = (int)(Math.random() * getWidth()) - x;

		h = (int)(Math.random() * getHeight())- y;

		c = new Color( (int) (Math.random() * 256) , (int) (Math.random() * 256) , (int) (Math.random() * 256));

		g.setColor(c);

		g.fillRect( x , y , w , h);

		

	}

    public void actionPerformed(ActionEvent e)

    {

        String buttonString = e.getActionCommand( );


        if (buttonString.equals("Random Rectangles")){	

        	repaint();

        }

        else if (buttonString.equals("Random Ellipses"))

            repaint();

        else if (buttonString.equals("Random Lines"))

            repaint();

        else

            System.out.println("Unexpected error.");

    }

}

Any one can help? :confused:

#2
docesam

docesam

    Newbie

  • Members
  • PipPip
  • 20 posts
thank you