0

I am trying to draw some rectangles in java using eclipse and calling the draw() method gives me an error. Is there something I am missing here?

import java.awt.Rectangle;

public class UsingRectangle {
    public static void main(String[] args) {
        Rectangle box1 = new Rectangle(0, 0, 20, 30);

        //error "The method draw() is undefined for the type Rectangle"
        box1.draw();
    }
}

Any help would be more than appreciated. Thank you.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Angelo
  • 1
  • 1
  • 3
  • 1
    @BlueIce It's in the question - "The method draw() is undefined for the type Rectangle" – takendarkk Jul 21 '14 at 03:20
  • Your error is saying that the class Rectangle doesn't have that method. Out of curiosity, where did you think that rectangle was going to appear? You should look at the [Swing Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/) if you want to start doing graphical things. – takendarkk Jul 21 '14 at 03:21
  • What exactly are you trying to draw? Is it a graphic? What made you think that Rectangle has that method? Did you see it somewhere? It's possible it was a custom class. If you want to draw a graphics. See [How to do Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). Also see [Graphics2D](http://docs.oracle.com/javase/tutorial/2d/index.html) which does have a `draw(Shape)` method. Please elaborate on what you are trying to accomplish. Your code example doesn't really say much – Paul Samsotha Jul 21 '14 at 03:22
  • Just as a side note, Eclipse will underline any unknown references and will suggest possible solutions, including any import statements of standard Java library classes that would resolve the error. – Rob Wise Jul 21 '14 at 03:28
  • @peeskillet I am trying to draw a rectangle. Yes, a graphic. I was working using blueJ and the snippet just worked fine. Now that I am using eclipse, most of my code is not working. Thank you for the links you have provided. – Angelo Jul 21 '14 at 03:29
  • @Angelo yep, that's because this wasn't a case of simply not importing the necessary class, so you know it is a deeper problem. See MadProgrammer's answer. – Rob Wise Jul 21 '14 at 03:44

3 Answers3

5

Start by having a read through:

Rectangle is a representation of a shape, in of itself, it has no concept of how to paint, but can be painted through the appropriate APIs

Rectangle

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.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PaintRectangle {

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

    public PaintRectangle() {
        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 {

        private Rectangle box1;

        public TestPane() {
            box1 = new Rectangle(0, 0, 20, 30);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(box1);
            g2d.setColor(Color.BLUE);
            g2d.draw(box1);
            g2d.dispose();
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

This is how you draw rectangles. Google a bit before you post questions :)

// Draw rectangles
import java.awt.*;
import java.applet.*;

public class Rectangles extends Applet {
    public void paint(Graphics g) {
        g.drawRect(10, 10, 60, 50);
        g.fillRect(100, 10, 60, 50);
        g.drawRoundRect(190, 10, 60, 50, 15, 15);
        g.fillRoundRect(70, 90, 140, 100, 30, 40);
    }
}

EDIT: Example taken from Java The complete reference by Herbert Schildt

kushpf
  • 1,078
  • 10
  • 22
  • Not the [dreaded Applet!!!](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/) – takendarkk Jul 21 '14 at 03:24
  • 1
    Consider using Swing over AWT, consider using `Graphics2D` over `Graphics`, consider calling `super.paint` and in fact, one would argue you shouldn't be overriding a `paint` of top level containers. `Applet`s are inheriently difficult to manage and you consider using a `JPanel` (as the primary surface) and a `JFrame` to display it instead. Consider providing a more suitable answer before posting – MadProgrammer Jul 21 '14 at 03:24
  • Yep I know Swing should be used, but as the OP had imported `awt` so I used applets – kushpf Jul 21 '14 at 03:26
  • `Rectangle` is simply a class within the AWT package, doesn't mean that the OP wants to use AWT – MadProgrammer Jul 21 '14 at 03:29
  • @MadProgrammer Thanks for all that information. I haven't ever used Applet myself, so never knew about these things. I just read everywhere that Swing is better, so always ended up using Swing, and never bothered why it is so. Thanks – kushpf Jul 21 '14 at 03:29
  • @kushpf The snippet I pasted above just works fine in blueJ. I was confused about why it was not working in eclipse. I tried to find how to use draw() but I could not find anything. Thank you for the reference you have provided. – Angelo Jul 21 '14 at 03:51
0

You want it to draw where? See your code you are using main which will output in console, and actually main without extending special class like JFrame (this is what you need because this is where you can draw ,this is a window in java)

see below code, for other way drawing your rectangle.

import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

class UsingRectangle extends JComponent {

  public void paint(Graphics g) {
    g.drawRect (10, 10, 200, 200);  
  }
}

public class DrawRect {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new UsingRectangle());
    window.setVisible(true);
  }
}

or using paintComponent

import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class UsingRectangle extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRectangle (10, 10, 200, 200);  
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(new UsingRectangle());
    frame.show();
  }
}

also see this for what the difference of paint and paintComponent for future reference.

Community
  • 1
  • 1
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54