a*****p 发帖数: 1285 | 1 java 里面建了arraylist来做画图程序,其他function都就绪了,现在color上
碰到点问题。因为用得arraylist,
每次graphics2d.setcolor得时候,一repaint,画图板上所有得图颜色都变了,怎么让
单个得图颜色变啊??变了之后还能
保存,每个图形自己本身有color属性么?? | a*****p 发帖数: 1285 | 2 可能说得不明白,补充一下。
我用graphics2d画图,一个component上面好几个shape,然后我换color,用g2.
setcolor,问题是一换color所有得图
颜色都变了,而不是当前选中得图,这种情况一般怎么只变当前图得颜色呢??再画另
外一个图得时候,其他得图不受影
响??就是存在不同颜色??
【在 a*****p 的大作中提到】 : java 里面建了arraylist来做画图程序,其他function都就绪了,现在color上 : 碰到点问题。因为用得arraylist, : 每次graphics2d.setcolor得时候,一repaint,画图板上所有得图颜色都变了,怎么让 : 单个得图颜色变啊??变了之后还能 : 保存,每个图形自己本身有color属性么??
| h**********c 发帖数: 4120 | 3 opengl 是这么干的:
push color1
change to color2
pop color1
大概就这个意思,g2.setcolor的时候先getcolor找地保存一下,
g2 画完了,再set color。
你说的意思还是比较含糊,最好把原代码帖上来,
我以前用java canvas画过比较复杂的东西,时间长了,后改opengl了。
【在 a*****p 的大作中提到】 : 可能说得不明白,补充一下。 : 我用graphics2d画图,一个component上面好几个shape,然后我换color,用g2. : setcolor,问题是一换color所有得图 : 颜色都变了,而不是当前选中得图,这种情况一般怎么只变当前图得颜色呢??再画另 : 外一个图得时候,其他得图不受影 : 响??就是存在不同颜色??
| a*****p 发帖数: 1285 | 4 public class MouseComponent extends JComponent {
public MouseComponent() {
shapes = new ArrayList();
lines = new ArrayList();
bounds = new ArrayList();
current = null;
startpoint = null;
endpoint = null;
isSelected = false;
eraserSelected = false;
cursorPointer = false;
isLine = false;
clear = false;
filled = false;
shapeIndex = 2;
pointCounter = 4;
penColor = Color.BLACK;
penStroke = 1f;
points = new Point2D[pointCounter];
highlights = new Rectangle2D[pointCounter];
for(int i=0; i
points[i] = new Point2D.Double(0, 0);
highlights[i] = new Rectangle2D.Double(0, 0, 0, 0);
}
// toolkit = Toolkit.getDefaultToolkit();
// selectorImg = toolkit.getImage("selector.jpg");
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// g.setColor(penColor);
// g2.setStroke(new BasicStroke(penStroke));
g.drawImage(imageBuffer, 0, 0, this);
// g2d.draw(current);
if(clear == true) {
shapes.clear();
bounds.clear();
isLine = false;
current = null;
}
// for (Shape r : shapes) {
// if(current != null && isSelected && r == current) {
// drawPenColor(g, current);
// } else
// g2.draw(r);
// }
if(current != null && isSelected) {
for(int j=0; j
setHighlight();
bounds.add(highlights[j]);
}
if(filled) g2.fill(current);
}
for (Rectangle2D r : bounds) {
g2.fill(r);
}
if(current != null && isSelected == true && eraserSelected == true) {
removeShapes(current);
}
bounds.clear();
eraserSelected = false;
clear = false;
}
Graphics g = this.getGraphics();
Graphics2D g2d;
// Graphics2D g2d = (Graphics2D) g;
Image imageBuffer;
int canvasWidth, canvasHeight;
private ArrayList shapes;
private ArrayList lines;
private Shape current;
private Point2D startpoint, endpoint;
private ArrayList bounds;
public boolean isSelected;
public boolean eraserSelected;
public boolean cursorPointer;
public boolean isLine;
public boolean clear;
public boolean filled;
protected int pointCounter;
protected int shapeIndex;
protected Color penColor;
protected float penStroke;
private Point2D[] points;
private Rectangle2D[] highlights;
// private Point2D[] points = new Point2D[pointCounter];
// private Rectangle2D[] highlights = new Rectangle2D[pointCounter];
// Toolkit toolkit;
// Image selectorImg;
public void setpenColor(Color c) {
penColor = c;
g.setColor(c);
}
public void setBounds(int x, int y, int width, int height) {
// Image newimageBuffer = createImage(width, height);
imageBuffer = createImage(width, height);
// g = newimageBuffer.getGraphics();
g = imageBuffer.getGraphics();
g2d = (Graphics2D) g;
if (imageBuffer != null) {
g.drawImage(imageBuffer, 0, 0 ,this);
}
// imageBuffer = newimageBuffer;
setpenColor(penColor);
super.setBounds(x, y, width, height);
repaint();
canvasWidth = width;
canvasHeight = height;
}
public void addShapes(Point2D startp, Point2D endp) {
if(!startp.equals(endp)) {
switch(shapeIndex) {
case 1: {
current = new Line2D.Double(startp, endp);
isLine = true;
lines.add((Line2D) current);
shapes.add(current);
g2d.setColor(penColor);
g2d.draw(current);
repaint();
break;
}
case 2: {
double startx = startp.getX();
double starty = startp.getY();
double endx = endp.getX();
double endy = endp.getY();
double width, height, x, y;
if(startx < endx) x=startx;
else x=endx;
if(starty < endy) y=starty;
else y=endy;
width = Math.abs(startx-endx);
height = Math.abs(starty-endy);
current = new Rectangle2D.Double(x, y, width, height);
isLine = false;
shapes.add(current);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(penColor);
g2.draw(current);
repaint();
break;
}
case 3: {
double startx = startp.getX();
double starty = startp.getY();
double endx = endp.getX();
double endy = endp.getY();
double width, height, x, y;
if(startx < endx) x=startx;
else x=endx;
if(starty < endy) y=starty;
else y=endy;
width = Math.abs(startx-endx);
height = Math.abs(starty-endy);
current = new Ellipse2D.Double(x, y, width, height);
isLine = false;
shapes.add(current);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(penColor);
g2.draw(current);
repaint();
break;
}
}
}
}
// public void drawPenColor(Graphics g, Shape s) {
// Graphics2D gnew = (Graphics2D) g;
// gnew.setColor(penColor);
// gnew.draw(s);
// }
public void setHighlight() {
if(current != null) {
if(isLine) {
pointCounter = 2;
Line2D line = new Line2D.Double();
line = (Line2D) current;
points[0].setLocation(line.getP1());
points[1].setLocation(line.getP2());
points[2].setLocation(0, 0);
points[3].setLocation(0, 0);
for(int j=0; j<2; j++) {
highlights[j] = new Rectangle2D.Double(points[j].getX()-
10, points[j].getY()-10, 20, 20);
}
highlights[2] = new Rectangle2D.Double(0, 0, 0, 0);
highlights[3] = new Rectangle2D.Double(0, 0, 0, 0);
} else {
pointCounter = 4;
Rectangle2D rect = new Rectangle2D.Double();
rect = current.getBounds2D();
points[0].setLocation(rect.getMinX(), rect.getMinY());
points[1].setLocation(rect.getMaxX(), rect.getMaxY());
points[2].setLocation(rect.getMinX(), rect.getMaxY());
points[3].setLocation(rect.getMaxX(), rect.getMinY());
for(int j=0; j
highlights[j] = new Rectangle2D.Double(points[j].getX()-
10, points[j].getY()-10, 20, 20);
}
}
repaint();
}
}
public Shape findShape(Point2D p) {
for(Line2D l: lines) {
if(l.getBounds2D().contains(p) && l.ptLineDistSq(p)<= 15) return
l;
}
for (Shape r : shapes) {
if(r.equals(current)) {
if (current != null && isSelected == true) {
if(highlights[0].contains(p)||highlights[1].contains(p)
||highlights[2].contains(p)||highlights[3].contains(
p)
||r.contains(p)) return current;
}
} else {
if(r.contains(p)) return r;
}
}
return null;
}
public void removeShapes(Shape r) {
if (r == null) return;
if (r == current) {
if(lines.contains(current)) isLine=false;
current = null;
}
shapes.remove(r);
repaint();
}
// public Cursor createCursor(int x) {
// int i = x;
// Cursor cursor = Cursor.getDefaultCursor();
// switch (i) {
// case 1:
// cursor = toolkit.createCustomCursor(selectorImg, new Point(20,
20), "selectorCursor");
// }
// return cursor;
// }
private class MouseHandler implements MouseListener {
public void mousePressed(MouseEvent event) {
current = findShape(event.getPoint());
if (current == null) startpoint = event.getPoint();
}
public void mouseReleased(MouseEvent event) {
endpoint = event.getPoint();
if(current == null) addShapes(startpoint, endpoint);
}
public void mouseClicked(MouseEvent event) {
current = findShape(event.getPoint());
if(lines.contains(current)) isLine=true;
repaint();
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
private class MouseMotionHandler implements MouseMotionListener {
public void mouseMoved(MouseEvent event) {
if(current != null && isSelected == true) {
cursorPointer = highlights[0].contains(event.getPoint())||
highlights[1].contains(event.getPoint())
||highlights[2].contains(event.getPoint())||highlights[3
].contains(event.getPoint());
if(cursorPointer)
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_
CURSOR));
else setCursor(Cursor.getDefaultCursor());
}
}
public void mouseDragged(MouseEvent event) {
if (current != null && isSelected == true) {
if(isLine) {
Line2D line = new Line2D.Double();
line = (Line2D) current;
if(highlights[0].contains(event.getPoint())) {
line.setLine(event.getPoint(), points[1]);
} else if(highlights[1].contains(event.getPoint())) {
line.setLine(points[0], event.getPoint());
} else {
double px = event.getPoint().getX();
double py = event.getPoint().getY();
double dx = line.getX2()-line.getX1();
double dy = line.getY2()-line.getY1();
line.setLine(px+dx/2, py+dy/2, px-dx/2, py-dy/2);
}
current = line;
repaint();
} else {
RectangularShape rect = (RectangularShape) current;
if(highlights[0].contains(event.getPoint())) {
rect.setFrameFromDiagonal(event.getPoint(), points[1
]);
// Graphics2D g2 = (Graphics2D) g;
// g2.draw(current);
} else if(highlights[1].contains(event.getPoint())) {
rect.setFrameFromDiagonal(points[0], event.getPoint(
));
// Graphics2D g2 = (Graphics2D) g;
// g2.draw(current);
} else if(highlights[2].contains(event.getPoint())) {
rect.setFrameFromDiagonal(event.getPoint(), points[3
]);
// Graphics2D g2 = (Graphics2D) g;
// g2.draw(current);
} else if(highlights[3].contains(event.getPoint())) {
rect.setFrameFromDiagonal(points[2], event.getPoint(
));
// Graphics2D g2 = (Graphics2D) g;
// g2.draw(current);
} else {
double x = event.getX();
double y = event.getY();
double width, height;
width = rect.getWidth();
height = rect.getHeight();
rect.setFrame(x - width/2, y - height/2, width,
height);
}
current = rect;
g2d.draw(current);
repaint();
}
}
}
}
} | a*****p 发帖数: 1285 | 5 有点长,这个现在是拖拽得时候,画好多图,而不是更新那一个图。 | a*****p 发帖数: 1285 | 6 贴了代码,能帮看看么??/
【在 h**********c 的大作中提到】 : opengl 是这么干的: : push color1 : change to color2 : pop color1 : 大概就这个意思,g2.setcolor的时候先getcolor找地保存一下, : g2 画完了,再set color。 : 你说的意思还是比较含糊,最好把原代码帖上来, : 我以前用java canvas画过比较复杂的东西,时间长了,后改opengl了。
| g*****g 发帖数: 34805 | 7 All you need is a hashmap instance variable from shape to color.
【在 a*****p 的大作中提到】 : 可能说得不明白,补充一下。 : 我用graphics2d画图,一个component上面好几个shape,然后我换color,用g2. : setcolor,问题是一换color所有得图 : 颜色都变了,而不是当前选中得图,这种情况一般怎么只变当前图得颜色呢??再画另 : 外一个图得时候,其他得图不受影 : 响??就是存在不同颜色??
|
|