| Author |
help! I cant get this to paint
|
Dave N.
Greenhorn
Joined: Feb 12, 2005
Posts: 1
|
|
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 myFrame.java myShape.java myCircle.java
|
 |
Rawat (Zeon)
Greenhorn
Joined: Feb 12, 2005
Posts: 5
|
|
Hi Dave, To reslove ths issue I had to add few lines of code to the following two files. The class headers are same. 1. myCircle.java 2. myFrame.java Code for myCircle.java // ----------------- Code for myCircle.java -------------------------------- import java.awt.*; import javax.swing.*; public class myCircle extends myShape { int shapeWidth; int shapeHeight; int pad =5; public myCircle(int w, int h) { super(); shapeWidth=w; shapeHeight=h; setSize(w,h); setBorder(BorderFactory.createEmptyBorder(this.pad, this.pad, this.pad, this.pad)); System.out.println("in myCircle constructor"); } protected void paintComponent(Graphics g) { if (isOpaque()) { //paint background g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); } g.setColor(Color.red); g.fillOval(0,0,shapeWidth,shapeHeight); } public Dimension getPreferredSize() { return new Dimension(shapeWidth, shapeHeight); } public Dimension getMinimumSize() { return new Dimension(shapeWidth, shapeHeight); } } // --------------------- End of myCircle.java ------------------ Code for myFrame.java // ----------------------- Code for 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(160,350); circle.setVisible(true); getContentPane().add(circle); repaint(); System.out.println("button pressed"); } } } // ------------------- end of myFrame.java ---------------------- Do go to the following link to know more about Custom JComponents http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html -Zeon
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: help! I cant get this to paint
|
|
|