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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:51:22+00:00 2026-06-14T20:51:22+00:00

This is the thread to make the numbers show up: class CoOrdCounter extends Thread{

  • 0

This is the thread to make the numbers show up:

    class CoOrdCounter extends Thread{
    public void run(){
        try{
            while(true){
                    Thread.sleep(500);
                    printX = cow.x;
                    printY = cow.y;
            }
        }catch(Exception e){}
    }
}
//Of course I also have CoOrdCounter co = new CoOrdCounter; co.start();

And then…
g.drawString("Co-ords: ("+printX+","+printY+")",50,100);
It always displays (0,0), probably because of int printX = 0, printY = 0;. So that means the variables were NOT changed at all… Why? And how should I make this work…?
[PLEASE] I know I shouldn’t use paint() but I’m beginner-intermediate so…
[EDIT] And now that MadProgrammer mentions it, I do get an EventDispatchThread error.
[EDIT 2] I understand to use a Timer but @MadProgrammer how do I get cow.x and cow.y by using your code snippet?
[EDIT 3] If you type the key p the KeyListener will start the instance of theThread Slider, thread.
[UPDATE CLASS 2 (the real thing)]

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;
import java.net.*;
@SuppressWarnings({"serial","rawtypes","unchecked"})
public class CowbenderI extends JFrame implements MouseListener, KeyListener{

CoOrdCounter co;
Counter cnt;
Icon icon = new ImageIcon("resources/img/cow.png");
int focusX = 0, focusY = 0, counter = 0;
ArrayList lines = new ArrayList();
Point2D.Double start;
final Color BROWN = new Color(156,93,82);
Slider thread;
Rectangle cow = null;
boolean drawGuy = false, useFinal = false, checkedForWin = false, alive = true;
int finalTime = 0;
int printX = 0, printY = 0;
public CowbenderI(){
    super("Cowbender I - \"Slope Run\"");
    setSize(700,700);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addMouseListener(this);
    addKeyListener(this);

    cnt = new Counter();
    cnt.go = false;
    cnt.start();
    co = new CoOrdCounter();
    co.go = false;
    co.start();
}

public void paint(Graphics g){
    super.paint(g);
    try{
        g.setColor(Color.WHITE);
        g.fillRect(-2000, -2000, 5000, 5000);
        URL url = this.getClass().getResource("resources/img/world/slope.png");
        Image bckgrnd = Toolkit.getDefaultToolkit().getImage(url);
        g.drawImage(bckgrnd,0,0,this);
    }catch(Exception e){}
    g.setColor(BROWN);
    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-focusX,y1-focusY,x2-focusX,y2-focusY);
    }

    if(drawGuy){
        try{
            g.setFont(new Font("Arial",Font.BOLD,16));
            if(useFinal == false) g.drawString("Current Time: "+counter,50,50);
            else g.drawString("Current Time: "+finalTime,50,50);
            g.drawString("Co-ords: ("+printX+","+printY+")",50,100);
            if(alive == true){
                URL url = this.getClass().getResource("resources/img/world/char.png");
                Image image = Toolkit.getDefaultToolkit().getImage(url);
                g.drawImage(image, cow.x-focusX, cow.y-focusY, this);
            }
            else{
                URL url = this.getClass().getResource("resources/img/world/deadchar.png");
                Image image = Toolkit.getDefaultToolkit().getImage(url);
                g.drawImage(image, cow.x-focusX, cow.y-focusY, this);
                    if(checkedForWin == false){
                        finalTime = counter;
                        useFinal = true;
                        checkWin();
                    }
                }
        } catch(Exception exc){}
        focusX = cow.x-100;
        focusY = cow.y-100;
    }
}

public void checkWin(){
    if(finalTime >= 45){
        JOptionPane.showMessageDialog(null,"You won!\nThe farmer got tired and ran back!","Cowbender I - The Slope",JOptionPane.INFORMATION_MESSAGE,icon);
        System.exit(0);
    }
}

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()+focusX,e.getY()+focusY);
}
public void mouseReleased(MouseEvent e){
    Point2D.Double end = new Point2D.Double(e.getX()+focusX,e.getY()+focusY);
    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()=='w'||e.getKeyChar()=='W'){
        focusY-=100;
        repaint();
    }
    if(e.getKeyChar()=='a'||e.getKeyChar()=='A'){
        focusX-=100;
        repaint();
    }
    if(e.getKeyChar()=='s'||e.getKeyChar()=='S'){
        focusY+=100;
        repaint();
    }
    if(e.getKeyChar()=='d'||e.getKeyChar()=='D'){
        focusX+=100;
        repaint();
    }
    if(e.getKeyChar()=='p'||e.getKeyChar()=='P'){
        alive = true;
        counter = 0;
        cnt.go = true;
        co.go = true;
        useFinal = false;
        thread = new Slider();
        thread.start();
        thread.action(true);
    }
    if(e.getKeyChar()=='z'||e.getKeyChar()=='Z'){
        lines.remove(lines.size()-1);
        repaint();
    }
    if(e.getKeyChar()=='x'||e.getKeyChar()=='X'){
        int response = milkSwing.confirmBox(null, "Do you really want to remove all of your summoned earth?", "Cowbender - The Slope", JOptionPane.YES_NO_OPTION);
        if(response == JOptionPane.YES_OPTION) lines.clear();
        repaint();
    }
    if(e.getKeyChar()=='q'||e.getKeyChar()=='Q'){
        System.exit(0);
    }
}

class CoOrdCounter extends Thread{
    public boolean go = true;
    public void run(){
        try{
            while(true){
                if(go){
                    Thread.sleep(500);
                    printX = cow.x;
                    printY = cow.y;
                    repaint();
                }
            }
        }catch(Exception e){}
    }
}

class Counter extends Thread{
    public boolean go = true;
    public void run(){
        try{
            while(true){
                if(go){
                    Thread.sleep(1000);
                    counter++;
                }
            }
        } catch(Exception e){}
    }
}

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;
                if(cow.x > 10000) alive = false;

                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+90,y-60,50,50);
        drawGuy = true;
    }
}
/**
 * @param args
 */
public static void main(String[] args) {
    CowbenderI g = new CowbenderI();

}
}<br />

[EDIT 4] @MadProgrammer not working:
enter image description here

  • 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-14T20:51:24+00:00Added an answer on June 14, 2026 at 8:51 pm

    You need to tell Swing that you want to update the UI. In you’re case that’s going to present issues as it’s possible to violate the contract with the Event Dispatching Thread (that you should never update the UI from any thread other then the Event Dispatching Thread)

    The example I’ve done below uses a simple javax.swing.Timer, which overcomes this issue

    public class PrintNumbers {
    
        public static void main(String[] args) {
            new PrintNumbers();
        }
    
        public PrintNumbers() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new PrintPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class PrintPane extends JPanel {
    
            private int printX;
            private int printY;
    
            public PrintPane() {
                Timer timer = new Timer(500, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        printX = (int) Math.round(Math.random() * 1000);
                        printY = (int) Math.round(Math.random() * 1000);
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int width = getWidth() - 1;
                int height = getHeight() - 1;
                FontMetrics fm = g.getFontMetrics();
                String text = printX + "x" + printY;
                int x = (width - fm.stringWidth(text)) / 2;
                int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
                g.drawString(text, x, y);
            }
    
        }
    
    }
    

    UPDATED

    From the example code.

    First, the list of things that are wrong with your sample code…

    • There is no reason to extend from JFrame or override the paint method. You would better off extending from a JPanel and overriding paintComponent. Firstly this will provide you with automatic double buffering, secondly, it will provide you with the flexible deployment options (add the panel to a JFrame or JApplet)
    • Your thread management needs more work. Running threads in a while loop that do nothing (while go is false) is wasting CPU as these threads still need to be scheduled to run. Have a read through Concurrency, in particular Synchronization and locks
    • Use the key bindings API over KeyListener. It has better focus control (the component doesn’t have to have focus in order to trigger the actions) and produces pluggable and reusable code.
    • Make use of inner classes for you event listeners. It will relieve you of the need to expose event handler methods via the main class when you really don’t want to
    • You should avoid using empty exception blocks where practical. catch (Exception e) {} isn’t going to tell you anything if something goes wrong. At the very least you should be logging the exception.

    As to your problem..

    Start by making CoOrdCounter#go, Counter#go, Slider#go and Cow volatile.

    I got away with CoOrdCounter#go and was able to see the results printed to the console correctly (you graphics didn’t work on my machine for reasons stated above)

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

Sidebar

Related Questions

public class LineNum1 extends Thread{ public synchronized void run() { try { Hello.main(null); System.out.println(Stack
Is this thread safe? private static bool close_thread_running = false; public static void StartBrowserCleaning()
I'm using an approach for this thread in order to make gallery change one
In this SO thread, Brian Postow suggested a solution involving fake anonymous functions: make
I'm learning thread synchronization and this is the demo to show how to lock
This is the error message I get when I run the show DB innobd
I am using preg_match_all to pull phone numbers from a thread. This puts them
Today I stumbled upon this thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560 The question is basically how to make
This thread didn't helped me. If I use $class_vars = get_class_vars(get_class($this)); foreach ($class_vars as
Following this thread, I tried to upload a file on the server. If I

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.