1
我創建了接口java.awt.geom.Shape的實現。這包含一系列路徑,以PathIterator的形式繪製與門的形狀。 現在我想移動這個形狀,當我移動鼠標指針。這工作很好,但是當我想增加線條的粗細時,這會停止工作。如果我不改變筆的厚度這效果很好。 當我將筆的寬度設置爲1時,它可以工作。但是當我將它設置爲10時,只是看起來從點(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();
}
}
}
爲了更快得到更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。在將類組合到一個源代碼之前,您可能會做的第一件事是刪除20行左右的註釋代碼! –
順便說一句 - 比'JFrame'中的paint(Graphics)'更好地覆蓋'JPanel'中的'paintComponent(Graphics)'。 –