-1

How do I draw the unique rectangle(cube) inside the oval:

https://i.stack.imgur.com/pmHTl.jpg

enter image description here

For me it is tricky to use graphics to draw a rectangle such as the one shown. Any advice on what to do.

Ok. I will try to make myself as clear as I can. What I have so far are the orange ovals and the slim gray oval that is behind it. I only need to create one of these "dots" in a class and i will make many objects of them. The Task I need help in is drawing the "rectangle" shape you see that is in the orange dot using J Component possibly. By request I will add a picture of what I have so far if this edit does not fulfill the need for you to understand my problem.

Thanks

Edit : Here is the Code that I have for creating the ovals if that is of interest to you-

public void paint(Graphics g)  {
    Color c = (Color.orange);
    g.setColor(Color.gray);
    g.fillOval(3,3,60,60);
    g.setColor(c);
   g.fillOval(0,0,60,60);
  } 

Edit: My Attempt at an SSCCE --> NanoBot Class(Where I am Creating the Bot in paint)

/**
 * @author (Omar Ahmed) 
 */
 import javax.swing.*;
 import java.awt.*;
 public class NanoBot extends Image
 {


public NanoBot(int x, int y, int w, int h)
{
    super(x,y,w,h);
}

public void paint(Graphics g)  {
    Color c = (Color.orange);
    g.setColor(Color.gray);
    g.fillOval(3,3,60,60);
    g.setColor(c);
    g.fillOval(0,0,60,60);
    //g.setColor(Color.black);
    //g.fillOval(10,20,10,10);
    //g.fillOval(40,20,10,10);

} 
}

And The Driver:

/** Bot Swarm
 *  Date: May, 2013
 *  Author: Omar Ahmed
 */
import java.awt.*;
import javax.swing.*;
public class Driver  { 
    private JFrame win;
    private NanoBot bot1;
        public Driver()   {
        win = new JFrame(" Swarm ");
        win.setLayout(null);
        win.setVisible(true);
        win.setBounds( 20, 20, 800, 700);
        win.getContentPane().setBackground(Color.white);
        bot1=new NanoBot(50,50,70,70);
        win.add(bot1,0);

}

Hope This Helps
omar ahmed
  • 1
  • 1
  • 4
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) You've described a problem and how you can't do it, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? – Andrew Thompson May 09 '13 at 03:24
  • 2
    You could try using a `AffineTransform` to shear the shape, but for me, personally, I would simply create a custom shape that did it for me...Start by taking a look at [Drawing Arbitrary Shapes](http://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html) and you could even have a look at [this](http://stackoverflow.com/questions/16434273/drawing-sierpinskis-triangle-in-java/16434439#16434439) which uses a couple of `Path2D`s to generate a series Triangles... – MadProgrammer May 09 '13 at 03:25
  • Im so confused. Im simply asking how to draw the image inside the dot. Do i need to zoom in so you know what i am asking? – omar ahmed May 09 '13 at 03:26
  • 1
    *"..Do i need.."* That is the 1st question you have asked. Congrats! Keep it up, [edit](http://stackoverflow.com/posts/16453797/edit) 1 into the question. And in answer to your first question, I need an SSCCE of your best attempt at it. I (and perhaps others) would also be interested to hear what you learned from the examples & docs linked by @MadProgrammer.. Please note that SO is not an help desk. The quality of answers often match the quality of the question. – Andrew Thompson May 09 '13 at 03:36
  • How can I generate an sscce from Blue J? Do you just want the Image class and this class(which inherits from the image class) and the driver? – omar ahmed May 09 '13 at 03:39
  • 1
    An SSCCE is one source file. **Read** the linked document. – Andrew Thompson May 09 '13 at 03:40
  • @Andrew Thompson Did I do it Correct? – omar ahmed May 09 '13 at 03:49
  • No. Do everyone a favor. 1) Go to the SO answer linked by @MadProgrammer 2) Select the code. 3) Copy it. 4) Go to BlueJ 5) ..paste.. or do whatever you do in BlueJ to create a new class. 6) Compile the code. You'll notice it compiles without warning or error. 7) It runs, to show a result.. in your case we are after any result on screen. -- The example posted by @Mad was an SSCCE! Does your current edit satisfy the requirements of a 'single copy/paste, run to see?' Note also that the example creates 2 custom classes within that single class with a `main(String[])` method to run it. – Andrew Thompson May 09 '13 at 03:57

1 Answers1

2

Your first step is to break down your requirements...

enter image description here

You need to draw 3 shapes, front, top, side.

The front's y position is offset by 0.412 of the overall height. It's width is 0.77 of the overall width.

The top's height is 0.412 of the overall height, and it has a horizontal inset of 0.2 of the overall width...

The side's x position is offset by 0.77 of the overall width and has an inset of 0.47 of the over all width.

This is all very important, as you want to ensure that the shapes can resize reasonably well...

Now, you could simply use Graphics#drawLine and Graphics#drawRectangle to build the shape, but that, to me, is a lot of work...

Instead the 2D Graphics is very powerful and contains many wonderful things, the one of interest today is the Shape API, which allows you to define many different shapes, which can be drawn or filled.

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestChip {

    public static void main(String[] args) {
        new TestChip();
    }

    public TestChip() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int width = 44;
            int height = 17;
            Front front = new Front(width, height);
            Top top = new Top(width, height);
            Side side = new Side(width, height);

            draw(g2d, front, Color.BLACK, Color.YELLOW);
            draw(g2d, top, Color.BLACK, Color.GRAY);
            draw(g2d, side, Color.BLACK, Color.DARK_GRAY);

            g2d.dispose();
        }

        protected void draw(Graphics2D g2d, Shape shape, Color foreground, Color background) {
            g2d.setColor(background);
            g2d.fill(shape);
            g2d.setColor(foreground);
            g2d.draw(shape);
        }
    }

    public class Front extends Path2D.Float {

        public Front(float width, float height) {
            float frontWidth = width * 0.77f;
            float yOffset = height * 0.412f;

            moveTo(0, yOffset);
            lineTo(frontWidth, yOffset);
            lineTo(frontWidth, height);
            lineTo(0, height);

            closePath();
        }
    }

    public class Side extends Path2D.Float {

        public Side(float width, float height) {
            float xOffset = width * 0.77f;
            float inset = height * 0.47f;
            moveTo(xOffset, inset);
            lineTo(width, 0);
            lineTo(width, inset);
            lineTo(xOffset, height);
            closePath();
        }
    }

    public class Top extends Path2D.Float {

        public Top(float width, float height) {
            float inset = width * 0.2f;
            float shapeHeight = height * 0.412f;
            moveTo(inset, 0);
            lineTo(width, 0);
            lineTo(width - inset, shapeHeight);
            lineTo(0, shapeHeight);
            closePath();
        }
    }
}

Your job is to now go away, study the example, study the referenced tutorials, study the associated API docs and figure out how to align the above shape within you circle and draw it's legs...

A hint. Make a "Bug" class that knows how to renderer all this and simply translate the position of the Graphics as required...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366