I need help with java gui

demon42

Member
Jul 19, 2004
160
0
0
As you can quickly guess, im new to java- I need help getting this circle to appear when my circle button is pressed... I need the class headers to remain the same (unless adding extends or implements) because this is actually a piece of an assignment.
I've been trying for hours and hours, but I cant get anything to appear!

drawMain.java
import javax.swing.*;
public class drawMain {
public drawMain() {
myFrame frame = new myFrame();
frame.setVisible(true);
}
static public void main(String[] args) {
new drawMain();
}
}
myFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.lang.Integer;

public class myFrame extends JFrame implements ActionListener {
JButton jButtonCir = new JButton();
JPanel myPanel = new JPanel();
public myFrame() {
jButtonCir.setLocation(160, 320);
jButtonCir.setSize(110, 40);
jButtonCir.setVisible(true);
jButtonCir.setText("Circle");
jButtonCir.addActionListener(this);
myPanel.setBackground(Color.white);
myPanel.setLocation(10, 10);
myPanel.setSize(433,295);
myPanel.setVisible(true);
getContentPane().add(jButtonCir);
getContentPane().add(myPanel);
getContentPane().setLayout(null);
setLocation(0, 0);
setTitle("myFrame");
setSize(460, 480);

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
}
public void actionPerformed( ActionEvent ae) {
if(ae.getSource()==jButtonCir){
myCircle circle=new myCircle(50,50);
circle.setLocation(0,0);
circle.setVisible(true);
getContentPane().add(circle);
System.out.println("button pressed");
}
}
}
myShape.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public abstract class myShape extends JComponent {
public myShape(){
super();
}
}
myCircle.java
import java.awt.*;
import javax.swing.*;
public class myCircle extends myShape{
int shapeWidth;
int shapeHeight;
public myCircle(int w, int h){
super();
shapeWidth=w;
shapeHeight=h;
System.out.println("in myCircle constructor");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println("in the paint component");
g.setColor(Color.red);
g.fillOval(0,0,shapeWidth,shapeHeight);
}
}
 

demon42

Member
Jul 19, 2004
160
0
0
thank you!
that works- it isn't exactly as i need it to be, but i am so relieved to FINALLY see that red ball i dont care anymore. And now i definitely have a direction to try in.

hopefully I will be adding a rectangle and triangle button and also be able to drag the shapes around inside the frame...

It seems like your setup won't quite work for that... or will it? Im not totally sure, but I'm going to try nonetheless.

Once again, thank you for your help!
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: demon42
thank you!
that works- it isn't exactly as i need it to be, but i am so relieved to FINALLY see that red ball i dont care anymore. And now i definitely have a direction to try in.

hopefully I will be adding a rectangle and triangle button and also be able to drag the shapes around inside the frame...

It seems like your setup won't quite work for that... or will it? Im not totally sure, but I'm going to try nonetheless.

Once again, thank you for your help!

It seemed like your JPanel was blocking the object and at first, it wasn't being displayed at all because the drawing method was never called.