I'm drawing small circles with radial gradient. Example result (r = 10 px):
But when I decrease the radius, strange things begin to happen. If r = 5px:
Scaled to match 10px image:
And it gets even worse if r = 2px (again, scaled):
As you see, the center of the gradient is consistently off (down and to the right) from center of circle.
The code I used to generate the above images (SSCCE, compilable&runnable):
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.*;
import java.awt.geom.*;
public class Test {
public static void main(String[] args) throws Exception {
float r = 2;
BufferedImage img = new BufferedImage((int) r*4, (int) r*4, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
RenderingHints rh = new RenderingHints(null);
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.addRenderingHints(rh);
g.setBackground(Color.WHITE);
g.clearRect(0, 0, (int) r*4, (int) r*4);
g.setPaint(new RadialGradientPaint(
r*2,r*2,r*2,
new float[] {0.1f,0.35f},
new Color[] {Color.RED, Color.GREEN}
));
g.fill(new Ellipse2D.Float(r,r,r*2,r*2));
g.dispose();
ImageIO.write(img, "png", new File("out.png"));
}
}
I tried switching rendering hints, but the result doesn't change. What could be causing this problem?
EDIT:
Offsetting RadialGradientPaint by 0.5f top-left results in correct image (r=2):
"Fixed" code:
g.setPaint(new RadialGradientPaint(
r*2-0.5f, r*2-0.5f, r*2,
new float[] {0.1f,0.35f},
new Color[] {Color.RED, Color.GREEN}
));
The problem has to do with the fact that the height and width of your circle is an even number. You used to have g.fill(new Ellipse2D.Float(r,r,r*2,r*2));
as part of your code. This made it so that the width and height of your circle was always even. This causes problems because computers are not perfect and its not possible to draw a pixel exactly in the center of a circle with even dimensions. To fix this, I added one to where you drew your circle. The working code follows:
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.*;
import java.awt.geom.*;
public class Test {
public static void main(String[] args) throws Exception {
float r = 2;
BufferedImage img = new BufferedImage((int) r*4 + 1, (int) r*4 + 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
RenderingHints rh = new RenderingHints(null);
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.addRenderingHints(rh);
g.setBackground(Color.WHITE);
g.clearRect(0, 0, (int) r*4 + 1, (int) r*4 + 1);
g.setPaint(new RadialGradientPaint(
r*2,r*2,r*2,
new float[] {0.1f,0.35f},
new Color[] {Color.RED, Color.GREEN}
));
g.fill(new Ellipse2D.Float(r,r,r*2 + 1,r*2 + 1));
g.dispose();
ImageIO.write(img, "png", new File("out.png"));
}
}
Related
I have converted the same pptx file through some online tools and the generated images for the slides are much lesser than the one generated through POI.
try {
//creating an empty presentation
//File file = new File("/Users/dk/Documents/Jitendra/work/Rough/download_images/pptx1.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("lighthouse_tobeused.pptx"));
//getting the dimensions and size of the slide
Dimension pgsize = ppt.getPageSize();
List<XSLFSlide> slide = ppt.getSlides();
BufferedImage img = null;
int imgWidth = pgsize.width;
int imgHeight = pgsize.height;
for (int i = 0; i < slide.size(); i++) {
LOG.info("imgWidth => "+imgWidth+" ,imgHeight => "+imgHeight);
img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST, 150);
graphics.setColor(Color.white);
graphics.clearRect(0, 0, imgWidth, imgHeight);
graphics.fill(new Rectangle2D.Float(0, 0, imgWidth, imgHeight));
//render
slide.get(i).draw(graphics);
FileOutputStream out = new FileOutputStream("ppt_image_"+i+".jpg");
javax.imageio.ImageIO.write(img, "jpg", out);
ppt.write(out);
//LOG.info("Image successfully created");
out.close();
At first do removing the ppt.write(out); from your code. That writes the whole XMLSlideShow ppt into the FileOutputStream out for each the single ppt_image_*.jpg. That is useless and only adds unnecessary bytes to each of the ppt_image_*.jpg files.
If then the size is not small enough, you could use java.awt.geom.AffineTransform to set a zoom scaling factor and then set that transform to the used Graphics2D.
...
Double zoom = .5d;
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
...
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
...
A zoom factor lower than 1d will lead to smaller image sizes and so smaller file sizes too.
Complete example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.RenderingHints;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class PptToImage {
public static void main(String args[]) throws Exception {
FileInputStream in =new FileInputStream("PPTX.pptx");
XMLSlideShow ppt = new XMLSlideShow(in);
//set zoom factor
Double zoom = .5d;
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
//get the dimension of size of the slide
Dimension pgsize = ppt.getPageSize();
//get slides
java.util.List<XSLFSlide> slides = ppt.getSlides();
BufferedImage img = null;
FileOutputStream out = null;
for (int i = 0; i < slides.size(); i++) {
img = new BufferedImage((int)Math.ceil(pgsize.width * zoom), (int)Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//set scaling transform
graphics.setTransform(at);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST, 150);
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slides.get(i).draw(graphics);
//creating an image file as output
out = new FileOutputStream("ppt_image_" + i + ".png");
ImageIO.write(img, "png", out);
out.close();
}
}
}
Do playing with different Double zoom = .5d;
I have an image with transparent background. I'd like to rotate this image to a specific angle and keep the transparent background for the resulting image. For this purpose I use the following method:
public static BufferedImage rotateImage(BufferedImage image, double angle, Color backgroundColor) {
System.out.println(image.getType());
double theta = Math.toRadians(angle);
double sin = Math.abs(Math.sin(theta));
double cos = Math.abs(Math.cos(theta));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
Graphics2D g2d = tmp.createGraphics();
if (backgroundColor != null) {
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, newW, newH);
}
g2d.fillRect(0, 0, newW, newH);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.translate((newW - w) / 2, (newH - h) / 2);
g2d.rotate(theta, w / 2, h / 2);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return tmp;
}
I invoke it with background=null:
BufferedImage image = ImageIO.read(file);
rotateImage(image, 4, null);
ImageIO.write(bi, "PNG", new File("image.png"));
but the background of the resulting image.png is WHITE. What am I doing wrong and how to properly keep the transparent background for image.png?
I'm a bit puzzled about the behavior of Graphics.drawImage(). Maybe somebody else can comment about it.
However, Graphics2D.drawRenderedImage() works a treat. It takes an AffineTransform to control the rotation. The below example nicely works. You probably have additional requirement about the final image size and the location of the rotated image.
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageRotation {
public static void main(String[] args) {
ImageRotation rotation = new ImageRotation();
rotation.rotate("input.png", 45, "output.png");
}
public void rotate(String inputImageFilename, double angle, String outputImageFilename) {
try {
BufferedImage inputImage = ImageIO.read(new File(inputImageFilename));
BufferedImage outputImage = rotateImage(inputImage, angle);
ImageIO.write(outputImage, "PNG", new File(outputImageFilename));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = destImage.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(angle / 180 * Math.PI, width / 2 , height / 2);
g2d.drawRenderedImage(sourceImage, transform);
g2d.dispose();
return destImage;
}
}
Update
While the above code works for most PNGs, it does not work for the image that alexanoid is using. I've analyzed the image:
It's a grayscale image without a color palette (PNG color type 0) .
It uses simple transparency with a 2 byte long tRNS chunk.
As far as I can tell that's perfectly legal. However, ImageIO does not implement this combination. If the image has no palette, it simply ignores the tRNS chunk and therefore ignores the transparency information. That's most likely a bug.
You basically have two options now:
Look for an alternative library to read PNG files.
Fix the transparency after you have read the PNG file. This only works if know that the image used the particular problematic format.
Input and output for working PNG files
Input image:
Ouptput Image:
The following code is the code that I am using to rotate two rectangles is below
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);
//r1
Rectangle2D r1 = new Rectangle2D.Double(0, 0, 50, 4);
g2d.rotate(Math.toRadians(45));
g2d.fill(r1);
//r3
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90));
g2d.fill(r3);
This create something which looks like this
Whereas I am trying to create something which looks like this
This occurs since when the rectangles are rotated, they are both rotated around the point 0,0. To fix that I tried using rotate(double theta, double x, double y). However I am having trouble using that correctly. For example when I have tried
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 25, 25);
or
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 0, 25);
I get similar undesired results when both the rectangles were being rotated around the point 0,0. I would appreciate any help if fixing my problem.
If you are wondering why I have done it like this, it is because I am hoping to make a effect similar to when you click on the 3 parallel lines seen here by the time I finish coding the graphic
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class Cross extends JPanel {
private Rectangle2D rectangle;
Cross() {
rectangle = new Rectangle2D.Double(0, 0, 50, 4);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.white);
AffineTransform at = g2.getTransform();
g2.translate(5, 5);
g2.rotate(Math.toRadians(45));
g2.fill(rectangle);
g2.setTransform(at);
g2.translate(5, 5 + Math.sqrt(2) * 25);
g2.rotate(Math.toRadians(-45));
g2.fill(rectangle);
g2.setTransform(at);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Cross");
frame.add(new Cross());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(128, 128);
frame.setVisible(true);
}
}
Although I think I might have done mistake somewhere with maths (it looks somewhat odd), this should give you an idea.
So it turns out that this can be done with some relatively simple maths. As the shape I am trying to make is a perfect X.
To work out the position for the rectangle we can use Pythagorean theorem.
The image above shows two steps.
Translation [2√2, 0] from the point [0, 0]
Rotate 45deg from the point [2√2, 0]
Next we need to work out the minimum point of this rectangle. Again we can use Pythagorean theorem.
This tells us where the top point of the second rectangle will be
Difference in height: 4 - 2√2
Bottom of line when straight: [0, 27√2 + (4 - 2√2)] = [0, 4 + 25√2]
Top of line when straight: [0, 25√2]
Finally we can put in the last rectangle starting at [0, 0]
Translation [0, 25√2] from the point [0, 0]
Rotate -45deg from the point [0, 25√2]
Now that the theory is out of the way, what does this look like in code? It looks similar to the code below
//Values
final static double[] r1Points = {2.828427125, 0}; //Equivilant 2√2
final static double[] r3Points = {0, 35.35533906}; //Equivilant 25√2
final static int[] widthNHeight = {50, 4}; //Width then height
final static double angle = 45.0; //Angle to rotate lines
//Declaring the rectangles
Rectangle2D r1 = new Rectangle2D.Double(r1Points[0], r1Points[1], widthNHeight[0], widthNHeight[1]);
Rectangle2D r3 = new Rectangle2D.Double(r3Points[0], r3Points[1], widthNHeight[0], widthNHeight[1]);
//r1
g2d.rotate(Math.toRadians(angle), r1Points[0], r1Points[1]); //Rotates graphic for first rectangle
g2d.fill(r1);
//r3
g2d.rotate(Math.toRadians(-angle), r1Points[0], r1Points[1]); //Rotates the graphic back to straight
g2d.rotate(Math.toRadians(-angle), r3Points[0], r3Points[1]); //Rotates graphic for second rectangle
g2d.fill(r3);
I have a (small) BufferedImage wich needs to be enlarged using nearest neighbor interpolation and then drawn to a Graphics2D. The Image has 1-bit alpha information and rendering it with antialiasing on and this code
AffineTransform oldT = g.getTransform();
Paint oldP = g.getPaint();
int w = img.getWidth(), h = img.getHeight();
g.transform(at);
g.setPaint(new TexturePaint(img, new Rectangle2D.Double(0, 0, w, h)));
g.fillRect(0, 0, w, h);
g.setPaint(oldP);
g.setTransform(oldT);
Where img is theBufferedImage to be rendered using at as an AffineTransform. The antialiasing activated by
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
produces good results, but only for the border of the entire image; looking very strange:
Notice the top border is actually the image border while the right is a transition from opaque to transparent pixel.
Is there a best practice to achieve the same antialiasing within the image as is applied to the border? Note that other interpolation methods than NearestNeighbor are unacceptable.
Thanks in advance for any hints.
The rendering shouldn't take too long since it is part of a paintComponent method, but preprocessing the BufferedImage is possible. The AffineTransform is (very) variable, though.
EDIT
I have achieved a small improvement by using a two-step method:
AffineTransform oldT = g.getTransform();
Paint oldP = g.getPaint();
int w = img.getWidth(), h = img.getHeight(), texw = (int) (w*oldT.getScaleX()), texh = (int) (h * oldT.getScaleY());
BufferedImage tex = new BufferedImage(texw, texh, BufferedImage.TYPE_INT_ARGB);
Graphics2D tg = tex.createGraphics();
tg.scale(oldT.getScaleX(), oldT.getScaleY());
tg.drawImage(img, 0, 0, this);
g.transform(at);
g.scale(1/oldT.getScaleX(), 1/oldT.getScaleY());
g.setPaint(new TexturePaint(tex, new Rectangle2D.Double(0, 0, texw, texh)));
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.fillRect(0, 0, texw, texh);
g.setPaint(oldP);
g.setTransform(oldT);
Resulting in this image:
This is still not perfect, though. I tried VALUE_INTERPOLATION_BICUBIC, but this is just incredibly slow and essentially produces the same result. I hope there is a way the get the exact same antialiasing effect, since it is still irritating.
A fairly complicated solution with a lot of tweaking and testing put into provides a very good quality by rendering each pixel as a shape reading its color and transparency from the BufferedImage:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
public class VectorizedImage {
private final BufferedImage img;
public VectorizedImage(BufferedImage img) {
this.img = img;
}
public void draw(Graphics2D g) {
Color oldC = g.getColor();
Composite oldCo = g.getComposite();
AlphaComposite comp = AlphaComposite.SrcOver;
double lap = 1/Math.sqrt(g.getTransform().getDeterminant()); // This deals with inter-pixel borders letting the background shine through
Path2D pixel = new Path2D.Double();
pixel.moveTo(0, 0);
pixel.lineTo(0, 1);
pixel.lineTo(1, 1);
pixel.lineTo(1, 0);
pixel.lineTo(0, 0);
pixel.transform(AffineTransform.getScaleInstance(1+lap, 1+lap));
for (int i = 0; i < img.getWidth(); i++)
for (int j = 0; j < img.getHeight(); j++) {
g.setColor(new Color(img.getRGB(i, j)));
g.setComposite(comp.derive(img.getAlphaRaster().getSampleFloat(i, j, 0) / 255));
g.fill(pixel.createTransformedShape(AffineTransform.getTranslateInstance(i, j)));
}
g.setColor(oldC);
g.setComposite(oldCo);
}
}
The result is this nicely rendered edge:
What I'm basically trying to do is rotate a BufferedImage and then draw it based on it's new centre point.
So, originally I would've just done;
graphics.drawImage(image, x - (image.getWidth() / 2), y - (image.getHeight() / 2), this);
However, I'm unsure how to do it when I rotate the image using AffineTransform.
If I remember from basic Maths at school I'll need to use Cos for the X and Sin for the Y, however I've been googling for an algorithm but can't seem to come across anything.
I found out how to get the coordinates of the rotated point. If x1,y1 are the original coordinates and at the AffineTransform then you can get the coordinates of the rotated point in the original coordinate system with
Point2D point1origCoords = at.transform(new Point2D.Double(x1,y1),null);
The coordinates of the point in the new coordinate system after the affine transformation would be the same as the original coordinates. They stay the same because it's the coordinate system that gets transformed instead, so you need to apply the affine transformation to them.
Here is a demo of a rotation about the origin:
And the code used to generate it:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Triangle extends JPanel {
public void drawTriangle(Graphics2D g,
int x1, int y1, int x2, int y2, int x3, int y3,
int s, Color c, AffineTransform at) throws NoninvertibleTransformException{
g.setStroke(new BasicStroke(3.0f));
g.setPaint(c);
int d = 10;
g.drawLine(x1*s, y1*s, x2*s, y2*s);
g.drawLine(x2*s, y2*s, x3*s, y3*s);
g.drawLine(x3*s, y3*s, x1*s, y1*s);
Ellipse2D center_of_triangle = new Ellipse2D.Double(.33*(x1+x2+x3)*s, .33*(y1+y2+y3)*s,4,4);
g.draw(center_of_triangle);
float dash1[] = {2.0f};
BasicStroke dashed =
new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
g.setStroke(dashed);
g.drawLine(0,0,x1*s,y1*s);
g.drawLine(0,0,x2*s,y2*s);
g.drawLine(0,0,x3*s,y3*s);
g.setColor(Color.black);
Point2D point1origCoords = at.transform(new Point2D.Double(x1,y1),null);
Point2D point2origCoords = at.transform(new Point2D.Double(x2,y2),null);
Point2D point3origCoords = at.transform(new Point2D.Double(x3,y3),null);
g.drawString("("+String.format("%.2f",point1origCoords.getX())+", "
+String.format("%.2f",point1origCoords.getY())+")",
x1*s,y1*s+2*d);
g.drawString("("+String.format("%.2f",point2origCoords.getX())+", "
+String.format("%.2f",point2origCoords.getY())+")", x2*s-4*d,y2*s-d);
g.drawString("("+String.format("%.2f",point3origCoords.getX())+", "
+String.format("%.2f",point3origCoords.getY())+")", x3*s+d,y3*s);
// center of the triangle
Point2D.Double center = new Point2D.Double(.33*(x1+x2+x3), .33*(y1+y2+y3));
Point2D centerorigCoords = at.transform(center,null);
g.drawString("("+String.format("%.2f",centerorigCoords.getX())+", "
+String.format("%.2f",centerorigCoords.getY())+")",
(int)Math.round(center.x*s),(int)Math.round(center.y*s)-d);
}
public void drawGrid(Graphics2D g, int s) {
int w = getWidth();
int h = getHeight();
float dash1[] = {2.0f};
BasicStroke dashed =
new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
g.setStroke(dashed);
g.setColor(Color.gray);
int j = 0;
while( j <= h ) {
g.drawLine(0,h-j,w,h-j);
j+=s;
}
j = 0;
while( j <= w ) {
g.drawLine(j,0,j,h);
j+=s;
}
}
public void paintComponent(Graphics g){
final int s=20;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
Font big = new Font("Times New Roman", Font.BOLD,20);
g2.setFont(big);
drawGrid(g2,s);
AffineTransform at = new AffineTransform();
// no rotation
at.rotate(Math.toRadians(0));
g2.transform(at);
// rotated triangle
try {
drawTriangle(g2,2,26,3,12,8,21,s,Color.blue,at);
} catch (NoninvertibleTransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
at.rotate(Math.toRadians(-45));
g2.transform(at);
try {
drawTriangle(g2,2,26,3,12,8,21,s,Color.magenta,at);
} catch (NoninvertibleTransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Window"); //frame is the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Triangle panel = new Triangle(); //panel is the graphics area where we can draw
frame.add(panel); //put the panel inside the window
frame.setSize(600,600); //set the window size to 600x600 pixels
frame.setVisible(true);
}
}