Lines are angular - java

I want to paint on an image what the user is painting on the screen. For that I created an image array where the images are saved. That all works how it should. But if I draw a circle or some other round shapes it is shown very angular and not nearly round. My code:
private void onPress(java.awt.event.MouseEvent evt) {
images[actualLevel] = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = (Graphics2D) images[actualLevel].getGraphics();
g2d.fillOval(evt.getX() - 200, evt.getY() - 200, 400, 400);
lastX = evt.getX();
lastY = evt.getY();
g2d = (Graphics2D) display.getGraphics();
g2d.drawImage(images[actualLevel], 0, 0, null);
g2d = (Graphics2D) paPaintingArea.getGraphics();
g2d.drawImage(display, 0, 0, paPaintingArea);
System.out.println(actualLevel);
}
private void onDrag(java.awt.event.MouseEvent evt) {
Graphics2D g2d = (Graphics2D) images[actualLevel].getGraphics();
g2d.setStroke(new BasicStroke(20));
g2d.drawLine(lastX, lastY, evt.getX(), evt.getY());
lastX = evt.getX();
lastY = evt.getY();
g2d = (Graphics2D) display.getGraphics();
g2d.drawImage(images[actualLevel], 0, 0, null);
g2d=(Graphics2D)paPaintingArea.getGraphics();
g2d.drawImage(display, 0, 0,paPaintingArea);
}
display is the image that is painted on the panel after saving the image in the array.
Why are my lines so angular and how can I fix it?

Related

Printing in java doesn't complete in windows 7

When i print something by java application in windows 7 the output is blank page and the last graphic only is printed but when i print it in windows 10 it printed successfully .. so i don't no why this happen in windows 7 specially it was printing before on windows 7 and suddenly it happen what i told before (blank page)
this is my code and the output in the picture below
public void printed_bill_printing() {
totals_panelB.setVisible(true);
footer_panelB.setVisible(true);
int count_rows = tableEB.getRowCount();
int count = 0;
int table_height = 0;
while (count <= count_rows - 1) {
int row_height = tableEB.getRowHeight(count);
table_height = row_height + table_height;
count++;
}
float widtha = scrollPaneEB.getWidth();
float heighta = table_height + 30;
scrollPaneEB.setSize(Math.round(widtha), Math.round(heighta));
PrinterJob printJob = PrinterJob.getPrinterJob();
final int finalTable_height = table_height;
printJob.setPrintable(new Printable() {
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D paint_PF = (Graphics2D) graphics;
paint_PF.scale(.68, .6);
B.print(paint_PF);
Graphics2D paint_table = (Graphics2D) graphics;
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
Graphics2D paint_egmaly = (Graphics2D) graphics;
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
Graphics2D paint_tawke3 = (Graphics2D) graphics;
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
return Printable.PAGE_EXISTS;
}
});
PrintRequestAttributeSet printRequestAttrSet = new HashPrintRequestAttributeSet();
printRequestAttrSet.add(new MediaPrintableArea(0, 0, 350, 500, MediaPrintableArea.MM));
printRequestAttrSet.add(Chromaticity.MONOCHROME);
try {
PrintService ps = findPrintService(String.valueOf(printer1PST_combo.getSelectedItem()));
printJob.setPrintService(ps);
printJob.print(printRequestAttrSet);
} catch (PrinterException ex) {
}
adapt_table_size(scrollPaneEB, 440, 351);
}
Casting a Graphics object does not create a new Graphics object. You repeatedly cast the Graphics object and place it in new variables, but it’s still the same Graphics object, so your changes are cumulative.
Stepping through the code:
Graphics2D paint_PF = (Graphics2D) graphics;
This causes paint_PF to contain the same print Graphics object that was passed to the method. It is referring to the graphics object as a Graphics2D, but it’s still the same object.
paint_PF.scale(.68, .6);
B.print(paint_PF);
Now the original print Graphics object is at 0.68 by 0.6 scale.
Graphics2D paint_table = (Graphics2D) graphics;
Again, this is the same Graphics object, which the code just set to have a 0.68 by 0.6 scale.
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
The print Graphics object is still scaled by 0.68 by 0.6; now you are scaling it further by 0.68, resulting in a Graphics object which is actually scaled by 0.04624 by 0.6 (because 0.68 × 0.68 = 0.04624).
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
Graphics2D paint_egmaly = (Graphics2D) graphics;
paint_egmaly still contains the original print Graphics object.
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
Now your print Graphics has been scaled, scaled again, translated, and scaled yet again.
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
Graphics2D paint_tawke3 = (Graphics2D) graphics;
paint_tawke3 contains the original graphics object, with all of the previous scaling and translation.
paint_tawke3.translate(0, 200);
Now the Graphics object has been scaled, scaled, translated, scaled, and translated again.
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
One way to make your changes to the Graphics temporary is by creating a new Graphics object, using Graphics.create, then apply your changes to that. If you do this, you are responsible for deallocating the Graphics object by calling its dispose method.
Graphics2D paint_PF = (Graphics2D) graphics.create();
paint_PF.scale(.68, .6);
B.print(paint_PF);
paint_PF.dispose(); // DO NOT FORGET THIS
Graphics2D paint_table = (Graphics2D) graphics.create();
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
paint_table.dispose(); // DO NOT FORGET THIS
Graphics2D paint_egmaly = (Graphics2D) graphics.create();
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
paint_egmaly.dispose(); // DO NOT FORGET THIS
Graphics2D paint_tawke3 = (Graphics2D) graphics.create();
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
paint_tawke3.dispose(); // DO NOT FORGET THIS
Another option, which uses fewer resources, is to restore the transform of the Graphics object after each change:
AffineTransform originalTransform = ((Graphics2D) graphics).getTransform();
Graphics2D paint_PF = (Graphics2D) graphics;
paint_PF.scale(.68, .6);
B.print(paint_PF);
paint_PF.setTransform(originalTransform);
Graphics2D paint_table = (Graphics2D) graphics;
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
paint_table.setTransform(originalTransform);
Graphics2D paint_egmaly = (Graphics2D) graphics;
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
paint_egmaly.setTransform(originalTransform);
Graphics2D paint_tawke3 = (Graphics2D) graphics;
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
paint_tawke3.setTransform(originalTransform);

java image transparency (image lost)

I think something is wrong with paint method, but I can't figure it out.
public void paint(Graphics g) {
screenImage = createImage(1280, 720);
screenGraphic = screenImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics g) {
g.drawImage(BG, 0, 0, null);
if(isMainScreen) {
g.drawImage(changedImageAlpha(selectedImage, 120), 130, 360, null);
}
paintComponents(g);
this.repaint();
}
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
BG is an Image object, an also is screenImage.
I expected image to be a transparent image, and yes.
I see some transparent image, but nothing in it. It's just a clear transparent image, no color, nothing.
What could be the problem?
As a comment to the answer to your previous question, I mentioned there was an easier than the bit manipulation suggested in the answer.
But I meant instead of not in addition to!
So this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
Should be this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
return img;
}
And as a tip: Try to understand the code others are providing. 'Cut & paste' programming usually ends in failure.

Rotating Rectangle with Color

I am starting to build a simulation in java and using a rectangle as a plane. But as I rotate the rectangle its foreground color won't stick to it. Can someone help me?
Screenshot:Here
Source Code:
private void drawTransform(Graphics g, double modifier) {
Rectangle rect = new Rectangle(130,350, 350, 15);
AffineTransform at = new AffineTransform();
at.rotate(-Math.toRadians(modifier), rect.getX(), rect.getY() + rect.height);
// Transform the shape and draw it to screen
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.fillRect(130, 350, 350, 15);
g2d.draw(at.createTransformedShape(rect));
}
Use fill instead of draw:
g2d.fill(at.createTransformedShape(rect));

How to create image in Java

say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.getGraphics(); <<<----- is this correct?
g.setColor(Color.blue);
for ( ..... ) {
g.fillRect(X , Y, width , height);
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics(); // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.red, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
Try this
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.createGraphics(); // it should be createGraphics
g.setBackground(Color.black);
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
It should be createGraphics.
http://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html
..

Java BasicStroke "Fuzzy"

I'm trying to write a simple paint applet with Java, but I'm having trouble with BasicStroke. Initially, my plan was to try to somehow draw a line with a width, but the API apparently doesn't support that.
I tried using BasicStroke, but the result is just a fuzzy mess. How can I fix this fuzz problem?
private void mousedrag_hook(Point point)
{
if(start == null)
start = point;
end = point;
Graphics2D g2d = (Graphics2D)applInstance.buffer_g;
g2d.setStroke(new BasicStroke(7));
//g2d.fillOval(point.x - 5, point.y - 5, 10, 10);
g2d.drawLine(start.x, start.y, end.x, end.y);
applInstance.repaint();
start = end;
}
Don't forget the RenderingHints:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
...
}

Categories