logo
down
shadow

Why am I getting a thread exception in my graphics program?


Why am I getting a thread exception in my graphics program?

By : user2956428
Date : November 22 2020, 10:56 AM
like below fixes the issue Before you "start" the game, make sure that the window has been properly realised (attached to a native peer) and sized.
You should also make sure that the UI is created within the context of the Event Dispatching Thread
code :
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Java Sucks!");
            frame.setResizable(false);
            frame.add(game);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setFocusable(true);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowOpened(WindowEvent e) {
                    game.start();
                }
            });

        }
    });
}
@Override
public Dimension getPreferredSize() {
    return new Dimension(600, 500);
}

public void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.BLACK);
    //Background///////////////
    g2d.fillRect(0, 0, getWidth(),getHeight());
    ///////////////////////////
    pris.render(g);
    g.dispose();
    bs.show();

}
public class MyAwesomeGame {

    public static void main() {
        new MyAwesomeGame()
    }

    public MyAwesomeGame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Window game = new Window();
                JFrame frame = new JFrame("Java Sucks!");
                frame.setResizable(false);
                frame.add(game);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setFocusable(true);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                frame.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowOpened(WindowEvent e) {
                            game.start();
                    }
                });

            }
        });
    }

}


Share : facebook icon twitter icon
Why do Graphics operations in background thread block Graphics operations in main UI thread?

Why do Graphics operations in background thread block Graphics operations in main UI thread?


By : user2545115
Date : March 29 2020, 07:55 AM
I wish did fix the issue. It turns out that the answer is to use multiple processes to handle background GDI+ tasks. If you run the above code under the concurrency profiler in VS2010, you'll see the foreground thread blocking on a critical section secured by the DrawImage() call in the background thread.
This thread also discusses this issue and points out that since it's using a critical section, the locks will be per-process and the background tasks can be parallelized using multiple processes instead of threads:
SWT Error: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/graphics/Device

SWT Error: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/graphics/Device


By : User1122
Date : March 29 2020, 07:55 AM
will be helpful for those in need Export your java code to a jar file e.g. hudmaker.jar then put your jar and the swt.jar in a directory. The swt.jar library can be downloaded from eclipse [1]. Open a command line and change to that directory.
Then start your code like this
code :
java.exe -Djava.library.path="." -cp "hudmaker.jar;swt.jar" db_hud.HUD_Maker
Exception in thread. I am not able to execute this program

Exception in thread. I am not able to execute this program


By : Naina
Date : March 29 2020, 07:55 AM
Hope this helps You need to pass at least two parameters to your java program.
For example:
code :
java sum 10 45
Exit program when catch exception from thread

Exit program when catch exception from thread


By : Umair Zaidi
Date : March 29 2020, 07:55 AM
Hope this helps Exception should be caught at the for loop instead of workerThread.run(), since run() will exit immediately.
code :
#include <thread>
#include <iostream>
#include <stdexcept>

static std::exception_ptr _exceptionPtr = nullptr;

struct WorkerThread
{
    std::thread thread;

    void doSomething()
    {
        int seconds = 0;
        bool shouldStop = false;

        while (shouldStop == false)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            std::cout << "time passed : " << ++seconds << "seconds" << std::endl;

            if (seconds == 10) // something bad happened 10 seconds later
            {
                try
                {
                    shouldStop = true;
                    throw std::runtime_error("something really bad happened");
                }
                catch (const std::runtime_error &ex)
                {
                    _exceptionPtr = std::current_exception();
                }
            }
        }
    }

    void run()
    {
        thread = std::thread([this] { doSomething(); });
        thread.detach();
    }
};

int main(int argc, char *argv[])
{
    WorkerThread workerThread;

    workerThread.run();



    for (;;)
    {
        // do A
        // do B
        // do C
        // do ...
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        std::cout << "waiting for thread..." << std::endl;

        if (_exceptionPtr)
        {
            try
            {
                std::rethrow_exception(_exceptionPtr);
            }
            catch (std::runtime_error &ex)
            {
                // terminates program if exception happens
                std::cout << "Program will now exit" << std::endl;
                exit(EXIT_FAILURE); // but program never executes till here
            }
        }
    }

    return 0;
}
Exception in thread "main" java.lang.NoClassDefFoundError: program (wrong name: program/Program)

Exception in thread "main" java.lang.NoClassDefFoundError: program (wrong name: program/Program)


By : Gil Raev
Date : March 29 2020, 07:55 AM
it should still fix some issue I wrote a program on my mac and compiled it via terminal: , Try using this.
Related Posts Related Posts :
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • Java POI Excel creating new column and new rows
  • NullPointerException when managing bean with Spring+JSF(Primefaces)
  • Content autocomplete in spring tool suite 4
  • Super Noob Help ~ MouseEvent/Frame
  • Making one project with dependencies stand-alone in Eclipse (Java)
  • Jsoup- getting certain attributes from website
  • MongoDB, how to make a link between collections with JAVA code
  • Overriding an abstract method means class abstract class or not in java?
  • Bukkit teleport - nullPointerException
  • How to check if a Float variable is negative in java
  • Servlet: Cannot forward after response has been committed
  • sql query to select a specific cell in excel
  • How to make new picture when mouse dragged in java
  • Use Constants interface in GWT Project
  • Setting size of a JPanel in GroupLayout
  • Construct a map from two equal size arrays
  • Implementing Read-Write Locks with Double-Checked Locking
  • Disable NO_BUTTON from JOptionPane Java if a condition is true
  • UDP multiple socket
  • Android Threading: This Handler class should be static or leaks might occur
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org