Solved

What am I missing (circle)?

Posted on 2006-11-16
12
261 Views
Last Modified: 2010-03-31
What am I missing, so my Circle will attach itself to the Pane I created in the sub part of my program? Below is part of the code. Thank you


class Graph extends JFrame {  
public Graph() {  
super("PIE CHART");  
setSize(500,500);  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
setVisible(true);

 class Circle extends JFrame {
public void paint(Graphics g) {

Graphics cir = (Graphics)g;
//JLabel barbar = new JLabel(, JLabel.CENTER);  
FlowLayout bar = new FlowLayout();
int x = 0;
int y = 0;
int width = getSize().width -100;
int height = getSize().height-100;
cir.drawOval(x, y, width, height);
   
Container pane = getContentPane();
pane.setLayout(bar);  
//pane.add(barbar);  
setContentPane(pane);
setVisible(true);
}
}  
}
}
}  
0
Comment
Question by:charliebaby
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17960234
You need to take all component creation out of the paint method and put it in the ctor
0
 
LVL 92

Expert Comment

by:objects
ID: 17960277
try:

class Graph extends JFrame {  
public Graph() {  
super("PIE CHART");  
setSize(500,500);  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
setVisible(true);

Container pane = getContentPane();
pane.setLayout(bar);  
pane.add(new Circle());  
setContentPane(pane);
setVisible(true);
}  
}
 class Circle extends JPanel {
public void paintComponent(Graphics g) {

Graphics cir = (Graphics)g;
//JLabel barbar = new JLabel(, JLabel.CENTER);  
FlowLayout bar = new FlowLayout();
int x = 0;
int y = 0;
int width = getSize().width -100;
int height = getSize().height-100;
cir.drawOval(x, y, width, height);
}
}
}  
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17960294
sorry, that should have been:

public class Graph extends JFrame {  
public Graph() {  
super("PIE CHART");  
setSize(500,500);  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
setVisible(true);

Container pane = getContentPane();
pane.setLayout(new FlowLayout());  
pane.add(new Circle());  
setContentPane(pane);
setVisible(true);
}  
}
 class Circle extends JPanel {
public void paintComponent(Graphics g) {

Graphics cir = (Graphics)g;
int x = 0;
int y = 0;
int width = getSize().width -100;
int height = getSize().height-100;
cir.drawOval(x, y, width, height);
}
}
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 17960554
try


class Graph extends JFrame {
      public Graph() {
            super("PIE CHART");
            setSize(500, 500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            Container pane = getContentPane();
            pane.add(new Circle());
            setContentPane(pane);
            setVisible(true);
      }
}

class Circle extends JPanel {
      public void paintComponent(Graphics g) {
            Graphics cir = (Graphics) g;
            FlowLayout bar = new FlowLayout();
            int x = 0;
            int y = 0;
            int width = getSize().width - 100;
            int height = getSize().height - 100;
            int diameter = Math.min(width, height);
            cir.drawOval(x, y, diameter, diameter);
      }
}
0
 

Author Comment

by:charliebaby
ID: 17960668
I am truly sorry, I made the changes and it did no good, so I am thinking I did something wrong elsewhere, am including the whole  code, can you check it Thank you.

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

public class Hw_prog3a extends JFrame {
Hw_prog3aEvent TRON = new Hw_prog3aEvent(this);

JButton Calculate;
JButton Clear;
JButton Exit;
JButton Pie;

int x;
int y ;
int width ;
int height;


//JTextArea area;
JTextArea area = new JTextArea(5, 15);
JScrollPane scrollPane = new JScrollPane(area);
JMenuBar menuBar;
JMenuItem item;
JToolBar toolBar;

JPanel File1 = new JPanel();
JLabel PrincipalLabel = new JLabel("$Enter Loan Amount:",
JLabel.CENTER); // create
JTextField LonAmt = new JTextField(8);

JPanel File2 = new JPanel();
JLabel RateLabel = new JLabel("0.0Enter Interest Rate:",
JLabel.CENTER); // create
JTextField Rat = new JTextField(8);

JPanel File3 = new JPanel();
JLabel TermLabel = new JLabel("Enter Term-Years:",
JLabel.CENTER); // create
JTextField Trm = new JTextField(8);

JPanel File4 = new JPanel();
JLabel PaymentLabel = new JLabel("Monthly Payments:",
JLabel.CENTER); // create
JTextField MthlyPymnt = new JTextField(8);

JPanel File5 = new JPanel(); // create fifth row (Buttons)

JPanel File6 = new JPanel();

JPanel File7 = new JPanel();

JPanel File8 = new JPanel();


// create borders(Grid layout)
public Hw_prog3a() { // Reference the main method

super("McBride.Financial.MortCal"); setSize(1024, 170);
// set size


// name Buttons
Calculate = new JButton("Calculate");
Clear = new JButton("New Amount");
Exit = new JButton("Exit");
Pie = new JButton("Pie");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // inform Frame to close

GridLayout layout = new GridLayout(1, 1, 1, 1); // calls the

Container pane = getContentPane(); // holds the feilds in place &

pane.setLayout(layout); // checks known parameters

PrincipalLabel.setForeground(Color.blue);
//PrincipalLabel.setBackground(Color.blue);
RateLabel.setForeground(Color.blue);
TermLabel.setForeground(Color.blue);
PaymentLabel.setForeground(Color.blue);

Calculate.setForeground(Color.blue);
//Calculate.setBackground(Color.blue);
Clear.setForeground(Color.blue);
//Clear.setBackground(Color.blue);
Exit.setForeground(Color.blue);
//Exit.setBackground(Color.blue);
Pie.setForeground(Color.blue);


pane.setForeground(Color.blue);

// TRON listens for Button fuctions
Pie.addActionListener(TRON);
Calculate.addActionListener(TRON);
Clear.addActionListener(TRON);
Exit.addActionListener(TRON);

// FlowLayout? components fall into place from left to right
FlowLayout mgr = new FlowLayout(FlowLayout.CENTER, 10, 10); // sets

File1.setLayout(mgr); // Directs the user were to place data
File1.add(PrincipalLabel); // add Component
File1.add(LonAmt); // add Component
pane.add(File1);

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
File2.setLayout(layout2);
File2.add(RateLabel);
File2.add(Rat);
pane.add(File2);

FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
File3.setLayout(layout3);
File3.add(TermLabel);
File3.add(Trm);
pane.add(File3);

FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 10, 10);
MthlyPymnt.setEditable(false);
File4.setLayout(layout4);
File4.add(PaymentLabel);
File4.add(MthlyPymnt);
pane.add(File4);

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 5, 5);
File5.setLayout(layout5);
File5.add(Pie);
File5.add(Calculate);
File5.add(Clear);
File5.add(Exit);

pane.add(File5);

FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);
File6.setLayout(layout6);
              //JTextArea area = new JTextArea(5, 10); // 5 rows and 10 columns
//JScrollPane scrollPane = new JScrollPane(area); // add the text area to
File6.add(scrollPane);
pane.add(File6);
//FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER);
//File7.setLayout(layout7);
JMenuBar bar = new JMenuBar();

File3.add(bar);

JMenu menu = new JMenu("FixedRatesMenu");
JMenuItem item = new JMenuItem("5.35% at 7yrs");
JMenuItem itema = new JMenuItem("5.50% at 15yrs");
JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
bar.add(menu);
menu.add(item);
menu.add(itema);
menu.add(itemb);
pane.add(File7);

item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// this will get called
Rat.setText("5.35");
Trm.setText("7");
TRON.LaunchComputation();
}
});
itema.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// this will get called
Rat.setText("5.5");
Trm.setText("15");
TRON.LaunchComputation();
}
});
itemb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// this will get called
Rat.setText("5.75");
Trm.setText("30");
TRON.LaunchComputation();
}
});
setVisible(true);
}

public static void main(String[] arguments) {
Hw_prog3a frame = new Hw_prog3a();

}

class Hw_prog3aEvent implements ActionListener {
Hw_prog3a virtual;

public Hw_prog3aEvent(Hw_prog3a in) {

virtual = in;

}

public void actionPerformed(ActionEvent event) {

String command = event.getActionCommand();

if(event.getSource() == Calculate)
LaunchComputation();
if(event.getSource() == Clear)
reset();
if(event.getSource() == Exit)
System.exit(0);
if(event.getSource() == Pie)
new Graph();
}
public void LaunchComputation() { // sub-program to do math
try {

double LonAmt =Double.valueOf(virtual.LonAmt.getText()).doubleValue(); // Object


double Rat =Double.valueOf(virtual.Rat.getText()).doubleValue();
int Trm = Integer.parseInt(virtual.Trm.getText());
int totalmonths = (Trm * 12);
double Payment = 0;
double InterestPaid;
String monthlyPayment = new String();
NumberFormat currency = NumberFormat.getCurrencyInstance();
double I = (Rat / 100.0 / 12.0);

Payment = (LonAmt * I) / (1 - Math.pow(1 / (1 + I), totalmonths));
virtual.MthlyPymnt.setText(currency.format(Payment));

//Payment Amoritization Table
int LoanPaymentNumber;
for(LoanPaymentNumber = 0; LoanPaymentNumber < (totalmonths); LoanPaymentNumber++){
InterestPaid = (LonAmt * I);
LonAmt = LonAmt - (Payment - InterestPaid);
virtual.area.append((LoanPaymentNumber+1) + "   " + currency.format(InterestPaid) + "   " + currency.format(LonAmt) + "\n");
}
}
catch (NumberFormatException ex) {
}
}
protected void reset() {
virtual.LonAmt.setText(null);
virtual.Rat.setText(null);
virtual.Trm.setText(null);
virtual.MthlyPymnt.setText(null);
virtual.area.setText(null);
}
}
class Graph extends JFrame {  
public Graph() {  
super("PIE CHART");  
setSize(500,500);  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
setVisible(true);

Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(new Circle());
setContentPane(pane);
setVisible(true);
}
}
 class Circle extends JPanel {
public void paintComponent(Graphics g) {

Graphics cir = (Graphics)g;
//JLabel barbar = new JLabel(, JLabel.CENTER);  
//FlowLayout bar = new FlowLayout();
int x = 0;
int y = 0;
int width = getSize().width -100;
int height = getSize().height-100;
cir.drawOval(x, y, width, height);
   
//Container pane = getContentPane();
//pane.setLayout(bar);  
//pane.add(barbar);  
//setContentPane(pane);
//setVisible(true);

}
}  
}
//}
//}  
0
 

Author Comment

by:charliebaby
ID: 17960698
well that did the trick thank you both...
0
 

Author Comment

by:charliebaby
ID: 17960731
I hope that was fair, I try my best to be.
0
 
LVL 92

Expert Comment

by:objects
ID: 17960775
how is that fair??  I fixed the problems with your code, all CEHJ did was copy my suggestions and change the diameter of the circle that was being displayed.
Point split should have been the other way around.

0
 

Author Comment

by:charliebaby
ID: 17960803
I am so sorry I am new at this please open the question again and I will make it right...
0
 

Author Comment

by:charliebaby
ID: 17960934
Objects, When I used your suggestions I typed them in and no go and then I pasted them in just to make sure I typed right , I had just noticed CEHJ code while I was working on yours and when I pasted them in, it worked, being new to code I didn't realize that they where your suggestions, it all starts looking alike after 5-6 hours of stareing at the screen, I am sorry I was unfair to you  please accept my appologies...
0
 
LVL 92

Expert Comment

by:objects
ID: 17960953
accepted :)
0
 

Author Comment

by:charliebaby
ID: 17960992
Objects, what would i do with out you? I would suffer a lot more through all this without you, thats what, Thank you for accepteing my appologies:)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 162
nextBoolean(double p) for Random class 3 40
table example 4 29
spring jars download 1 27
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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