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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:50:17+00:00 2026-06-15T05:50:17+00:00

I have a problem with this… When I draw a line the character follows

  • 0

I have a problem with this… When I draw a line the character follows the line I drew but the computer paints a line somewhere else:
enter image description here

So does anyone know what is going on?
My code:

@SuppressWarnings({"serial","rawtypes","unchecked"})
public class someGame extends JFrame implements MouseListener, KeyListener{

ArrayList lines = new ArrayList();
Point2D.Double start;
final Color BROWN = new Color(156,93,82);
Slider thread;
Rectangle cow = null;
boolean drawGuy = false;
public someGame(){
    super("Some Game");
    setSize(700,700);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addMouseListener(this);
    addKeyListener(this);
}

public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.black);
    for(int i = 0; i < lines.size(); i++){
        Line2D.Double temp = (Line2D.Double) lines.get(i);
        int x1 = Integer.parseInt(""+Math.round(temp.getX1()));
        int x2 = Integer.parseInt(""+Math.round(temp.getX2()));
        int y1 = Integer.parseInt(""+Math.round(temp.getY1()));
        int y2 = Integer.parseInt(""+Math.round(temp.getY2()));

        g.drawLine(x1,x2,y1,y2);
    }

    if(drawGuy){
        try{
            URL url = this.getClass().getResource("resources/img/world/char.png");
            Image image = Toolkit.getDefaultToolkit().getImage(url);
            g.drawImage(image, cow.x, cow.y, this);
        } catch(Exception exc){}
    }
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
    start = new Point2D.Double(e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e){
    Point2D.Double end = new Point2D.Double(e.getX(),e.getY());
    lines.add(new Line2D.Double(start,end));
    repaint();
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
    if(e.getKeyChar()=='p'||e.getKeyChar()=='P'){
        thread = new Slider();
        thread.start();
        thread.action(true);
    }
    if(e.getKeyChar()=='q'||e.getKeyChar()=='Q'){
        thread.action(false);
        drawGuy = false;
        thread = null;
    }
}
private class Slider extends Thread{
    double velocity, gravity;
    boolean go = false;
    public void run(){
        if(go){
            initGuy();
            velocity = 0;
            gravity = 1;
        }
        while(go){
            try{
                Line2D.Double lineTaken = null;
                boolean onLine = false;
                int firstOnLine = -1;
                for(int i = lines.size()-1; i>=0; i--){
                    Line2D.Double temp = (Line2D.Double) lines.get(i);
                    if(temp.intersects(cow.x,cow.y,50,50)){
                        lineTaken = temp;
                        onLine = true;
                        if(firstOnLine!=i){
                            firstOnLine = i;
                            gravity = 0;
                        }
                        break;
                    }
                }
                if(onLine){
                    double grav = (lineTaken.y2-lineTaken.y1)/50;
                    double vlct = (lineTaken.x2-lineTaken.x1)/100;
                    if(velocity<5)velocity+=vlct;
                    if(gravity<2.5)gravity+=grav;
                }
                else{
                    gravity+=.2;
                }
                cow.x+=velocity;
                cow.y+=gravity;

                Thread.sleep(75);
                repaint();
            }catch(Exception e){break;}
        }
    }
    public void action(boolean b){
        go = b;
    }
    public void initGuy(){
        Line2D.Double firstLine = (Line2D.Double) lines.get(0);
        int x = Integer.parseInt(""+Math.round(firstLine.x1));
        int y = Integer.parseInt(""+Math.round(firstLine.y1));
        cow = new Rectangle(x+30,y-20,30,30);
        drawGuy = true;
    }
}

Sorry for the large code but I have no idea what went wrong…
Big thanks to anyone who answers 🙂
…and yes, the main method is someGame g = new someGame();

  • 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-15T05:50:19+00:00Added an answer on June 15, 2026 at 5:50 am

    1) You should never use paint(..) without a good reason. rather extend JPanel class and override paintComponent(...).

    2) Dont extend JFrame class unless adding functionality

    3) Dont forget all Swing component creation and manipulation should be done on EDT – Event Dispatch Thread

    4) Java naming convention is CamelCase, and each new word for a class name should begin with capitals i.e SomeGame

    5) Dont use setSize rather override component getPreferredSize and return desired size and than call pack() on JFrame before setting visible.

    6) Use Keybindings instead of KeyListener

    7) Use MouseAdapter instead of MouseListener

    8) Do not extend Thread class rather implement Runnable and create start() method to start thread in runnable class (I have not implemented this in below code)

    9) Not a good idea to do long running tasks like load images or do any other work besides the necessaries for painting objects in paint method you should load picture etc on class initialization or at least outside of a components paint methods. (I have not implemented this in below code)

    10) Your biggest problem about lines not being drawing correctly was:

    g.drawLine(x1,x2,y1,y2);
    

    it should be drawLine(int x1,int y1,int x2,int y2):

    g.drawLine(x1,y1,x2,y2);
    

    Here is the code (Dont have time to explain right now hope you can understand):

    I added some extra stuff like casting to Graphics2D and using RenderHints for Anti-Aliasing as suggested by @MadProgrammer in his comment below.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.net.URL;
    import java.util.ArrayList;
    import javax.swing.AbstractAction;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    
    public class SomeGame {
    
        public SomeGame() {
            JFrame frame = new JFrame("Some Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            DrawPanel dp = new DrawPanel();
            frame.add(dp);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new SomeGame();
                }
            });
    
        }
    }
    
    class DrawPanel extends JPanel {
    
        ArrayList lines = new ArrayList();
        Point2D.Double start;
        final Color BROWN = new Color(156, 93, 82);
        Slider thread;
        Rectangle cow = null;
        boolean drawGuy = false;
    
        public DrawPanel() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseReleased(MouseEvent me) {
                    super.mouseReleased(me);
                    Point2D.Double end = new Point2D.Double(me.getX(), me.getY());
                    lines.add(new Line2D.Double(start, end));
                    repaint();
                }
    
                @Override
                public void mousePressed(MouseEvent me) {
                    super.mousePressed(me);
                    start = new Point2D.Double(me.getX(), me.getY());
                }
            });
            setKeyBindings();
        }
    
        private void setKeyBindings() {
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0), "P");
            getActionMap().put("P", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    thread = new Slider(DrawPanel.this);
                    thread.action(true);
                    thread.start();
                }
            });
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "q");
            getActionMap().put("Q", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    thread.action(false);
                    drawGuy = false;
                    thread = null;
                }
            });
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 700);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            //lets go 2D :)
            Graphics2D g2d = (Graphics2D) g;
    
            //turn on anti aliasing
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    
            g2d.setColor(Color.black);
    
            for (int i = 0; i < lines.size(); i++) {
                Line2D.Double temp = (Line2D.Double) lines.get(i);
                int x1 = Integer.parseInt("" + Math.round(temp.getX1()));
                int x2 = Integer.parseInt("" + Math.round(temp.getX2()));
                int y1 = Integer.parseInt("" + Math.round(temp.getY1()));
                int y2 = Integer.parseInt("" + Math.round(temp.getY2()));
    
                g2d.drawLine(x1, y1, x2, y2);
            }
    
            if (drawGuy) {
                try {
                    URL url = this.getClass().getResource("resources/img/world/char.png");
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    g.drawImage(image, cow.x, cow.y, this);
                } catch (Exception exc) {
                }
            }
        }
    
        private class Slider extends Thread {
    
            double velocity, gravity;
            boolean go = false;
            private final DrawPanel dp;
    
            private Slider(DrawPanel dp) {
                this.dp = dp;
            }
    
            public void run() {
                if (go) {
                    initGuy();
                    velocity = 0;
                    gravity = 1;
                }
                while (go) {
                    try {
                        Line2D.Double lineTaken = null;
                        boolean onLine = false;
                        int firstOnLine = -1;
                        for (int i = lines.size() - 1; i >= 0; i--) {
                            Line2D.Double temp = (Line2D.Double) lines.get(i);
                            if (temp.intersects(cow.x, cow.y, 50, 50)) {
                                lineTaken = temp;
                                onLine = true;
                                if (firstOnLine != i) {
                                    firstOnLine = i;
                                    gravity = 0;
                                }
                                break;
                            }
                        }
                        if (onLine) {
                            double grav = (lineTaken.y2 - lineTaken.y1) / 50;
                            double vlct = (lineTaken.x2 - lineTaken.x1) / 100;
                            if (velocity < 5) {
                                velocity += vlct;
                            }
                            if (gravity < 2.5) {
                                gravity += grav;
                            }
                        } else {
                            gravity += .2;
                        }
                        cow.x += velocity;
                        cow.y += gravity;
    
                        Thread.sleep(75);
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                dp.repaint();
                            }
                        });
                    } catch (Exception e) {
                        break;
                    }
                }
            }
    
            public void action(boolean b) {
                go = b;
            }
    
            public void initGuy() {
                Line2D.Double firstLine = (Line2D.Double) lines.get(0);
                int x = Integer.parseInt("" + Math.round(firstLine.x1));
                int y = Integer.parseInt("" + Math.round(firstLine.y1));
                cow = new Rectangle(x + 30, y - 20, 30, 30);
                drawGuy = true;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem when I run SAS 9.2 on the command line on
I'm using a font in my site: http://www.fontsquirrel.com/fonts/Bentham But I have a problem: This
Hey most of my issue has been solved but i have little problem This
Does anyone ever have problem like this?? The Enter , Tab , and space
I'm have problem with Isometric. I'm don't know how to name this problem, but
I using multilanguage solution from this thread Multilanguage in WPF But i have problem
I readed the userguide also this question , but still have problem with the
I try to do a migration but i have problem with this query :
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I really have problem with this one. So I have a jar with a

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.