Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

New to Java Need help with Basic Graphic program

Posted on 2006-03-19
7
Medium Priority
?
209 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.
0
Comment
Question by:bigred1022
  • 3
  • 2
  • 2
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16232915
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
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16232941
0
 

Author Comment

by:bigred1022
ID: 16233017
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 16233038
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.
0
 

Author Comment

by:bigred1022
ID: 16233096
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.
0
 
LVL 9

Accepted Solution

by:
Manikandan Thiagarajan earned 1500 total points
ID: 16233190
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
             
    }
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16286864
glad to help you, reason for b grade
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question