New Posts  All Forums:Forum Nav:

Really confused with JFrame.

post #1 of 6
Thread Starter 
I've been looking at several places to answer my question, but I cant get a good answer. Whenever I make a JFrame, and draw to it, or even set it's Background, I always just get a gray Window with nothing in it. There must be something simple that I am doing wrong. Before, I used code from a tutorial that made a fullscreen window game. In the tutorial, they didn't use PaintComponent or Paint or anything, so I am confused with that too. What is wrong with this? It doesn't even show the background!

I am posting a simple version of my code, so make it less complicated to you.

Code:
import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.*;

public class Window extends JFrame {

private static final long serialVersionUID = 1L;

boolean running = false;
int x = 10;
int y = 10;
int r = 100;

public static void main(String args[]){
new Window();
}

public Window(){
JFrame f = new JFrame("Game!!!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,768);
f.setResizable(false);
f.setBackground(Color.GREEN);
f.setVisible(true);

running = true;
run();
}

public void run(){
while(running){
repaint();
x++;
}
try{
Thread.sleep(20);
}catch(Exception e){}
}

public void paint(Graphics g){
g.setColor(Color.RED);
g.fillOval(x-r, y-r,2*r,2*r);
}
}
post #2 of 6
you need to use a container. use this instead of setting the background color in the frame directly.

Code:
Container content = f.getContentPane();
content.setBackground(Color.green);
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by travesty View Post
you need to use a container. use this instead of setting the background color in the frame directly.

Code:
Container content = f.getContentPane();
content.setBackground(Color.green);
But what about the oval. Why isn't that showing?
post #4 of 6
i'm guessing that the program is supposed to incrementally draw larger ovals or something? anyway, i changed the code around a bit. the way it was, paint() was overriding the paint method for window. the other reason the oval wasn't showing up was the coordinates defined by x-r and y-r were off the screen. i put in some print statements to show what happens to those values. right now the modified code draws 1 big oval in the middle of the frame. i moved the run() method inside of mycomponent. think of mycomponent like a canvas that you can draw things on. you don't want to draw directly on the frame or window.

Code:
import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.*;

public class Window extends JFrame {

private static final long serialVersionUID = 1L;

boolean running = false;
int x = 10;
int y = 10;
int r = 100;

public static void main(String args[]){
new Window();
}

public Window(){
JFrame f = new JFrame("Game!!!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,768);
f.setResizable(false);
Container content = f.getContentPane();
content.add(new MyComponent());
content.setBackground(Color.green);
    f.setVisible(true);

running = true;
((MyComponent) content.getComponent(0)).run();
}


public class MyComponent extends JComponent{
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void run(){
while(running){
repaint();
x++;
}
try{
Thread.sleep(20);
}catch(Exception e){}
}

public void paintComponent(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(Color.RED);
    g.fillOval(0, 0, width, height);
    //g.fillOval(x-r, y-r, 2*r, 2*r);
    System.out.println(x-r);
    System.out.println(y-r);
    System.out.println(2*r);
    System.out.println(2*r);
  }
}
}
you're using thread.sleep and a run method. did you do this on purpose? if you want to use threads properly, you should either implement runnable interface, or extend thread.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 6
Thread Starter 
My goal was to make a red circle go across the screen. The problem was that whenever I incremented x in the While loop, the oval didn't show up, but when I incremented it in the PaintComponent, it did show up. That was the problem. It refused to show the oval if I changed x in any other method than paintComponent. What makes that happen?
post #6 of 6
Quote:
Originally Posted by Jtvd78 View Post
The problem was that whenever I incremented x in the While loop, the oval didn't show up, but when I incremented it in the PaintComponent, it did show up. That was the problem. It refused to show the oval if I changed x in any other method than paintComponent. What makes that happen?
x was incrementing in a while(true) loop. i believe it was incrementing so fast that the coordinate went off the screen before it was able to paint it. the system.println confirms that x gets into the millions by the time paintComponent executes. also, the thread.sleep will work for this application. sorry for any confusion.

the code below successfully moves the red circle across the screen. the trick was to increment x and call thread.sleep in a for loop. the for loop will constrain the x coordinate better than a while(true) loop.

Code:
import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.*;

public class Window extends JFrame {

private static final long serialVersionUID = 1L;

boolean running = false;
int x = 0;

public static void main(String args[]){
new Window();
}

public Window(){
JFrame f = new JFrame("Game!!!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,768);
f.setResizable(false);
Container content = f.getContentPane();
content.add(new MyComponent());
content.setBackground(Color.green);
    f.setVisible(true);

running = true;
((MyComponent) content.getComponent(0)).run();
}


public class MyComponent extends JComponent{
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void run(){
    int width = getWidth();
    for(int i=0; i<width; i++){
try{
    x++;
    Thread.sleep(20);
}
                        catch(Exception e){
    e.printStackTrace();
}
    }
}

public void paintComponent(Graphics g) {
    int height = getHeight();
    g.setColor(Color.RED);
    g.fillOval(x, height/2, 80, 80);
    repaint();
  }
}
}

Edited by travesty - 4/13/11 at 7:39pm
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming