我看你回答一个关于java 打印 纸张设置方面的回答! 但是有些我不太明白,可以直接问你吗?
1个回答
展开全部
打印分很多,不知道你是要打印excel还是word还是其他什么的,使用java打印推荐你学习一下ireport。
graphic2d 属于java2d的范畴了。。。给你贴个简单的画图程序
package chapter2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class DrawShapes extends JApplet implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Drawing Geometric Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new DrawShapes();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
JavaDraw2DPanel panel = null;
@Override
public void init() {
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu menu = new JMenu("Objects");
mb.add(menu);
JMenuItem mi = new JMenuItem("Rectangle");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("RoundRectangle");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Ellipse");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Arc");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Line");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("QuadCurve");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("CubicCurve");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Polygon");
mi.addActionListener(this);
menu.add(mi);
panel = new JavaDraw2DPanel();
this.getContentPane().add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("Rectangle".equals(command)) {
panel.shapeType = JavaDraw2DPanel.RECTANGLE;
} else if ("RoundRectangle".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ROUNDDECTANGLE2D;
} else if ("Ellipse".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ELLIPSE2D;
} else if ("Arc".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ARC2D;
} else if ("Line".equals(command)) {
panel.shapeType = JavaDraw2DPanel.LINE2D;
} else if ("QuadCurve".equals(command)) {
panel.shapeType = JavaDraw2DPanel.QUADCURVE2D;
} else if ("CubicCurve".equals(command)) {
panel.shapeType = JavaDraw2DPanel.CUBICCURVE2D;
} else if ("Polygon".equals(command)) {
panel.shapeType = JavaDraw2DPanel.POLYGON;
}
}
}
class JavaDraw2DPanel extends JPanel implements MouseListener,
MouseMotionListener {
private Vector shapes = new Vector();
static final int RECTANGLE = 0;
static final int ROUNDDECTANGLE2D = 1;
static final int ELLIPSE2D = 2;
static final int ARC2D = 3;
static final int LINE2D = 4;
static final int QUADCURVE2D = 5;
static final int CUBICCURVE2D = 6;
static final int POLYGON = 7;
static final int GRNERAL = 8;
static final int AREA = 9;
int shapeType = RECTANGLE;
Vector points = new Vector();
int pointIndex = 0;
Shape partialShape = null;
Point p = null;
public JavaDraw2DPanel() {
super();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(640, 480));
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < shapes.size(); i++) {
Shape s = (Shape) shapes.get(i);
g2.draw(s);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
pointIndex++;
p = null;
}
@Override
public void mouseReleased(MouseEvent e) {
Graphics g = this.getGraphics();
Point p1 = (Point) points.get(pointIndex - 1);
p = e.getPoint();
Shape s = null;
switch (shapeType) {
case RECTANGLE:
s = new Rectangle(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ROUNDDECTANGLE2D:
s = new RoundRectangle2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y,
10, 10);
break;
case ELLIPSE2D:
s = new Ellipse2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ARC2D:
s = new Arc2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120,
Arc2D.OPEN);
break;
case LINE2D:
s = new Line2D.Float(p1.x, p1.y, p.x, p.y);
break;
case QUADCURVE2D:
if (pointIndex > 1) {
Point p2 = (Point) points.get(0);
s = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
}
break;
case CUBICCURVE2D:
if (pointIndex > 2) {
Point p2 = (Point) points.get(pointIndex - 2);
Point p3 = (Point) points.get(pointIndex - 3);
s = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
}
break;
case POLYGON:
if (e.isShiftDown()) {
s = new Polygon();
for (int i = 0; i < pointIndex; i++) {
((Polygon) s).addPoint(((Point) points.get(i)).x,
((Point) points.get(i)).y);
((Polygon) s).addPoint(p.x, p.y);
}
}
break;
}
if (s != null) {
shapes.add(s);
points.clear();
pointIndex = 0;
p = null;
repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
Graphics2D g = (Graphics2D) this.getGraphics();
g.setXORMode(Color.WHITE);
Point p1 = (Point) points.get(pointIndex - 1);
switch (shapeType) {
case RECTANGLE:
if (p != null) {
g.drawRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
}
p = e.getPoint();
g.drawRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ROUNDDECTANGLE2D:
if (p != null) {
g.drawRoundRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 10, 10);
}
p = e.getPoint();
g.drawRoundRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 10, 10);
break;
case ELLIPSE2D:
if (p != null) {
g.drawOval(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
}
p = e.getPoint();
g.drawOval(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ARC2D:
if (p != null) {
g.drawArc(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120);
}
p = e.getPoint();
g.drawArc(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120);
break;
case LINE2D:
case POLYGON:
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
break;
case QUADCURVE2D:
if (pointIndex == 1) {
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
} else {
Point p2 = (Point) points.get(pointIndex - 2);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
g.draw(partialShape);
}
break;
case CUBICCURVE2D:
if (pointIndex == 1) {
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
} else if (pointIndex == 2) {
Point p2 = (Point) points.get(pointIndex - 2);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
g.draw(partialShape);
} else {
Point p2 = (Point) points.get(pointIndex - 2);
Point p3 = (Point) points.get(pointIndex - 3);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y,
p1.x, p1.y, p.x, p.y);
g.draw(partialShape);
}
break;
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
graphic2d 属于java2d的范畴了。。。给你贴个简单的画图程序
package chapter2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class DrawShapes extends JApplet implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Drawing Geometric Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new DrawShapes();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
JavaDraw2DPanel panel = null;
@Override
public void init() {
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu menu = new JMenu("Objects");
mb.add(menu);
JMenuItem mi = new JMenuItem("Rectangle");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("RoundRectangle");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Ellipse");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Arc");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Line");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("QuadCurve");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("CubicCurve");
mi.addActionListener(this);
menu.add(mi);
mi = new JMenuItem("Polygon");
mi.addActionListener(this);
menu.add(mi);
panel = new JavaDraw2DPanel();
this.getContentPane().add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("Rectangle".equals(command)) {
panel.shapeType = JavaDraw2DPanel.RECTANGLE;
} else if ("RoundRectangle".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ROUNDDECTANGLE2D;
} else if ("Ellipse".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ELLIPSE2D;
} else if ("Arc".equals(command)) {
panel.shapeType = JavaDraw2DPanel.ARC2D;
} else if ("Line".equals(command)) {
panel.shapeType = JavaDraw2DPanel.LINE2D;
} else if ("QuadCurve".equals(command)) {
panel.shapeType = JavaDraw2DPanel.QUADCURVE2D;
} else if ("CubicCurve".equals(command)) {
panel.shapeType = JavaDraw2DPanel.CUBICCURVE2D;
} else if ("Polygon".equals(command)) {
panel.shapeType = JavaDraw2DPanel.POLYGON;
}
}
}
class JavaDraw2DPanel extends JPanel implements MouseListener,
MouseMotionListener {
private Vector shapes = new Vector();
static final int RECTANGLE = 0;
static final int ROUNDDECTANGLE2D = 1;
static final int ELLIPSE2D = 2;
static final int ARC2D = 3;
static final int LINE2D = 4;
static final int QUADCURVE2D = 5;
static final int CUBICCURVE2D = 6;
static final int POLYGON = 7;
static final int GRNERAL = 8;
static final int AREA = 9;
int shapeType = RECTANGLE;
Vector points = new Vector();
int pointIndex = 0;
Shape partialShape = null;
Point p = null;
public JavaDraw2DPanel() {
super();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(640, 480));
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < shapes.size(); i++) {
Shape s = (Shape) shapes.get(i);
g2.draw(s);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
pointIndex++;
p = null;
}
@Override
public void mouseReleased(MouseEvent e) {
Graphics g = this.getGraphics();
Point p1 = (Point) points.get(pointIndex - 1);
p = e.getPoint();
Shape s = null;
switch (shapeType) {
case RECTANGLE:
s = new Rectangle(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ROUNDDECTANGLE2D:
s = new RoundRectangle2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y,
10, 10);
break;
case ELLIPSE2D:
s = new Ellipse2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ARC2D:
s = new Arc2D.Float(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120,
Arc2D.OPEN);
break;
case LINE2D:
s = new Line2D.Float(p1.x, p1.y, p.x, p.y);
break;
case QUADCURVE2D:
if (pointIndex > 1) {
Point p2 = (Point) points.get(0);
s = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
}
break;
case CUBICCURVE2D:
if (pointIndex > 2) {
Point p2 = (Point) points.get(pointIndex - 2);
Point p3 = (Point) points.get(pointIndex - 3);
s = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
}
break;
case POLYGON:
if (e.isShiftDown()) {
s = new Polygon();
for (int i = 0; i < pointIndex; i++) {
((Polygon) s).addPoint(((Point) points.get(i)).x,
((Point) points.get(i)).y);
((Polygon) s).addPoint(p.x, p.y);
}
}
break;
}
if (s != null) {
shapes.add(s);
points.clear();
pointIndex = 0;
p = null;
repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
Graphics2D g = (Graphics2D) this.getGraphics();
g.setXORMode(Color.WHITE);
Point p1 = (Point) points.get(pointIndex - 1);
switch (shapeType) {
case RECTANGLE:
if (p != null) {
g.drawRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
}
p = e.getPoint();
g.drawRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ROUNDDECTANGLE2D:
if (p != null) {
g.drawRoundRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 10, 10);
}
p = e.getPoint();
g.drawRoundRect(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 10, 10);
break;
case ELLIPSE2D:
if (p != null) {
g.drawOval(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
}
p = e.getPoint();
g.drawOval(p1.x, p1.y, p.x - p1.x, p.y - p1.y);
break;
case ARC2D:
if (p != null) {
g.drawArc(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120);
}
p = e.getPoint();
g.drawArc(p1.x, p1.y, p.x - p1.x, p.y - p1.y, 30, 120);
break;
case LINE2D:
case POLYGON:
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
break;
case QUADCURVE2D:
if (pointIndex == 1) {
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
} else {
Point p2 = (Point) points.get(pointIndex - 2);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
g.draw(partialShape);
}
break;
case CUBICCURVE2D:
if (pointIndex == 1) {
if (p != null) {
g.drawLine(p1.x, p1.y, p.x, p.y);
}
p = e.getPoint();
g.drawLine(p1.x, p1.y, p.x, p.y);
} else if (pointIndex == 2) {
Point p2 = (Point) points.get(pointIndex - 2);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y,
p.x, p.y);
g.draw(partialShape);
} else {
Point p2 = (Point) points.get(pointIndex - 2);
Point p3 = (Point) points.get(pointIndex - 3);
if (p != null) {
g.draw(partialShape);
}
p = e.getPoint();
partialShape = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y,
p1.x, p1.y, p.x, p.y);
g.draw(partialShape);
}
break;
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询