Question:
java.awt.geom outdated/update?
Sgt Me
2009-09-29 23:14:07 UTC
I am using the sun java tutorials, which make use of the java.awt.geom file. (Im using the NetBeans compiler). When I am typing, it shows up the errors as I go. When I type g2.draw(new Rectangle2D(100, 100, 200, 300))
it says that it cant find it in the java.awt.geom package. It is there as Line2D works, but I think it is outdated.
How do I get the new version of it, and how do I then use it in my program?

PS. Simple answers please. Ich bin ein noob.
Three answers:
deonejuan
2009-09-30 09:39:11 UTC
You have to cast the Graphics g to Graphics2D, then you can use Rectangle2D as a Shape. FYI Rectangle2D has two static constructors. Rectangle2D.Float and Rectangle2D.Double.



Shape is an interface, you would g2.fill( r ) or g2.stroke( r ); after you g2.setPaint( Color.red ); g2.setStroke( 2f ); Graphics2D is a whole new way of making graphics, more than I can explain here. In fact, the Graphics2D is a separate tutorial from the Java tuts.



IIWY, stick with the simple .awt Rectagle( x, y, w, h ); using int and Graphics. Graphics2D is fabulous (Adobe wrote the code), but also Graphics2D is a whole different way of composing graphics at type double precision.



You already have Graphics2D.



The easiest way to see Graphics2D... make a JFrame and add this



import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.Shape;

import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;

import javax.swing.JPanel;

import javax.swing.border.BevelBorder;



public class PrettyPanel

extends JPanel {



public PrettyPanel() {



setBorder( BorderFactory

.createBevelBorder(

BevelBorder.RAISED));

setBackground( Color.magenta );

setOpaque( true );

}

protected void paintComponent( Graphics g ) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

Rectangle r1 = getBounds();

Rectangle2D r2 = r1.getBounds2D();

double xLoc = r2.getWidth() / 2;

double yLoc = r2.getHeight() / 2;

double wide = 166;

double tall = 103;

xLoc -= wide / 2;

yLoc -= tall / 2;

r2.setRect(xLoc,yLoc,wide,tall);



g2.setPaint(Color.red);

g2.fill(r2);

g2.setStroke( new BasicStroke(3f));

g2.setColor( Color.cyan );

g2.draw(r2);

}

}
nogoodaddress
2009-09-30 05:13:25 UTC
From the API:



This is an abstract class that cannot be instantiated directly.



http://java.sun.com/javase/7/docs/api/java/awt/geom/Rectangle2D.html#Rectangle2D()



(In object-oriented programming, abstract classes are "base" classes which provide structure and methods/variables to be inherited and built upon by "subclasses". An example might be a "shape" class - you can't really instantiate a shape, but you can instantiate a subclass of it, perhaps RectangularPrism.).



Instead, use Rectangle:



g2.draw(new Rectangle(100, 100, 200, 300));



http://java.sun.com/javase/7/docs/api/java/awt/Rectangle.html#Rectangle(int,%20int,%20int,%20int)



The API can be very helpful in figuring out your errors:

http://java.sun.com/javase/7/docs/api/
?
2016-12-04 17:54:17 UTC
it ought to be countless issues. a million. verify and notice if the video is deleted by potential of the owner or despite. 2. locate out in case you have the replace for flash participant 3. verify to verify in the experience that your connection is sweet


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...