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);
Related
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?
I'm trying to make a 2D game, but I can't figure out why my graphics from precedent frames are still there. The effect is when moving a rectangle on x with 1 pixel / frame, I get a long bar drawn on my screen.
In the class body I declared the objects beneath like that:
private int x, y;
private BufferedImage image;
private Graphics2D g;
This is how I'm initializing:
x = 10, y = 25;
image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_BGR);
g = (Graphics2D) image.getGraphics();
r = new Rectangle(x, y, 10, 10);
I have method where I draw graphics:
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.draw(r);
g2.dispose();
I also have a tick method where I move the rectangle:
x++;
The class extends Canvas and implements Runnable for the game loop and I use a JFrame object .
This is the result of 10x10 rectangle movement:
capture
This code below is my paintComponent for painting image objects, but while I want to be able rotate the image with out moving it about the center but also translate it according to a certain value.
trans.translate(xShift, yShift);
this is the line that I tried to use to translate my image, but it cuts off part of the image every time for some reason.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
AffineTransform trans = new AffineTransform();
trans.translate(getWidth() / 2, getHeight() / 2);
trans.rotate(piece.getOrientation() * Math.PI / 2);
trans.translate(-myImage.getWidth() / 2, -myImage.getHeight() / 2);
trans.translate(xShift, yShift);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(myImage, trans, null);
}
}
My sense is that java has a problem with compounding transforms.
Maybe you can find another approach: e.g. transform the image itself, then apply a simple translation:
BufferedImage b22=new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2=(Graphics2D)b22.getGraphics();
AffineTransform trans = new AffineTransform();
trans.translate(getWidth() / 2, getHeight() / 2);
trans.rotate(Math.PI / 2);
trans.translate(-bim.getWidth() / 2, -bim.getHeight() / 2);
g2.drawImage(bim, trans, null);
AffineTransform trans2 = new AffineTransform();
trans2.translate(xoffset, yoffset);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(b22, trans2, null);
// or just g2d.drawImage(b22, xoffset, yoffset, null);
Of course it will cut of if you translate a certain extend.
I am trying to achieve the following
http://www.qksnap.com/i/3hunq/4ld0v/screenshot.png
I am currently able to draw rectangles successfully on a semi-transparent glasspane background using the following code:
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.black); // black background
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
g2.setColor(Color.GREEN.darker());
if (getRect() != null && isDrawing()) {
g2.draw(getRect()); // draw our rectangle (simple Rectangle class)
}
g2.dispose();
}
Which works great, however, I would love to have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.
Any ideas?
..have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.
Create a Rectangle (componentRect) that is the size of the component being painted.
Create an Area (componentArea) of that shape (new Area(componentRect)).
Create an Area (selectionArea) of the selectionRectangle.
Call componentArea.subtract(selectionArea) to remove the selected part.
Call Graphics.setClip(componentArea)
Paint the semi-transparent color.
(Clear the clipping area if more paint operations are required).
As Andrew has suggested (just beat me while I was finishing off my example)
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g.setColor(Color.black); // black background
Area area = new Area();
// This is the area that will filled...
area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight())));
g2.setColor(Color.GREEN.darker());
int width = getWidth() - 1;
int height = getHeight() - 1;
int openWidth = 200;
int openHeight = 200;
int x = (width - openWidth) / 2;
int y = (height - openHeight) / 2;
// This is the area that will be uneffected
area.subtract(new Area(new Rectangle2D.Float(x, y, openWidth, openHeight)));
// Set up a AlphaComposite
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2.fill(area);
g2.dispose();
}
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);
...
}