由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - java graphics2d 画图请教
相关主题
请问Java如何实现对.jpg图片的缩放显示?用java画直线的问题
SkylineJava 里划直线加粗的问题
questions on repaint()怎么编辑和保存JAVA的图片输出?
how to update swing componentsweb service returns HashMap that contains multiple ArrayList
Open Too Many Cursor Issueplease help me to solve this question!
怎么能够减少image上的锯齿?Get windows info
how to improve java programming abilityRe: JPanel help!
用java 2D作图, 画线条的时候都成锯齿状怎么办Re: Swing问题
相关话题的讨论汇总
话题: current话题: graphics2d话题: highlights话题: public话题: points
进入Java版参与讨论
1 (共1页)
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所有得图
: 颜色都变了,而不是当前选中得图,这种情况一般怎么只变当前图得颜色呢??再画另
: 外一个图得时候,其他得图不受影
: 响??就是存在不同颜色??

1 (共1页)
进入Java版参与讨论
相关主题
Re: Swing问题Open Too Many Cursor Issue
paint()呀,repaint()呀怎么能够减少image上的锯齿?
农民运动讲习所1. Thread and paint()how to improve java programming ability
question about repaint, update and paint用java 2D作图, 画线条的时候都成锯齿状怎么办
请问Java如何实现对.jpg图片的缩放显示?用java画直线的问题
SkylineJava 里划直线加粗的问题
questions on repaint()怎么编辑和保存JAVA的图片输出?
how to update swing componentsweb service returns HashMap that contains multiple ArrayList
相关话题的讨论汇总
话题: current话题: graphics2d话题: highlights话题: public话题: points