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 6822741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:41:59+00:00 2026-05-26T21:41:59+00:00

I am trying to make a simple line drawing program in java, I am

  • 0

I am trying to make a simple line drawing program in java, I am storing each pixel on the screen in an array for drawing. When a user drags the mouse, each pixel is set to 1, then I try to iterate through and draw a line between each pair of points. However it is not drawing properly, can someone see the problem here?

public void mouseDragged(MouseEvent m) {
    screen[m.getX()][m.getY()] = 1;
    drawOffscreen();
}

public void mouseReleased(MouseEvent e) {
    end[e.getX()][e.getY()] = true;     
}


int prex = -1;
int prey = -1;
public void paint(Graphics g) {
    g.drawImage(offscreen, 0, 0, null);     
    for (int x = 0; x < screen.length; x++){
        for (int y = 0; y < screen[0].length; y++){
            if(screen[x][y] == 1){
                if (prex != -1 && prey != -1 && !end[x][y]){
                    g.drawLine(prex, prey, x, y);                                               
                }
                prex = x;
                prey = y;
            }
        }
    }

}
  • 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-05-26T21:42:00+00:00Added an answer on May 26, 2026 at 9:42 pm

    (I think it is a homework? if so, please tag it as homework)

    I bet no one will clearly know what do u mean by “not drawing correctly”. Anyway, one of the problem I can see.

    I bet you are storing only 1 line. However the way you store and draw is problematic.

    You stored a the coordinates that your mouse “passed” by marking the coordinate on the “virtual screen”. However, when you draw that on screen, you are not following the order the mouse passed. Instead, you are drawing lines in the order from top to down, left to right, which is just giving you a mess.

    You may consider storing a list of coordinate, and when you paint, you draw according to the coordinate.

    Pseudo-code:

    class Coordinate {  // there are some class in Java that already does that, 
                        //leave it to you to find out  :)
      int x;
      int y;
    }
    
    List<Coordinate> theOnlyLine=....;
    public void mouseDragged(MouseEvent m) {
        theOnlyLine.add(new Coordinate(m.getX(), m.getY());
    }
    
    public void mouseReleased(MouseEvent e) {
        theOnlyLine.add(new Coordinate(m.getX(), m.getY());
    }
    
    public void paint(Graphics g) {
    
      int prevX = -1;
      int prevY = -1;
      for (Coordinate coordinate : theOnlyLine) {
        if (prevX > 0 && prevY > 0) {
          g.drawLine(prevX, prevY, coordinate.getX(), coordinate.getY());
        }
        prevX = coordinate.getX();
        prevY = coordinate.getY();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I'm trying to make some simple XML with Java and org.w3c.dom, but I got
I'm trying to make a simple drawing app in c++, but i'm having trouble
I am trying to make simple regex that will check if a line is
I'm trying to make a simple version of getline. It should read a line
I'm trying to make a simple three question quiz in php. After the user
I'm trying to make a simple http client server using java. It will show
Trying to make a call and retrieve a very simple, one line, JSON file.
I am trying to make a simple phone directory program in windows console application.
I am trying to make a simple calculator. I am getting blue line under

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.