我创建了java.awt.geom.Shape接口的实现。它包含一系列PathIterator路径,用于绘制AND门的形状。 现在我想在移动鼠标指针时移动这个形状。这很好用,但是当我想增加线条的粗细时,这就停止工作了。如果我不改变笔的厚度这适用。 当我将笔的宽度设置为1时,它可以工作。但是当我将它设置为10时,只有一部分门的apears看似从点(0,0)绘制,因此翻译不受影响。 这是门源:
package pashe;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class Gate implements Shape {
GPathIterator pathIterator;
public Gate(){
pathIterator = new GPathIterator();
}
@Override
public boolean contains(Point2D arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Rectangle2D arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(double arg0, double arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(double arg0, double arg1, double arg2, double arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public Rectangle getBounds() {
// TODO Auto-generated method stub
return new Rectangle(0,0,50,30);
}
@Override
public Rectangle2D getBounds2D() {
// TODO Auto-generated method stub
return (new Rectangle(0,0,50,30));
}
@Override
public PathIterator getPathIterator(AffineTransform arg0) {
return pathIterator;
}
@Override
public PathIterator getPathIterator(AffineTransform arg0, double arg1) {
// TODO Auto-generated method stub
return pathIterator;
}
@Override
public boolean intersects(Rectangle2D arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean intersects(double arg0, double arg1, double arg2, double arg3) {
// TODO Auto-generated method stub
return false;
}
}
package pashe;
import java.awt.geom.PathIterator;
public class GPathIterator implements PathIterator {
int current = 0;
@Override
public int currentSegment(float[] points) {
switch (current){
case 0:{
points[0] = 0;
points[1] = 0;
return PathIterator.SEG_MOVETO;
}
case 1:
{
points[0] = 15;
points[1] = 0;
return PathIterator.SEG_LINETO;
}
case 2:
{
points[0] = 40;
points[1] = 0;
points[2] = 40;
points[3] = 15;
return PathIterator.SEG_QUADTO;
}
case 3:
{
points[0] = 40;
points[1] = 30;
points[2] = 15;
points[3] = 30;
return PathIterator.SEG_QUADTO;
}
case 4:
{
points[0] = 0;
points[1] = 30;
return PathIterator.SEG_LINETO;
}
case 5:
{
return PathIterator.SEG_CLOSE;
}
default:
return -1;
}
}
@Override
public int currentSegment(double[] arg0) {
// TODO Auto-generated method stub
return current;
}
@Override
public int getWindingRule() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isDone() {
// TODO Auto-generated method stub
if (current < 6)
return false;
return true;
}
@Override
public void next() {
// TODO Auto-generated method stub
current++;
}
}
package pashe;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
public class Prob extends JFrame {
private JPanel contentPane;
Adap adap;
BasicStroke stroke;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Prob frame = new Prob();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Prob() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
adap.mouseMoved(e);
}
});
//System.out.println(stroke.getLineWidth());
adap = new Adap(this);
contentPane.addMouseListener(adap);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
/*super.paint(g);
if (adap.evt != null){
int x = adap.evt.getX();
int y = adap.evt.getY();
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(5));
GeneralPath gp = new GeneralPath();
gp.moveTo(x, y);
gp.lineTo(x + 15 , y);
gp.quadTo(x + 40, y , x + 40, y + 15);
gp.quadTo(x + 40, y + 30, x + 15, y + 30);
gp.lineTo(x, y+30);
gp.closePath();
g2d.draw(gp);
gp.moveTo(x - 8, y + 5);
gp.lineTo(x, y + 5);
gp.moveTo(x - 8, y + 25);
gp.lineTo(x, y + 25);
g2d.draw(gp);
gp.moveTo(x + 40, y + 15);
gp.lineTo(x + 48, y + 15);
g2d.draw(gp);
}*/
//g2d.setStroke(new BasicStroke(3));
BasicStroke old = (BasicStroke)g2d.getStroke();
stroke = new BasicStroke(10f,old.getEndCap(),old.getLineJoin(),old.getMiterLimit(),old.get DashArray(),old.getDashPhase());
if (adap.evt == null)
{
return;
}
super.paint(g);
int x = adap.evt.getX();
int y = adap.evt.getY();
System.out.println("x=" + x + " y=" + y);
Shape shape = new Gate();
AffineTransform trans = AffineTransform.getTranslateInstance(x,y);
//Shape shape = trans.createTransformedShape(gate);
//shape = stroke.createStrokedShape(shape);
//g2d.fill(shape);
//Shape shape = trans.createTransformedShape(gate);
//g2d.setTransform(trans);
//shape = new Ellipse2D.Double(0,0,18,18);
g2d.setStroke(stroke);
g2d.transform(trans);
//shape = trans.createTransformedShape(shape);
g2d.draw(shape);
g2d.setStroke(old);
}
class Adap extends MouseAdapter {
Prob prob;
MouseEvent evt;
public Adap(Prob p){
prob = p;
}
@Override
public void mouseEntered(MouseEvent ev) {
evt = ev;
prob.repaint();
}
public void mouseMoved(MouseEvent ev) {
evt = ev;
prob.repaint();
}
}
}
答案 0 :(得分:0)
使用以下内容 - 阅读Graphics2D以获取更多信息:
g2d.setStroke(stroke);
g2d.transform(trans);
Shape shape2 = stroke.createStrokedShape(shape);
g2d.draw(shape2);
干得好!