更改 JPanel 中形状列表的颜色 java
Change color of a list of shape in a JPanel java
我在 JPanel 上绘制了一个 Shape 列表,我需要更改那些包含通过鼠标单击获得的点的颜色,但我真的不明白如何使用 repaint() 来做到这一点。
这是测试class:
public class TestShapeViewer
{
public static void main(String[] args)
{
List<Shape> figure = new ArrayList<Shape>();
Utils util = new Utils();
figure.add(new Rect(new P2d(200,200), new P2d(90,40)));
figure.add(new Circle(new P2d (150,150), new P2d (300,150)));
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowWindow(figure);
}
});
}
private static void createAndShowWindow(List<Shape> figure)
{
JFrame window = new JFrame("TestShapeViewer");
window.setSize(500,500);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new Viewer(figure));
window.pack();
window.setVisible(true);
}
}
这是我的 JPanel class
public class Viewer extends JPanel implements ShapeViewer
{
private List<Shape> shapesToDraw;
private List<Shape> shapeToColor;
private Utils utility = new Utils();
private Color colorToUse;
public Viewer(List<Shape> shapes)
{
this.shapesToDraw = shapes;
this.colorToUse = Color.BLACK;
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
repaintShapes(e.getX(), e.getY());
}
});
}
public void update(List<Shape> shapes)
{
this.shapesToDraw = shapes;
}
public Dimension getPreferredSize()
{
return new Dimension(500,500);
}
public void paintComponent(Graphics shape)
{
super.paintComponent(shape);
shape.setColor(colorToUse);
shapesToDraw.stream().forEach(x -> DrawShape(shape,x));
}
public void DrawShape(Graphics shape, Shape s)
{
BBox box = s.getBBox();
if (s instanceof Line)
{
shape.drawLine(box.getUpperLeft().getX(),
box.getUpperLeft().getY(),
box.getBottomRight().getX(),
box.getBottomRight().getY());
}
else if(s instanceof Rect)
{
int[] xpoints = {box.getUpperLeft().getX(),
box.getBottomRight().getX(),
box.getBottomRight().getX(),
box.getUpperLeft().getX()};
int[] ypoints = {box.getUpperLeft().getY(),
box.getUpperLeft().getY(),
box.getBottomRight().getY(),
box.getBottomRight().getY()};
Polygon rect = new Polygon(xpoints, ypoints, 4);
shape.drawPolygon(rect);
}
else if(s instanceof Circle)
{
int raggio = (int)(new Line(box.getUpperLeft(),
new P2d(box.getBottomRight().getX(),
box.getUpperLeft().getY())).getPerim())/2;
shape.drawOval(box.getUpperLeft().getX(),
box.getUpperLeft().getY(),
raggio*2, raggio*2);
}
else if(s instanceof Combo)
{
Combo co = (Combo) s;
co.getComp().stream().forEach(x -> DrawShape(shape,x));
}
}
private void repaintShapes(int x, int y)
{
shapeToColor = utility.getContaining(shapesToDraw,new P2d(x,y));
this.colorToUse = Color.RED;
repaint();
}
}
我的目的是单击一个点,检查哪些形状包含该点并仅更改它们的颜色。
为清楚起见:
- P2d 是一个 class 标识点 (x,y)
- Utils.getContaining() 是一种 returns 包含点
的形状列表的方法
- BBox 是包含形状的最小边界框
I have a list of Shape drawn on a JPanel and I need to change the color of those which contain a point
而不是保留形状列表。您可以保留 "ColoredShapes" 的列表。您将创建一个包含两个属性的自定义 class:1) 形状,2) 颜色并将此对象添加到列表中。当然你也需要在绘制Shape之前修改绘制代码获取Color信息
然后,当您使用鼠标单击某个点时,您将遍历列表以查找包含鼠标点的任何形状,并更新颜色。然后您只需在自定义绘画面板上调用 repaint()。
查看 Custom Painting Approaches 中的 DrawOnComponent
示例。此方法使用 "ColoredRectangle" 来跟踪这两个属性,因此该方法与您需要的非常相似。
我在 JPanel 上绘制了一个 Shape 列表,我需要更改那些包含通过鼠标单击获得的点的颜色,但我真的不明白如何使用 repaint() 来做到这一点。
这是测试class:
public class TestShapeViewer
{
public static void main(String[] args)
{
List<Shape> figure = new ArrayList<Shape>();
Utils util = new Utils();
figure.add(new Rect(new P2d(200,200), new P2d(90,40)));
figure.add(new Circle(new P2d (150,150), new P2d (300,150)));
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowWindow(figure);
}
});
}
private static void createAndShowWindow(List<Shape> figure)
{
JFrame window = new JFrame("TestShapeViewer");
window.setSize(500,500);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new Viewer(figure));
window.pack();
window.setVisible(true);
}
}
这是我的 JPanel class
public class Viewer extends JPanel implements ShapeViewer
{
private List<Shape> shapesToDraw;
private List<Shape> shapeToColor;
private Utils utility = new Utils();
private Color colorToUse;
public Viewer(List<Shape> shapes)
{
this.shapesToDraw = shapes;
this.colorToUse = Color.BLACK;
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
repaintShapes(e.getX(), e.getY());
}
});
}
public void update(List<Shape> shapes)
{
this.shapesToDraw = shapes;
}
public Dimension getPreferredSize()
{
return new Dimension(500,500);
}
public void paintComponent(Graphics shape)
{
super.paintComponent(shape);
shape.setColor(colorToUse);
shapesToDraw.stream().forEach(x -> DrawShape(shape,x));
}
public void DrawShape(Graphics shape, Shape s)
{
BBox box = s.getBBox();
if (s instanceof Line)
{
shape.drawLine(box.getUpperLeft().getX(),
box.getUpperLeft().getY(),
box.getBottomRight().getX(),
box.getBottomRight().getY());
}
else if(s instanceof Rect)
{
int[] xpoints = {box.getUpperLeft().getX(),
box.getBottomRight().getX(),
box.getBottomRight().getX(),
box.getUpperLeft().getX()};
int[] ypoints = {box.getUpperLeft().getY(),
box.getUpperLeft().getY(),
box.getBottomRight().getY(),
box.getBottomRight().getY()};
Polygon rect = new Polygon(xpoints, ypoints, 4);
shape.drawPolygon(rect);
}
else if(s instanceof Circle)
{
int raggio = (int)(new Line(box.getUpperLeft(),
new P2d(box.getBottomRight().getX(),
box.getUpperLeft().getY())).getPerim())/2;
shape.drawOval(box.getUpperLeft().getX(),
box.getUpperLeft().getY(),
raggio*2, raggio*2);
}
else if(s instanceof Combo)
{
Combo co = (Combo) s;
co.getComp().stream().forEach(x -> DrawShape(shape,x));
}
}
private void repaintShapes(int x, int y)
{
shapeToColor = utility.getContaining(shapesToDraw,new P2d(x,y));
this.colorToUse = Color.RED;
repaint();
}
}
我的目的是单击一个点,检查哪些形状包含该点并仅更改它们的颜色。
为清楚起见:
- P2d 是一个 class 标识点 (x,y)
- Utils.getContaining() 是一种 returns 包含点 的形状列表的方法
- BBox 是包含形状的最小边界框
I have a list of Shape drawn on a JPanel and I need to change the color of those which contain a point
而不是保留形状列表。您可以保留 "ColoredShapes" 的列表。您将创建一个包含两个属性的自定义 class:1) 形状,2) 颜色并将此对象添加到列表中。当然你也需要在绘制Shape之前修改绘制代码获取Color信息
然后,当您使用鼠标单击某个点时,您将遍历列表以查找包含鼠标点的任何形状,并更新颜色。然后您只需在自定义绘画面板上调用 repaint()。
查看 Custom Painting Approaches 中的 DrawOnComponent
示例。此方法使用 "ColoredRectangle" 来跟踪这两个属性,因此该方法与您需要的非常相似。