0

So this was the example given in the notes for my current project. Usually I run the the example code and play around with it to see how everything works. However i'm not sure how to properly call the functions in the example code.

This what was given to me:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class Draw extends JPanel implements ActionListener
{
    JTextField tfInfo;
    JLabel lblColor, lblShapes;
    JCheckBox cbRed, cbBlue;
    ButtonGroup shapes;
    JRadioButton rbCircle, rbSquare;
    JButton btnSubmit; 
    public Draw() 
    {
        setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
        tfInfo = new JTextField("Color & Shapes", 15);
        lblColor = new JLabel("Colors:");
        cbRed = new JCheckBox("Red");
        cbBlue = new JCheckBox("Blue");
        lblShapes = new JLabel("Shapes:");
        shapes = new ButtonGroup();
        rbCircle = new JRadioButton("Circle");
        rbSquare = new JRadioButton("Square");
        btnSubmit = new JButton("Submit"); 
        btnSubmit.addActionListener(this);
        add(tfInfo);
        add(lblColor);
        add(cbRed); 
        add(cbBlue); 
        add(lblShapes);
        add(rbCircle);
        add(rbSquare);
        add(btnSubmit);
        shapes.add(rbCircle);
        shapes.add(rbSquare); 
    }
    public static void main(String [] args)
    {
        Draw n = new Draw();
        n.setVisible(true);
    }
    public void actionPerformed(ActionEvent a)
    {
        if(a.getSource() == btnSubmit)
        { 
            if(cbRed.isSelected()&&cbBlue.isSelected())
            {
                if(rbCircle.isSelected())
                {
                    tfInfo.setText("urple Circle"); 
                }
                else if(rbSquare.isSelected())
                {
                    tfInfo.setText("Purple Square");
                } 
            }
            else if(cbRed.isSelected())
            {   
                if(rbCircle.isSelected())
                { 
                    tfInfo.setText("Red Circle");
                }
                else if(rbSquare.isSelected())
                {
                    tfInfo.setText("Red Square");
                }    
            }
            else if(cbBlue.isSelected())
            {  
                if(rbCircle.isSelected())
                {
                    tfInfo.setText("Blue Circle"); 
                } 
            }
            else if(rbSquare.isSelected())
            {
                tfInfo.setText("Blue Square");
            } 
        }
    }
    public class MApp extends JPanel implements MouseListener 
    {
        private boolean clicked; 
        private Rectangle r; 
        public MApp()
        {
            clicked = false;
            r = new Rectangle(10, 10, 50, 50); 
            addMouseListener(this);
        }
        public void paintComponent(Graphics g)
        {
            if(clicked)
            {
                g.setColor(Color.BLUE);
            }
            else
            {
                g.setColor(Color.RED);
            } 
            g.fillRect((int)r.getX(), (int)r.getY(),
            (int)r.getWidth(), (int)r.getHeight()); 
        }
        public void mouseClicked (MouseEvent e) 
        {
            Point p = new Point(e.getX(),e.getY()); 
            if(r.contains(p))
            {
                clicked = !clicked; 
            }
            repaint(); 
        }
        public void mousePressed (MouseEvent evnt) {}
        public void mouseReleased (MouseEvent evnt) {}
        public void mouseEntered (MouseEvent evnt) {}
        public void mouseExited (MouseEvent evnt) {} 
    }
}
2

1 Answer 1

1

I think I would break it down into these parts:

1) put the MApp into it's own java file. Also, I changed the repaint() in the mouseClicked to: paintComponent(getGraphics()); Your new java file will look like this:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class MApp extends JPanel implements MouseListener 
{
    private boolean clicked; 
    private Rectangle r; 
    public MApp()
    {
        clicked = false;
        r = new Rectangle(10, 10, 50, 50); 
        addMouseListener(this);
    }
    public void paintComponent(Graphics g)
    {
        if(clicked)
        {
            g.setColor(Color.BLUE);
        }
        else
        {
            g.setColor(Color.RED);
        } 
        g.fillRect((int)r.getX(), (int)r.getY(),
        (int)r.getWidth(), (int)r.getHeight()); 
    }
    public void mouseClicked (MouseEvent e) 
    {
        Point p = new Point(e.getX(),e.getY()); 
        if(r.contains(p))
        {
            clicked = !clicked; 
        }
        paintComponent(getGraphics()); 
    }
    public void mousePressed (MouseEvent evnt) {}
    public void mouseReleased (MouseEvent evnt) {}
    public void mouseEntered (MouseEvent evnt) {}
    public void mouseExited (MouseEvent evnt) {} 
}

Next: Create a main file like so:

import javax.swing.JFrame;

public class Tester {

    public static JFrame window = new JFrame("Graphics");

    public static void main(String[] args) {
        window.setBounds(100, 100,800, 800);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(null);

        MApp m = new MApp();
        m.setBounds(100,100,50,50);
        window.add(m);

        Draw d = new Draw();
        d.setBounds(0, 0, window.getWidth(), 80);
        window.add(d);

        window.setVisible(true);
    }

}

(Don't forget to delete the MApp class from your Draw class)

You'll be able to run your code and view what's going on... This is just a quick example with a few shortcuts but you'll get the idea

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.