我希望能够将使用图形对象创建的形状存储为变量,但是输出的类型为void,并且将其转换为无效。
目前我只能使用paintComponent方法使用Graphics引用。但是我希望能够将一个形状(碰巧是void类型)存储为Graphic类型的实例变量...
请考虑以下事项:
test class{
Graphics instanceOfGraphics;
public void createShape(Graphics g1){
g1.setPaint(new Color(255, 0, 0));
instanceOfGraphics = g1.fillOval(//xPosition, //yPosition,
//width, //height);
}
}
上面的源代码不起作用,因为fillOval方法的返回类型是void,即使我尝试转换静默返回值它也不起作用。
我如何将创建的形状保存/存储为变量。
将来我希望能够在Shape上注册一个Listener,并允许我创建的形状成为事件源。
答案 0 :(得分:4)
上面的源代码不起作用,因为fillOval方法的返回类型是void,即使我尝试转换静默返回值它也不起作用。
您正在考虑存储效果,这是不可能的,您要做的是存储数据。如果要存储椭圆型数据,只需使用Ellipse2D
字段并将信息存储在那里。
Ellipse2D ellipse = new Ellipse2D.Double(//xPosition, //yPosition,
//width, //height);
然后在paintComponent中使用Graphics2D对象绘制它。
Swing具有有用的Shape接口,该接口已经实现为几个可以很好地满足您目的的具体类。
如,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
public class GraphicsEg extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private List<Shape> shapes = new ArrayList<>();
private Map<Shape, Color> shapeColorMap = new HashMap<>();
public GraphicsEg() {
Shape shape = new Ellipse2D.Double(10, 10, 30, 30);
shapeColorMap.put(shape, Color.RED);
shapes.add(shape);
shape = new Rectangle2D.Double(140, 140, 200, 200);
shapeColorMap.put(shape, Color.BLUE);
shapes.add(shape);
shape = new RoundRectangle2D.Double(200, 200, 80, 80, 10, 10);
shapeColorMap.put(shape, Color.GREEN);
shapes.add(shape);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Shape shape : shapes) {
Color color = shapeColorMap.get(shape);
g2.setColor(color);
g2.fill(shape);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GraphicsEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GraphicsEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这里有几个不同的形状(Ellipse2D,Rectangle2D,RoundRectangle2D)存储在Shape对象的ArrayList中,然后在第一次将Graphics对象强制转换为Graphics2D之后,在JPanel的paintComponent方法中绘制这些形状。我还添加了一个HashMap<Shape, Color>
,以便将Shape与颜色轻松关联。
另外,您在评论中说明,
将其用作事件来源
为所有实现Shape的类带来了另一个有用的功能 - 它们有一个contains(...)
方法,如果与MouseListener一起使用,可以让你轻松地看到鼠标是否在你的一个形状内部被点击。 / p>
例如:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class GraphicsEg extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final Color SELECTED_COLOR = Color.RED;
private static final Stroke SELECTED_STROKE = new BasicStroke(8f);
private List<Shape> shapes = new ArrayList<>();
private Map<Shape, Color> shapeColorMap = new HashMap<>();
private Shape selectedShape = null;
public GraphicsEg() {
Shape shape = new Ellipse2D.Double(10, 10, 90, 90);
shapeColorMap.put(shape, Color.GRAY);
shapes.add(shape);
shape = new Rectangle2D.Double(140, 140, 200, 200);
shapeColorMap.put(shape, Color.BLUE);
shapes.add(shape);
shape = new RoundRectangle2D.Double(200, 200, 80, 80, 10, 10);
shapeColorMap.put(shape, Color.GREEN);
shapes.add(shape);
addMouseListener(new MyMouseListener());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Shape shape : shapes) {
Color color = shapeColorMap.get(shape);
g2.setColor(color);
g2.fill(shape);
}
if (selectedShape != null) {
Graphics2D newG2 = (Graphics2D) g2.create();
newG2.setColor(SELECTED_COLOR);
newG2.setStroke(SELECTED_STROKE);
newG2.draw(selectedShape);
newG2.dispose(); // because this is a created Graphics object
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
for (int i = shapes.size() - 1; i >= 0; i--) {
if (shapes.get(i).contains(e.getPoint())) {
selectedShape = shapes.get(i);
repaint();
return;
}
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GraphicsEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GraphicsEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}