We help IT Professionals succeed at work.

New to Java Need help with Basic Graphic program

bigred1022
bigred1022 asked
on
Medium Priority
231 Views
Last Modified: 2010-03-31
Hello all,

I am starting to write a Basic Graphic program for a College JAVA course that will draw simple shapes (Oval and Rectangle) when the respective button is pushed.  Now I already have the design set up with the buttons I need.  These include the two mentioned earlier and a "Center" button as well as a "Corner" button.  These will either draw the shape (which ever is selected) in the Center or all 4 corners of the Display Panel.  I also already have three classes which are: DisplayPanel, ButtonPanel, and ButtonInterfaceApplication.

My question is this:  How do I get DisplayPanel class to correctly display the shapes and layout?  Meaning I click the button which registers in the ButtonPanel class and the DisplayPanel class will read the information from that class and do what is necessary.

I know that I should use Boolean expressions for the 4 buttons.  But I am confused on tieing it all together.  Mainly method calling and the interface aspect.  I already know how to draw the shapes with the Graphics g thing.

Thanks for any help at all.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
have a read of this to get you started:

http://www.ictp.trieste.it/~manuals/programming/Java/tutorial/uiswing/painting/drawingShapes.html

give me a yell when u get stuck
Manikandan ThiagarajanSenior consultant

Commented:

Author

Commented:
Both suggestions are greatly appreciated but not what I am looking for exactly.  here is some of my code that I already have:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class ButtonPanel extends JPanel implements ActionListener{
   private  JButton jbCircle;
   private  JButton jbSquare;
   
    /** Creates a new instance of ButtonPanel */
    public ButtonPanel() {
        jbCircle = new JButton("Circle");
        jbSquare = new JButton("Square");
        jbCenter = new JButton("Center");
        jbCorner = new JButton("Corner");
        jbCircle.addActionListener(this);
        jbSquare.addActionListener(this);
        jbCenter.addActionListener(this);
        jbCorner.addActionListener(this);
        add(jbCircle);
        add(jbSquare);
        add(jbCenter);
        add(jbCorner);
       
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("A button was Clicked....");
    }
   
Now Instead of the Println command I need the program to do what the button says.  Should I be placing IF statements in the actionPerformed? (I.E. IF jbCircle(true) then draw).  I am unsure of what to write in order for the DisplayPanel to do the requested operation.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you need to add a paintComponent() method to your panel (or the panel you want to paint your shapes on).
Also member vars to store the details of the actual shapes to paint.

Author

Commented:
This is from my DisplayPanel class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DisplayPanel extends JPanel {
   
    /** Creates a new instance of DisplayPanel */
    public DisplayPanel() {
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
             
    }

Now I know that you would write something like g.drawOval(2, 3, 45, 50) and g.drawRect(2, 2, 2, 2)
But I don't know How to code the classes in order for them to basically "see" each other.  I click a button and the DisplayPanel draws.  But how?  I know I am making this sound tougher than it is.
Senior consultant
Commented:
public void actionPerformed(ActionEvent e) {
       if(e.getSource() == jbCircle )
{

       repaint();// this repaint would call the public void paintComponent(Graphics g)



    }


}

  public void paintComponent(Graphics g) {

        super.paintComponent(g);

// do some painting here
             
    }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Manikandan ThiagarajanSenior consultant

Commented:
glad to help you, reason for b grade
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.