This is an extremely simple question, but I have no idea what to do.
package typeandscreen;
imports
public class DisplayWindow {
Font f = new Font();
void run(){
try {
Display.setDisplayMode(new DisplayMode(1000,560));
Display.setTitle("Test for Bitmaps / Letters");
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
while(!Display.isCloseRequested()){
Graphics g;
What do I make Graphics g equal? I just want to draw the subImage f.letters[0] onto the screen, but I don't know what I'm supposed to put there.
Making the lower line Graphics.drawImage(etc) doesn't work, and leaving it how it is, is a null pointer exception. If I do "Graphics g = f.letters[0].getGraphics()" for the above line then it's also a null pointer exception.
g.drawImage(f.letters[0],0,0,null);
Display.update();
}
Display.destroy();
}
public static void main(String args[]){
DisplayWindow dw = new DisplayWindow();
dw.run();
}
}
Related
first questing for me here. Been searching forever but cant seem to find the answer.
Im working on a school assignment. Got given an ui and are supposed to make the different panels in it do different things in separate threads. Anyway, Im trying to make a triangle rotate inside one of the JPanels. I have managed to draw it and somewhat rotate it, but when I try to make a loop to update it it just blinks and then disappears again. Heres the code Ive written.
StartAssignment, starts the application
public class StartAssignment1 {
public static void main(String[] args) {
Controller controller = new Controller();
}
Controller, recieves calls from the ui and executes various functions in other classes
public class Controller {
private GUIAssignment1 gui = new GUIAssignment1(this);
private RotateShape rotateShape;
private Thread t1;
public Controller() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.Start();
}
});
}
public void startT1(JPanel panel) {
rotateShape = new RotateShape(panel);
t1 = new Thread(rotateShape);
t1.start();
}
public void t1Shutdown() {
rotateShape.shutdown();
}
RotateShape, where Im trying to rotate the damned thing
public class RotateShape implements Runnable {
JPanel panel;
private volatile boolean t1Running = true;
public RotateShape(JPanel panel) {
this.panel = panel;
}
private void draw() {
Graphics2D g2 = (Graphics2D) panel.getGraphics();
g2.rotate(Math.toRadians(10));
g2.drawPolygon(new int[] {50, 100, 150}, new int[] {150, 50, 150}, 3);
}
public void shutdown() {
t1Running = false;
System.out.println("Shutdown");
}
#Override
public void run() {
while (t1Running) {
try {
draw();
Thread.sleep(500);
System.out.println("loop working");
panel.repaint();
panel.revalidate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I'm not certain because I don't have access to your GUI code and therefore cannot test it myself, but here's an idea:
It looks like you're rotating your Graphics2D object by a fixed amount on every re-render (i.e. every invocation of draw()). It's possible that the internal JPanel code initiates the Graphics2D at a default rotation before every re-render, which may be why your rotation only causes the shape to "somewhat rotate."
Maybe try storing a variable for the radian offset (e.g. double offset;) as a member field of the RotateShapeclass, incrementing it on every re-render, then calling g2.rotate(Math.toRadians(offset));?
Addendum 1:
The reason that it may draw on top of the previous render is because the Graphics2D object is not clearing the canvas between re-renders. One way to fix this is to "clear" the canvas at the beginning of the draw() method by filling a rectangle that covers the whole canvas.
Addendum 2:
When you call panel.repaint(), it triggers the paintComponent() method of the JPanel object. So, by calling repaint() and your own draw() method, you are doing two separate renders, which may cause some graphical errors. If you were able to extends the JPanel class and use that, you should define an override to replace draw():
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// TODO: add your rendering operations here
}
This way, calls to repaint() should trigger this method.
I work on a Java development software with Swing and I have a problem with my code, I want to display an image with the LoadingFrame class, its main work but when I call the constructor and the start() method in my main class, the frame opens but the image doesn't display (I have no Exception).
Why it doesn't work with my main class?
public class LoadingFrame
{
private JFrame frame;
public LoadingFrame()
{
frame = new JFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setContentPane(new Panneau());
}
public void start()
{
frame.setVisible(true);
}
public void stop()
{
frame.setVisible(false);
}
public static void main(String[] args)
{
LoadingFrame l = new LoadingFrame();
l.start();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
l.stop();
}
}
public class Panneau extends JPanel
{
public void paintComponent(Graphics g)
{
System.out.println("hello");
try
{
Image img = ImageIO.read(new File("Images/loading.png"));
//g.drawImage(img, 0, 0, this);
//Pour une image de fond
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
The App class is my main class :
public class App {
//Attributes used to display the application
private JFrame frame;
//Attribute which display a waiting frame
private static LoadingFrame loadingFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public App()
{
initialize();
synchronizeScriptReferenceList();
synchronizeTests();
}
[...]
}
I was able to get this to work from App.java. For some reason, using EventQueue isn't cutting it. I tried to use SwingUtilities as well, but that doesn't work either. Finally I tried just get rid of the Thready-stuff in App.main at just straight up running it in the main thread. For some reason, this works when the other approaches do not! Here is my code:
// In the App class:
public static void main(String[] args) {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
When I used this code, I got it to work! (for some reason unknown to me), And here's a bonus rewrite of the Panneau class:
class Panneau extends JPanel
{
Image img;
public Panneau() {
try
{
img = ImageIO.read(new File("Images/loading.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
There are two main difference with this class; problems which I addressed. Here they are:
I call super.paintComponent as the very first method in our own paintComponent
I only load the loading image once, in the constructor, and not every single time I want to draw, which moves everything along much smoother. (you don't want the loading screen to be CPU heavy, do you?)
Hopefully, with these improvements, I hope you can make your program work! It worked with me, so I wish the best of luck to you.
P.S. Don't call frame.pack(), that was a mistake on my part. For some reason, I think it doesn't work well with undecorated windows.
This is a very simple problem to some people I'm assuming and I just can't see it. (I'm very amateur at Java).
This is some test code I wrote to try and troubleshoot why it's not working in my other project. For some reason my rocketshipStationary.png just won't show up when I load the Java Applet.
This is my code:
public class Test extends Applet {
public Image offScreen;
public Graphics d;
public BufferedImage rocketship;
public void init() {
setSize(854, 480);
offScreen = createImage(854,480);
d = offScreen.getGraphics();
try {
rocketship = ImageIO.read(new File("rocketshipStationary.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
d.clearRect(0, 0, 854, 480);
d.drawImage(rocketship, 100, 100, this);
d.drawImage(offScreen, 0, 0, this);
}
}
You should be getting a nice big stack trace that describes what happens. The bottom line is that 'applets and files do not play well together'.
Instead, either establish an URL to the image and use that for ImageIO, or alternately use the URL in the Applet.getImage(URL) method.
I have a serious problem with my LWJGL program.
If I set one of my text's colour, the background's color changes, too.
I think I know why, I just put all of the drawing things in an infinite loop, and maybe that's the reason.Can you help me with this? Am I correct with the reason of the problem? If yes, how can I solve that? If not, what do you think, what is the problem?
Here's my code:
The main class:
<!-- language: lang-java -->
public class DoomMain {
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
public static final int SCALE = 1;
private static State state;
public DoomMain() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH*SCALE,HEIGHT*SCALE));
Display.setTitle("Tomco - Doom");
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
}
while(!Display.isCloseRequested()) {
state = State.MENU;
switch(state) {
case INTRO:
new Intro();
break;
case MENU:
new MainMenu();
break;
case CREDITS:
break;
case GAME:
break;
default:
break;
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) {
new DoomMain();
}
}
The MainMenu class:
<!-- language: lang-java -->
public class MainMenu {
private Texture background;
private TrueTypeFont doomfont;
private boolean ana = false;
public MainMenu() {
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL11.GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,DoomMain.WIDTH,DoomMain.HEIGHT);
glMatrixMode(GL11.GL_MODELVIEW);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(0,DoomMain.WIDTH,DoomMain.HEIGHT,0,1,-1);
glMatrixMode(GL11.GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
drawBackground();
drawText();
glDisable(GL_TEXTURE_2D);
}
private void drawBackground() {
try {
background = TextureLoader.getTexture("PNG",new FileInputStream(new File("res/textures/mainmenu/doom_00340461.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
}
glBegin(GL_QUADS);
{
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(background.getTextureWidth(),0);
glTexCoord2f(1,1);
glVertex2f(background.getTextureWidth(),background.getTextureHeight());
glTexCoord2f(0,1);
glVertex2f(0,background.getTextureHeight());
}
glEnd();
}
private void drawText() {
Color.white.bind();
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("res/fonts/DooM.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT,inputStream);
awtFont = awtFont.deriveFont(24f);
doomfont = new TrueTypeFont(awtFont,ana);
} catch(Exception e) {
e.printStackTrace();
}
doomfont.drawString(200,100,"Start new game",Color.red);
}
}
By default, the texture will be multiplied by the current color. As a state machine, the "current color" is whenever you set it last. In your case, when you draw text; when you enter the loop again, that same color is used for the texture.
To simplify the problem, you can reduce the whole drawText routine to just setting the color:
LOOP:
a) draw background
b) set color
results in: ... set color; draw background ...
The glClear function clears the buffers of what is already drawn, but not the color to be used next.
As a solution, you can explicitly change the color just before you draw the texture. glColor3f(1.0f, 1.0f, 1.0f) will produce the texture as is, and any other value will work as a color filter (just as it is now).
Another solution is to change the texture drawing behavior with glTexEnv. Assuming the texture is GL_RGBA, then calling glTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) works. The linked page contains a table with different settings and their effects.
First if all : The Color.white.bind() call in your drawText() function is probably what is affecting the background.
Here is a quick and dirty example of how to only load the data once. I've moved data loading to the loadData() function. The DoomMain class is created once after the screen is opened, loadData() is called before the main loop and you call your drawing using menu.draw().
Ideally you would load all the resources you need in one place first, then pass the objects to DoomMain at initialization. You might want to use that texture and font in other places as well.. and you don't want to load it multiple times.
public DoomMain()
{
// Open screen here since we need a context to make textures etc
...
// Make objects for your different parts
MainMenu menu = MainMenu();
menu.loadData(); // <- Loads the texture and font
while(!Display.isCloseRequested())
{
state = State.MENU;
switch(state) {
...
case MENU:
menu.draw();
break;
...
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
MainMenu
public class MainMenu
{
private Texture background;
private TrueTypeFont doomfont;
private boolean ana = false;
public MainMenu() { }
public void loadData()
{
// Load the background texture
try {
background = TextureLoader.getTexture("PNG",new FileInputStream(new File("res/textures/mainmenu/doom_00340461.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
}
// Load and initialize the font here
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("res/fonts/DooM.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT,inputStream);
awtFont = awtFont.deriveFont(24f);
doomfont = new TrueTypeFont(awtFont,ana);
} catch(Exception e) {
e.printStackTrace();
}
}
public void draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL11.GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,DoomMain.WIDTH,DoomMain.HEIGHT);
glMatrixMode(GL11.GL_MODELVIEW);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(0,DoomMain.WIDTH,DoomMain.HEIGHT,0,1,-1);
glMatrixMode(GL11.GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
drawBackground();
drawText();
glDisable(GL_TEXTURE_2D);
}
private void drawBackground()
{
... bind background texture
... draw your quad
}
private void drawText()
{
doomfont.drawString(....);
}
}
trying to get an image to print into a window. Everything runs without errors, and it also works if I replace the drawImage with another graphics class. However, the window is missing the image, and i'm not sure why. Again, the JFrame stuff and Graphics work fine with drawing other graphics, but only doesn't draw the image here. Thanks.
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class GraphicsMovement2 extends JApplet{
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
JApplet example = new GraphicsMovement2();
JFrame frame = new JFrame("Movement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(example);
frame.setSize(new Dimension(1366,768)); //Sets the dimensions of panel to appear when run
frame.setVisible(true);
}
public void paint (Graphics page){
page.drawImage(image, 100, 100, 100, 100, Color.RED, this);
}
}
You've defined image twice...
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
This essentially means that by the time you get to the paint method, it is null as you haven't initialized the instance variable.
Another problem you will have is the fact that you are trying to load the image from a static reference but the image isn't declared as static. Better to move this logic into the constructor or instance method.
Don't use JApplet as your container when you're adding to a JFrame, you're better of using something like JPanel. It will help when it comes to adding things to the container.
YOU MUST CALL super.paint(g)...in fact, DON'T override the paint method of top level containers like JFrame or JApplet. Use something like JPanel and override the paintComponent method instead. Top level containers aren't double buffered.
The paint methods does a lot of important work and it's just easier to use JComponent#paintComponent ... but don't forget to call super.paintComponent
UPDATED
You need to define image within the context it is going to be used.
Because you declared the image as an instance field of GraphicsMovement2, you will require an instance of GraphicsMovement2 in order to reference it.
However, in you main method, which is static, you also declared a variable named image.
The paint method of GraphicsMovement2 can't see the variable you declared in main, only the instance field (which is null).
In order to fix the problem, you need to move the loading of the image into the context of a instance of GraphicsMovement2, this can be best achived (in your context), but moving the image loading into the constructor of GraphicsMovement2
public GraphicsMovement2() {
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
}
The two examples below will produce the same result...
The Easy Way
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
public ImagePane() {
setLayout(new BorderLayout());
ImageIcon icon = null;
try {
icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
} catch (Exception e) {
e.printStackTrace();
}
add(new JLabel(icon));
}
}
}
The Hard Way
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage background;
public ImagePane() {
try {
background = ImageIO.read(new File("/path/to/your/image"));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
Take the time to read through the tutorials
Creating a GUI With JFC/Swing
Performing Custom Painting
Your class shouldn't extend JApplet when you're not even using applets -- this makes no sense. Instead
Have your drawing class extend JPanel
Draw in the JPanel's paintComponent method
Add this JPanel to the JFrame's contentPane.
Read the Swing painting tutorials. You can't guess at this stuff and expect it to work, and the tutorials will show you how it's done correctly.
Don't mix file deviders,
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
should be replaced with:
File file = new File("C:/Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");