Hello,
I was hoping someone can help me, im looking for a way to extend a basic shape class in particular the roundedrectangle2d class and be able to draw it in the same way we could handle an object with a simple call to g.draw(object instance);

However im having problems with it not being drawn to the canvas, i only see a gray rectangle for the code below :

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

public class objectView extends JComponent {

    private static final long serialVersionUID = 1L;

    public objectView()
    {

    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }
    public Dimension setPreferredSize(int width, int height) {
        return new Dimension(width, height);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
         Graphics2D graphics2 = (Graphics2D) g;

        int margin = 0;
        Dimension dim = getSize();

        graphics2.setColor(Color.GRAY);
        graphics2.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);

        BasicStroke fineStroke=new BasicStroke(1);
        graphics2.setStroke(fineStroke);
        graphics2.setColor(Color.red);
        drawObject fo = new FileObject();
        fo.setRoundRect(100, 100, 240, 160, 10, 10); //tried just incase its size was 0
        graphics2.fill(fo);
        graphics2.draw(fo);


    }
}


import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;


public class drawObject extends RoundRectangle2D{

    public drawObject() {
        new RoundRectangle2D.Float(100, 100, 240, 160, 10, 10);
    setFrame(new Rectangle(100,100,100,100));


        // TODO Auto-generated constructor stub
    }

    @Override
    public Rectangle2D getBounds2D() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public double getArcHeight() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double getArcWidth() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void setRoundRect(double arg0, double arg1, double arg2,
            double arg3, double arg4, double arg5) {
        // TODO Auto-generated method stub

    }

    @Override
    public double getHeight() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double getWidth() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double getX() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double getY() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }


}

Can anyone help me fix this so i can have a custom class for a shape that i can extend by drawing on top of, say i want to add text to a rounded rectangle within the drawObjwect class, how could i go about that, tutorials online only seem to focus on making a simple extension of classes like circle without drawing ontop of the existing shape etc, any help would be great :)

in java , if you dont provide a constructor , java provides one for you , but if you do give one , then java will follow that constructor only. you have a constructor for objectView , but you dont put anything inside it. plus , i dont think i saw any main method in your code, that would have helped in understanding your problem. also , line 45 , whats with the new FileObject ?

It may be a lot easier to extend something that has a paintComponent method, and include the rounded rectangle by composition rather than subclassing...

class RRectText extends JComponent {
    RoundRectangle2D rect;
    String text;
    public RRectText(float x, y, w, h, arcw, arch, String text) {
       rect = new RoundRectangle2DFloat(z,y, ...
       this.text = text;
    }
    ...
    public void paintComponent(Graphics g) {
        g.drawShape(rect)
        g.drawString(text, ...
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.