Reputation: 178
I am trying to write a simple game. I am in the beginning of everything and my goal is to create a box that listens to my keyboard. While trying to just draw a box in a JPanel, the JFrame launches and then closes in 3 seconds. Can someone please help? I am lost in this Java Swing JFrame, JPanel situation.
package abbygail;
public class Abbygail {
public static void main(String[] args) {
GUI gui = new GUI();
}
}
Different class:
package abbygail;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame {
public GUI(){
setSize(640, 480);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Game_Board brd = new Game_Board();
setContentPane(brd);
getContentPane().setLayout(null);
}
}
Different class:
package abbygail;
import javax.swing.JPanel;
public class Game_Board extends JPanel{
public Game_Board(){
setSize(640, 480);
Blue_Box blbx = new Blue_Box();
add(blbx);
}
}
Different class:
package abbygail;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class Blue_Box extends JPanel{
/**
*
* @param g
*/
private Point p1 = new Point(100, 100);
private Point p2 = new Point(540, 380);
public Blue_Box(){
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(8,BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g2d.draw(new Rectangle2D.Double(p1.x, p1.y, 40, 30));
}
}
Upvotes: 0
Views: 90
Reputation: 285405
You never call setVisible(true);
on your GUI instance.
So:
public class Abbygail {
public static void main(String[] args) {
GUI gui = new GUI();
gui.setVisible(true);
}
}
Also, a small side issue regarding your use of null layouts, while null layouts and setBounds()
might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Regarding your blue box -- your Blue_Box JPanel is sized at 0, 0. Give it a getPreferredSize method and return a decent size:
public class Blue_Box extends JPanel {
private static final int PREF_W = 640;
private static final int PREF_H = 480;
private Point p1 = new Point(100, 100);
private Point p2 = new Point(540, 380);
public Blue_Box() {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g2d.draw(new Rectangle2D.Double(p1.x, p1.y, 40, 30));
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
and get rid of null layouts
Upvotes: 6