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

  • Home
  • SEARCH
  • 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 9231857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:10:26+00:00 2026-06-18T06:10:26+00:00

I need to draw lines between in java from a file that is formatted

  • 0

I need to draw lines between in java from a file that is formatted the following way:

5 //Number of lines of points
10   10
23   56
15   34
32   67
76   45

I think I am going to have to set up two arrays and then somehow add the values like that but I am completely lost and really need some guidance.
Any help would be appreciated! The code below is what needs to be modified to draw lines. Right now it just plots the points.

Code:

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Test {

    private static final String FILE = "Desktop/Assign2Test1.txt";
    private static Point[] points;

    public static void main(final String[] args){
        try{
            final BufferedReader br = new BufferedReader(new FileReader(new File(FILE)));
            points = new Point[Integer.parseInt(br.readLine())];
            int i = 0;
            int xMax = 0;
            int yMax = 0;
            while(br.ready()){
                final String[] split = br.readLine().split("\t");
                final int x = Integer.parseInt(split[0]);
                final int y = Integer.parseInt(split[1]);
                xMax = Math.max(x, xMax);
                yMax = Math.max(y, yMax);
                points[i++] = new Point(x, y);




            }
            final JFrame frame = new JFrame("Point Data Rendering");
            final Panel panel = new Panel();
            panel.setPreferredSize(new Dimension(xMax + 10, yMax + 10));
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
            frame.repaint();
        } catch (final Exception e){
            e.printStackTrace();
        }
    }

    public static class Panel extends JPanel {

        @Override
        public void paintComponent(final Graphics g){
            g.setColor(Color.RED);
            for(final Point p : points){
                g.fillRect((int) p.getX(), (int) p.getY(), 2, 2);
            }
        }

    }

}
  • 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-18T06:10:27+00:00Added an answer on June 18, 2026 at 6:10 am

    Here is code that will help you how to read file and how to extract values from there. This is first you have to make it correctly before doing any other stuff.

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class BufferedReaderExample {
    
        public static void main(String[] sm) {
    
            BufferedReader br = null;
    
            try {
    
                String sCurrentLine;
    
                br = new BufferedReader(new FileReader("You file path"));
                String[] xy;
                // get your points and convert very first line
                int points = Integer.parseInt(br.readLine()); 
                while ((sCurrentLine = br.readLine()) != null) {
                    xy = sCurrentLine.split("\\s+"); // split by whitespace
                    System.out.println(xy[0] +" : "+ xy[1]);
                    // Do something
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)
                                            br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
        }
    }
    

    EDIT: –> if you want to draw line between two points. you have to make use of drawline() method. It should go something like below. I am also giving you link how to make line is java.

    1. Graphic drawLine() API

    2. How to draw lines in Java

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            g.setColor(line.color);
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
    
        }
    }
    

    If you have any question let us know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to draw 100 lines between 100 semi-random points in a 800x800 pixel
I need to draw a line from two points and what I did so
I've faced a problem in Qt where I need to draw lines with borders
I need to draw a lot of lines. I'm using UIBezierPath for drawing lines
My need is to draw a basic x-axis, y-axis plot of several lines, with
i need to draw a polygon of n sides given 2 points (the center
How would you draw a line connector between two shapes, such that the line
I have computed the values of the points I need to draw and I
i'm stuck at this problem. I need to draw a line between the previous
I need to draw a line between 2 divs. I currently use jQuery. The

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.