JPanel从另一个类重新粉刷 [英] JPanel repaint from another class

查看:110
本文介绍了JPanel从另一个类重新粉刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示图像的JPanel.在一个单独的类中,我从xml文件中读取要点.我首先从这些点创建三角形的数组列表.但是我需要在图像上显示三角形,即在其上绘制! (是的,这应该很简单).但是,由于这些点和三角形是在另一个类中创建的,因此我似乎无法在GUI类中已经显示的图像上绘制它们.我尝试在JPanel本身中创建一个ArrayList,我对其进行了更新,然后想要重新绘制,尽管它不允许我这样做,如下所示:

I have a JPanel which displays an image. In a separate class, I'm reading from an xml file points. I am firstly creating an arraylist of triangles from these points. However I need to show the triangles on the image, i.e. draw them on! (yes this should be simple). But as these points and triangles are created in another class, I do not seem to be able to draw them on the already-displayed image within the GUI class. I have tried creating a ArrayList in the JPanel itself, which I update and then want to repaint, although it will not let me do this as shown below:

班级

triangles = clips.getTriangles();
tempPanel.setTriangles(triangles){

JPanel

 public void settriangles(ArrayList<Point[]> t){
 triangles = t;
 repaint();
}

我唯一的另一个想法是让JPanel拥有一个侦听器,等待返回三角形时进行更新,从而更新该字段,然后重新绘制.

My only other idea is for the JPanel to have a listener waiting for when triangles are returned, updating the field and hence then repainting.

有什么想法吗?

谢谢

绘图代码

public void settriangles(ArrayList<Point[]> t){
    triangles = t;
    repaint();
}

public void paintComponent(Graphics g) {

    System.out.println("in paint component");
if (g != null) {
    Graphics2D graphics = (Graphics2D) g;
    try {
        Rectangle back_rect = new Rectangle(0, 0, getWidth(),
                getHeight());
        graphics.setColor(GuiComponentGenerator.GUI_BACKGROUND_COLOUR);
        graphics.fill(back_rect);
        if (image != null) {
            int width = Math.round(image.getWidth() * magnification);
            int height = Math.round(image.getHeight() * magnification);
            Rectangle image_rect = new Rectangle(offset.x, offset.y,
                    width, height);
            graphics.setColor(Color.BLACK);
            graphics.draw(image_rect);
            graphics.drawImage(image, offset.x, offset.y, width,
                    height, null);
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            for(int pos = 0; pos < triangles.size(); pos++){
                Point[] current = triangles.get(pos);                   
                ArrayList<Point> current_triangle = new ArrayList<Point>();
                current_triangle.add(current[0]);
                current_triangle.add(current[1]);
                current_triangle.add(current[2]);
                drawRegion(graphics, current_triangle); 
            }
        }
    }

finally {
        graphics.dispose();
}
}



private void drawRegion(Graphics2D graphics, ArrayList<Point> points) {
    graphics.setColor(trans_grey);
    Area area = getArea(points);
    graphics.fill(area);
    graphics.setStroke(new BasicStroke(2));
    graphics.setColor(Color.BLACK);
    graphics.draw(area);
}

private Area getArea(ArrayList<Point> points) {
    Area area = new Area(getPath(points, true));
    return area;
}

private GeneralPath getPath(ArrayList<Point> points, boolean close_path) {
    GeneralPath path = new GeneralPath();
    Point current_screen_point = calculateScreenPoint(points.get(0));
    path.moveTo(current_screen_point.x, current_screen_point.y);
    for (int point_num = 1; point_num < points.size(); point_num++) {
        current_screen_point = calculateScreenPoint(points.get(point_num));
        path.lineTo(current_screen_point.x, current_screen_point.y);
    }
    if (close_path)
        path.closePath();
    return path;
}

public Point calculateScreenPoint(Point image_point) {
    float h_proportion = (float) image_point.x / (float) image.getWidth();
    float v_proportion = (float) image_point.y / (float) image.getHeight();
    float image_width_in_panel = (float) image.getWidth() * magnification;
    float image_height_in_panel = (float) image.getHeight() * magnification;

    Point on_screen_point = new Point(0, 0);
    on_screen_point.x = offset.x
            + Math.round(h_proportion * image_width_in_panel);
    on_screen_point.y = offset.y
            + Math.round(v_proportion * image_height_in_panel);
    return on_screen_point;
}

推荐答案

我研究了如何在通过arrayList时在图像上绘制三角形,其中每个Point []代表三角形的点.

I worked out how to draw triangles on the image, when passing through an arrayList, where each Point[] represents the points of the triangle.

请注意,这现在在传递信息的单个完整类中,而不是尝试从另一个类调用重绘.

Note that this is now in a single entire class which is passed the information, rather than trying to call repaint from another class.

public AnnotatedDisplayTriangles(BufferedImage image, String image_path, ArrayList<Point[]> triangles) {

    this.image = image;
    this.image_path = image_path;
    this.triangles = triangles;

}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Draw image centered.
    int x = (getWidth() - image.getWidth())/2;
    int y = (getHeight() - image.getHeight())/2;
    g.drawImage(image, x, y, this);

    Stroke drawingStroke = new BasicStroke(2);
    Graphics2D graph = (Graphics2D)g;
    graph.setStroke(drawingStroke);
    graph.setPaint(Color.black);


    for(int p = 0; p < triangles.size(); p++){

        Point[] current_triangles = triangles.get(p);

        for(int triangle = 0; triangle < current_triangles.length; triangle++ ){

            Point current = current_triangles[triangle];
            Point next;
            if(triangle == current_triangles.length -1 )
                next = current_triangles[0];
            else
                next = current_triangles[triangle + 1];

            Line2D line = new Line2D.Double(current.x, current.y, next.x, next.y);
            graph.draw(line);

        }
    }
}

public static void main(String image_path,ArrayList<Point[]> triangles, String panel_name) throws IOException {
    String path = image_path;
    BufferedImage image = ImageIO.read(new File(path));
    AnnotatedDisplayTriangles contentPane = new AnnotatedDisplayTriangles(image, path, triangles);
    // You'll want to be sure this component is opaque
    // since it is required for contentPanes. Some
    // LAFs may use non-opaque components.
    contentPane.setOpaque(true);

    int w = image.getWidth();
    int h = image.getHeight();

    JFrame f = new JFrame(panel_name);
//  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(contentPane);
    f.setSize(w,h);
    f.setLocation(200,200);
    f.setVisible(true);
}

这篇关于JPanel从另一个类重新粉刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆