Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:47:57+00:00 2026-06-10T09:47:57+00:00

I have a JPanel which displays an image. In a separate class, I’m reading

  • 0

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:

Class

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

JPanel

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

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.

Any ideas?

Thanks

Edit: Code for Drawing

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:47:58+00:00Added an answer on June 10, 2026 at 9:47 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets
I have a JPanel that displays a group of JLabels which are all computed
I want to have a JPanel which uses an image as a background, with
Java Swing question. I have a JPanel which displays a graph. When I move
I have class PrintUtilities , which has the following method to print JPanel and
I have a JPanel which draws .png images. each image has 2 copies to
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel which contains some fields. The height of the JPanel is
I have a JPanel (A) which contains another JPanel (B). Each panel implements a
I have a JPanel to which when a button is pressed I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.