• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help! I cant get this to paint

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic