lundi 13 février 2017

Graphics cannot be showed

Vote count: 0

Hy! I'm trying to make something as a test. At first I made the basic things (frame, graphics, textures). When I ran the aplication it showed just an empty window. At first I thought it was from my texture class. But when I tried to set the color black it also didn't worked. Here's my code:

package Main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import Audio.MusicPlayer;
import MultiThreading.ThreadPool;
import Textures.Textures;

public class Main extends Canvas implements Runnable{

private static final long serialVersionUID = 1L;

private boolean running = true;
private Thread thread;
public static Main test = new Main();
public static final int height = 842 ;
public static final int width = 595;
public static final int scale = 1;

private Textures background;

public void init(){
    background = new Textures("Test paper");
    System.out.println("Initiating...");
}

public void run() {
    init();
    System.out.println("Starting...\nReady!");
    running = true; 
    requestFocus();
    while (running) {
        //System.out.println("Running...");
        render();
         try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    stop();
}

public void render(){
    BufferStrategy bs = getBufferStrategy();
    if(bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();


    g.dispose();
    bs.show();

    g.setColor(new Color(0, 11, 54));
    g.fillRect(0, 0, width, height);
    background.render(g, 100, 100);
}

private synchronized void stop() {
    if (!running) return; 
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.exit(1); 
}

public static void main (String args[])
{

    ThreadPool pool = new ThreadPool(2);
    JFrame frame = new JFrame("Stilul gotic si stilul renascentist");

    frame.add(test);
    frame.setSize(width / scale, height / scale);
    frame.setFocusable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.pack();
    MusicPlayer audio = new MusicPlayer("J");
    pool.runTask(test); 
    pool.runTask(audio);


    pool.join();
}

}

I checked the multithreading, so it couldn't be from there (the audio file is running and I used same classes for a game). Also, in the console isn't any error... Can anyone help me?

asked 30 secs ago

Let's block ads! (Why?)



Graphics cannot be showed

Aucun commentaire:

Enregistrer un commentaire