我已经搜索了很多,找不到运气的解决方案。我原以为有一个简单的解决方案。我有以下代码,其中我发现存储在ArrayList中的一些点,并且想要在ArrayList中给出的每个点处绘制一个形状(无论在这个阶段,矩形会做什么)。代码如下:
public static void main(String[] args){
image = null;
try {
// Read from a file
File file = new File("box.jpg");
image = ImageIO.read(file);
} catch (IOException e) {
System.out.print("LAAAAMMME!!!!");
}
ImagePanel panel = new ImagePanel(image); //Custom JPanel to show a background image
panel.setPreferredSize(new Dimension(500, 500));
JFrame frame = new JFrame("Find the Squares");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(panel);
frame.validate();
frame.setVisible(true);
frame.pack();
ArrayList<Point> points = getCenterPoint(floatarray);
for(int x = 0 ; x < points.size(); x++){
//Here I guess is where each point is created and drawn.
}
}
有什么建议吗?
答案 0 :(得分:1)
class MyTest{
public static void main(String[] args){
image = null;
try {
// Read from a file
File file = new File("box.jpg");
image = ImageIO.read(file);
} catch (IOException e) {
System.out.print("LAAAAMMME!!!!");
}
ImagePanel panel = new ImagePanel(image); //Custom JPanel to show a background image
panel.setPreferredSize(new Dimension(500, 500));
JFrame frame = new JFrame("Find the Squares");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(panel);
frame.add(new MainPanel(Color.Red));
frame.validate();
frame.setVisible(true);
frame.pack();
/*ArrayList<Point> points = getCenterPoint(floatarray);
for(int x = 0 ; x < points.size(); x++){
//Here I guess is where each point is created and drawn.
}*/
}
private class MainPanel extends JPanel {
Color color;
public MainPanel(Color color) {
this.color = color;
}
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(color);
//g.drawRect(startx,starty,endx,endy); // here you can drawRect as per your value
}
}
}
检查this网站
您需要将坐标传递给MainPanel类,以便您可以在面板上绘制任何想要绘制的形状。
答案 1 :(得分:1)
将属性List<Point> list
添加到ImagePanel
。
覆盖paintComponent(g)
中的ImagePanel
。使用属性list
中的数据进行绘制。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(...);
}
将列表从JFrame
发送到ImagePanel
。
构建框架后,您可能需要调用frame.setLayout(new FlowLayout())
。