I'm a Computer Science student who uses a mid-2009 MacBook Pro running OS X Yosemite 10.10.3. I recently had a class activity in my Object-Oriented programming class where we went step-by-step in creating an interactive Java program in which the user simply clicks a football and watch it get kicked over the goalpost with a green background.
However, even though my Java code matched the code of my classmates' Windows computers with no syntax errors, there are some problems with my program properly running while theirs work perfectly fine:
While the window for the application opens with the title and green background, neither the football nor goalpost is displayed. However, if I manually stretch the window, they show back up. I've tried changing the window dimensions to see if that was causing it to no avail.
When the football is clicked, instead of moving towards the goalpost as intended, both the football and goalpost vanish and don't return. Only the green background is displayed, even when I try manually stretching the window again.
I still submitted the code to my instructor, which worked fine on his computer (he doesn't understand the problem either since he doesn't use OS X). I tried to run the code on two other IDE's to see if Eclipse was the problem, but they all produced the same results. If this is an OS X or computer-exclusive problem, how am I able to get around this?
This is my current code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Football extends JFrame {
final int WINDOW_WIDTH = 800;
final int WINDOW_HEIGHT = 400;
private int x = 40; // Ball's X coordinate
private int y = 300; // Ball's Y coordinate
private final int WIDTH = 35; // Ball's width
private final int HEIGHT = 60; // Ball's height
private final int X_MOVE = 14; // Pixels to move ball
private final int Y_MOVE = 4;
private final int TIME_DELAY = 25; // Time delay
private Timer timer; // Timer object
/**
init method
*/
public Football() {
setTitle("Football");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// Set Background to a Dark Green
getContentPane().setBackground(new Color(0, 220, 50));
// initTimer();
addMouseListener(new FbMouseListener());
}
public void paint(Graphics g)
{
// Call the superclass paint method.
super.paint(g);
// Set the color to Brown
g.setColor(new Color(129, 74, 25));
// Draw the football
g.fillOval(x, y, WIDTH, HEIGHT);
// Draw the Goalpost
g.setColor(Color.YELLOW);
g.fillRect(670, 240, 5, 140);
g.fillRect(610, 80, 5, 140);
g.fillRect(740, 120, 5, 140);
// Need Thicker line
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(5));
g2.drawLine(612, 220, 742, 260);
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
// Update the ball's position
y -= Y_MOVE;
x += X_MOVE;
// Force a call to the paint method
repaint();
}
}
public void initTimer()
{
timer = new Timer(TIME_DELAY, new TimerListener());
timer.start();
}
private class FbMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
if (e.getX() >= x && e.getX() <= (x + WIDTH) && e.getY() >= y && e.getY() <= (y + HEIGHT))
{
initTimer();
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public static void main(String[] args)
{
Football fb = new Football();
}
}
Any help or suggestions would be appreciated, as I would like to make sure this doesn't affect any future programs I create.
Generally, overriding paint of a top level container like JFrame is a bad idea, JFrame contains a bunch of child components that can be painted independently of the parent, which seems to be the case here
As you can see, there are (at least) 3 other components in between the frame and the user
Generally, you should create a custom class which extends from something like JPanel and override it's paintComponent and perform your custom painting there.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Football {
public static void main(String[] args) {
new Football();
}
public Football() {
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("Football");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FootballPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class FootballPane extends JPanel {
public static final int WINDOW_WIDTH = 800;
public static final int WINDOW_HEIGHT = 400;
private int x = 40; // Ball's X coordinate
private int y = 300; // Ball's Y coordinate
private static final int WIDTH = 35; // Ball's width
private static final int HEIGHT = 60; // Ball's height
private static final int X_MOVE = 14; // Pixels to move ball
private static final int Y_MOVE = 4;
private static final int TIME_DELAY = 25; // Time delay
private Timer timer; // Timer object
/**
* init method
*/
public FootballPane() {
// Set Background to a Dark Green
setBackground(new Color(0, 220, 50));
// initTimer();
addMouseListener(new FbMouseListener());
}
#Override
public Dimension getPreferredSize() {
return new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
}
#Override
protected void paintComponent(Graphics g) {
// Call the superclass paint method.
super.paintComponent(g);
// Set the color to Brown
g.setColor(new Color(129, 74, 25));
// Draw the football
g.fillOval(x, y, WIDTH, HEIGHT);
// Draw the Goalpost
g.setColor(Color.YELLOW);
g.fillRect(670, 240, 5, 140);
g.fillRect(610, 80, 5, 140);
g.fillRect(740, 120, 5, 140);
// Need Thicker line
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(5));
g2.drawLine(612, 220, 742, 260);
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Update the ball's position
y -= Y_MOVE;
x += X_MOVE;
// Force a call to the paint method
repaint();
}
}
public void initTimer() {
timer = new Timer(TIME_DELAY, new TimerListener());
timer.start();
}
private class FbMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
if (e.getX() >= x && e.getX() <= (x + WIDTH) && e.getY() >= y && e.getY() <= (y + HEIGHT)) {
initTimer();
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
}
See Painting in AWT and Swing and Performing Custom Painting for more details
Related
How do I make a sprite move in a custom JPanel?
I have looked at the similar questions and although one question is similar, it isn't addressing my problem. I have a sprite in a JPanel and I am unable to get it to move. One of the requirements I have to meet for the program is that it must begin moving when a JButton is pressed (Mouse Click). I have the code set-up in a way I believe should work, but it will spit out a long list of errors when I press the button. I'm also required to have the panel be a custom panel class.
What I need to know is this:
Methods (ha) of programming sprite movement.
Continuing to move the sprite without a trail.
Making the sprite bounce off the edges of the panel. Done (Unable to test due to no moving ball)
Here's the code I have (MainClient).
package clientPackage;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import logicPack.Logic;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ClientClass
{
Ball mSolo = new Ball();
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientClass window = new ClientClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ClientClass()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
Logic Logical;
Graphics g;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 520);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
SpriteField panel = new SpriteField();
panel.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
/* int tX = e.getX();
Logical.MoveBallX();
int tY = e.getY();
Logical.MoveBallY();
panel.repaint();*/
Logical.MoveBallX();
Logical.MoveBallY();
panel.repaint();
}
});
panel.setForeground(Color.WHITE);
panel.setBackground(Color.GRAY);
panel.setBounds(64, 92, 434, 355);
frame.getContentPane().add(panel);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Graphics2D g2 = (Graphics2D)g;
mSolo.DrawSprite(g2 , Logical.MoveBallX(), Logical.MoveBallY());
}
});
btnStart.setBounds(64, 13, 174, 60);
frame.getContentPane().add(btnStart);
}
}
And here are my other Classes (Logic)
package logicPack;
import clientPackage.Ball;
public class Logic
{
Ball mSolo;
public int MoveBallX()
{
int NewX = mSolo.xPos + 50;
return NewX;
}
public int MoveBallY()
{
int NewY = mSolo.yPos + 50;
return NewY;
}
//Motion, force, friction and collision GO HERE ONLY
}
SpriteField
package clientPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class SpriteField extends JPanel
{
Ball mSolo;
SpriteField()
{
mSolo = new Ball();
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
mSolo.DrawSprite(g2 , mSolo.xPos , mSolo.yPos);
}
}
Ball
package clientPackage;
import java.awt.Color;
import java.awt.Graphics2D;
public class Ball
{
Ball()
{
}
public int xPos = 25;
public int yPos = 25;
int diameter = 25;
public void DrawSprite(Graphics2D g2, int xPos, int yPos)
{
g2.setColor(Color.BLACK);
g2.fillOval(xPos - diameter / 2 , yPos - diameter / 2 , diameter , diameter);
}
}
If you do not understand my Java comments, you can just ignore them.
If you need more details to help me, let me know.
EDIT 1:
Andrew, the closest article I could find used arrow keys to move a sprite. The article was "Sprite not moving in JPanel". All the other articles I found either addressed JPanels without sprites, or animating a sprite. However, I need a JButton that is MouseClicked to simply start the movement, and the ball does not change shape or color. I believe I have the collision part working, but I'm unable to test it until the ball starts moving.
EDIT 2:
LuxxMiner, Thanks for the hints. I have refined my collision portion to be a little more accurate using the getHeight and getWidth methods.
EDIT 3:
MadProgrammer, Thanks...? The problem is not the painting of the ball, I cannot get the ball to move in the first place to repaint it. And the example uses arrow keys, not a mouse click or JButton.
First, take a look at Painting in AWT and Swing and Performing Custom Painting to understand how painting works in Swing.
Let's have a look at the code...
You have a Ball class, which has it's own properties, but then your DrawSprite method passes in values which override these properties?
public class Ball {
Ball() {
}
public int xPos = 25;
public int yPos = 25;
int diameter = 25;
public void DrawSprite(Graphics2D g2, int xPos, int yPos) {
g2.setColor(Color.BLACK);
g2.fillOval(xPos - diameter / 2, yPos - diameter / 2, diameter, diameter);
}
}
What's the point of that? The Ball should paint it's own current state. You should get rid of the additional parameters
public class Ball {
Ball() {
}
public int xPos = 25;
public int yPos = 25;
int diameter = 25;
public void DrawSprite(Graphics2D g2) {
g2.setColor(Color.BLACK);
g2.fillOval(xPos - diameter / 2, yPos - diameter / 2, diameter, diameter);
}
}
ClientClass, Logic and SpriteField all have their own Ball references, none of which is shared so if Logic where to update the state of it's Ball, neither ClientClass or SpriteField would actually see those changes.
In reality, only SpriteField needs an instance of Ball, as it's basically the "ball container", it has the information need to determine if the ball moves out of bounds and wants to know when the ball should be repainted, better to isolate the functionality/responsibility for the Ball to SpriteField at this time.
You also need a means to actually move the ball. While you could use other events, I'd be nice if the ball just moved itself, to this end, you can use a Swing Timer, which won't block the Event Dispatching Thread, but which notifies the registered ActionListener within the context of the EDT, making it safe to update the UI from within.
public class SpriteField extends JPanel {
private Ball mSolo;
private Timer timer;
private int xDelta, yDelta;
public SpriteField() {
mSolo = new Ball();
do {
xDelta = (int) ((Math.random() * 8) - 4);
} while (xDelta == 0);
do {
yDelta = (int) ((Math.random() * 8) - 4);
} while (yDelta == 0);
}
public void start() {
if (timer == null || !timer.isRunning()) {
timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mSolo.xPos += xDelta;
mSolo.yPos += yDelta;
if (mSolo.xPos - (mSolo.diameter / 2) < 0) {
mSolo.xPos = mSolo.diameter / 2;
xDelta *= -1;
} else if (mSolo.xPos + (mSolo.diameter / 2) > getWidth()) {
mSolo.xPos = getWidth() - (mSolo.diameter / 2);
xDelta *= -1;
}
if (mSolo.yPos - (mSolo.diameter / 2) < 0) {
mSolo.yPos = (mSolo.diameter / 2);
yDelta *= -1;
} else if (mSolo.yPos + (mSolo.diameter / 2) > getHeight()) {
mSolo.yPos = getHeight() - (mSolo.diameter / 2);
yDelta *= -1;
}
repaint();
}
});
timer.start();
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
mSolo.DrawSprite(g2);
}
}
Now, all you need to do, is when the "Start" button is clicked, call the start method
public class ClientClass {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientClass window = new ClientClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ClientClass() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
// Logic Logical;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 520);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpriteField panel = new SpriteField();
panel.setForeground(Color.WHITE);
panel.setBackground(Color.GRAY);
frame.getContentPane().add(panel);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.start();
}
});
frame.getContentPane().add(btnStart, BorderLayout.SOUTH);
}
}
I'm currently trying to make a game with a GUI that needs to paint new things on the screen on button clicks. For example:
public class GUI() extends JPanel {
public void paintComponent() {
/*
*Basic initial set up here
*/
// ***** Call method here on mouse click *****
}
public void setUpGUI() {
JFrame mainFrame = new JFrame();
GUI paintGUI = new GUI();
clickDetector click = new clickDetector();
mainFrame.addMouseListener(click);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(500, 500);
mainFrame.getContentPane().add(paintGUI);
mainFrame.setVisible(true);
}
public static void main(String args[]) {
GUI gui = new GUI();
gui.setUpGUI();
}
}
I need to implement a method in the // ***** Call method here on mouse click ***** that will paint on the new additions to the frame (in my case these are circles that represent pieces on the board) but I'm unsure of how to do this on a button click. How can I repaint the frame each time the mouse is clicked so that I can modify my game board?
----EDIT----
Here is my paintComponent code, along with the listener being used to repaint.
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Method called");
g.setColor(Color.red);
for(int y = 0; y < 6; y++) {
for(int x = 0; x < 7; x++) {
g.fillOval(x*70 + 10, y*70 + 10, 50, 50);
}
}
g.setColor(Color.BLACK);
g.fillRect(0, 430, 500, 50);
g.setColor(Color.white);
g.drawString("CONNECT FOUR", 250, 450);
g.setColor(Color.LIGHT_GRAY);
click.paintPiece(g);
}
public void mouseClicked(MouseEvent e) {
this.repaint();
}
Here is the method that paintComponent should be calling, but is not
public void paintPiece(Graphics g) {
int x = getMouseX() + 10;
int y = mover.getRow() + 10;
g.fillOval(x, y, 50, 50);
}
Just create a mouse listener:
MouseListener listen = new MouseListener()
{
void mouseClicked(MouseEvent e){}
void mouseReleased(MouseEvent e){}
void mousePressed(MouseEvent e){paintGUI.repaint();}
void mouseExited(MouseEvent e){}
void mouseEntered(MouseEvent e){}
};
paintGUI.addMouseListener(listen);
Every time you click inside of the JPanel, you should now see it repaint. Likewise, if you want to update when a JButton is pressed, just use ActionListener instead:
ActionListener listen = new ActionListener()
{
public void actionPerformed(ActionEvent e){paintGUI.repaint();}
}
button.addActionListener(listen);
This should be placed in your setUpGUI() method.
Add a Action Event listener to the button.
Oracle Docs Action Event Listener
In the actionPerformed method add whatever needs to be added then call
repaint();
This changes will let you paint a rectangle each time you click on the frame
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JPanel implements MouseListener{
private Rectangle rect;
private int width = 100;
private int height = 100;
public GUI(int x, int y, int width, int height)
{
rect = new Rectangle(x, y, width, height);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.draw(rect);
}
public void mouseClicked(MouseEvent e) {
rect = new Rectangle(e.getX(), e.getY(), width, height);
repaint();
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void setUpGUI() {
JFrame mainFrame = new JFrame();
mainFrame.addMouseListener(this);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(500, 500);
mainFrame.getContentPane().add(this);
mainFrame.setVisible(true);
}
public static void main(String args[]) {
GUI gui = new GUI(0,0,100,100);
gui.setUpGUI();
}
}
Notice the MouseListener was implemented by the GUI class and when you are trying to initialize the MouseListener for the frame, you are just putting this as parameter, which will refer to the GUI class and therefore to your JPanel
I'm using Netbeans (JDK 1.7) on Windows 8.1 to run an application that:
Uses the Graphics object to draw a frog in the paintComponent() of a 400x400 JPanel;
Adds the panel to a JFrame for displaying purposes;
This should be a very simple task, of course, but I'm having trouble understanding a specific behavior in this application.
Here is the problem: the very first time the window is displayed, the panel shows up at an offset (image below) when it shouldn't. I'm trying to understand why this is happening and how to solve it:
After one of the arrow keys are pressed on the keyboard the drawing gets displaced 5 pixels to the left/right/up/down. However, when this happens, the entire JPanel seems to be moved to the (0,0) coordinate of the window (which is what I expected from the beginning), thus moving the drawing a lot more than it should:
I painted a black border on the edges of the panel to make the problem more apparent.
Here is the Short, Self Contained, Correct (Compilable), Example:
KAfrog.java:
import javax.swing.SwingUtilities;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class KAfrog
{
public static void main( String args[] )
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
} // end of anonymous innerclass
); // end of invokeLater
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " + SwingUtilities.isEventDispatchThread());
Window janela = new Window("Khan Academy Frog");
janela.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} // end of anonymous innerclass
); // end of addWindowListener
}
}
Window.java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
class DrawingPanel extends JPanel
{
private int x = 50, y = 50;
public DrawingPanel() {
super();
setBorder(new LineBorder(Color.BLACK));
}
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void setX(int x) {
if (x < 0 || x >= 400)
return;
this.x = x;
repaint();
}
public void setY(int y) {
if (y < 0 || y >= 400)
return;
this.y = y;
repaint();
}
public int getX() { return this.x; }
public int getY() { return this.y; }
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("DrawingPanel::paintComponent: drawing at x:" + x + " y:" + y);
System.out.println("DrawingPanel::paintComponent: panel size " + getWidth() + "x" + getHeight());
// face
Color green = new Color(30, 204, 91);
g.setColor(green);
g.fillOval(x, y, 200, 100);
// mouth
g.setColor(Color.BLACK); // g.setColor(new Color(0, 0, 0));
g.fillOval(x+25, y+25, 150, 50);
// left eye
g.setColor(green);
g.fillOval(x+30, y-30, 45, 45); // background
g.setColor(Color.WHITE);
g.fillOval(x+35, y-25, 35, 35); // white eye sockets
g.setColor(Color.BLACK);
g.fillOval(x+48, y-15, 10, 10); // black eyes pretos
// right eye
g.setColor(green);
g.fillOval(x+110, y-30, 45, 45); // background
g.setColor(Color.WHITE);
g.fillOval(x+115, y-25, 35, 35); // white eye sockets
g.setColor(Color.BLACK);
g.fillOval(x+128, y-15, 10, 10); // black eyes
}
}
public class Window extends JFrame
{
DrawingPanel frog_panel;
public Window(String title)
{
super(title);
frog_panel = new DrawingPanel();
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
frog_panel.setX(frog_panel.getX()-5);
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
frog_panel.setX(frog_panel.getX()+5);
if (e.getKeyCode() == KeyEvent.VK_UP)
frog_panel.setY(frog_panel.getY()-5);
if (e.getKeyCode() == KeyEvent.VK_DOWN)
frog_panel.setY(frog_panel.getY()+5);
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
);
add(frog_panel);
pack();
setVisible(true);
System.out.println("Window::Window: size is " + getWidth() + "x" + getHeight());
}
}
Does anyone know why this happens and how to fix it?
You overrode public int getX() from javax.swing.JComponent.
It returns the current x coordinate of the component's origin. This method is preferable to writing component.getBounds().x, or component.getLocation().x because it doesn't cause any heap allocations.
Rename your methods public int getX() and public int getY().
This should fix the problem.
I'm having trouble loading a sprite into a game. I have the background done, but the sprite doesn't seem to be on the background. I want to assume you put it in the main constructor, but I don't think it will work.
package MyGamePKG;
import java.awt.BorderLayout;
public class GameJFrame extends JFrame {
private JPanel contentPane;
private int x,y,x_vel,y_vel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameJFrame frame = new GameJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GameJFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 720, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
GameJPanel Jpanel = new GameJPanel();
Jpanel.setBounds(0, 0, 720, 480);
contentPane.add(Jpanel);
Jpanel.setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(GameJFrame.class.getResource("/Resources/gamemap.png")));
lblNewLabel.setBounds(0, 0, 720, 480);
contentPane.add(lblNewLabel);
}
}
The JPanel class
package MyGamePKG;
import java.awt.BasicStroke;
public class GameJPanel extends JPanel implements ActionListener, KeyListener {
private int frameRate = 30;
private int x=0;
private int y=460;
private int x_vel, y_vel;
private Image map;
private Image character;
/**
* Create the panel.
*/
public GameJPanel() {
character = new ImageIcon(getClass().getResource("/resources/sprite-concepts.png")).getImage();
this.addKeyListener(this);
this.setFocusable(true);
Timer timer = new Timer(30, this);
timer.start();
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(rh);
this.setOpaque(false);
myDrawBoxOvalsandRect(x,y,100,g2d);
//myDrawArc(x+25,y+40,50,50,550,170,g2d);
} // paintComponent
public void myDrawBoxOvalsandRect( int x, int y, int scale, Graphics my_g)
{
my_g.setColor(Color.cyan);
my_g.fillOval(x, y, 15, 15); //face
}
public void myDrawArc(int x, int y, int height, int width, int angle1, int angle2, Graphics my_g)
{
my_g.setColor(Color.red);
my_g.drawArc(x, y, 50, 50, 550, 170); //happy face
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT)
{
x_vel = -2;
y_vel = 0;
}
if(c == KeyEvent.VK_UP)
{
x_vel = 0;
y_vel = -2;
}
if( c == KeyEvent.VK_RIGHT)
{
x_vel = 2;
y_vel = 0;
}
if( c == KeyEvent.VK_DOWN)
{
x_vel = 0;
y_vel = 2;
}
repaint();
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
x_vel = 0;
y_vel = 0;
repaint();
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(x < 0)
{
x_vel = 0;
x = 0;
}
if(x > 720)
{
x_vel = 0;
x = 720;
}
if(y < 0)
{
y_vel = 0;
y = 0;
}
if(y > 480)
{
y_vel = 0;
y = 480;
}
x = x + x_vel;
y = y + y_vel;
repaint();
}
}
I suspect a number of things...
It's possible that the z-ordering of the components is rendering the background over the foreground components
The reliance on null layouts means that it's possible the window borders are taking up more space then you are allowing for, covering up portions of the content.
Don't rely on a null layout and guess work. Each platform/Look and Feel has different requirements when it comes to the frame border (amongst other things). Instead, you should consider...
Overriding getPreferredSize to provide better sizing hints
Use an OverlayLayout manager
Use JFrame#pack to pack the frame around the content instead of using setBounds
Consider painting the entire game state using a single paintComponent method, this would mean rendering the background, middleground and foreground within GameJPanel's paintComponent.
The benefit of this, is you gain complete control over the z-ordering process.
You should consider using key bindings over using KeyListener as it does not suffer from the same focus related issues and provides you with finer control over determining the level of focus that the component will raise key events
Hi guys I'm super new to Java; I've looked around and haven't been able to find an answer to this question. Any chance you could help me?
Here is an example of what I'm trying to achieve.
public class FrameWork extends JFrame implements MouseListener {
... //Irrelevant to the question code
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
if (x==1 && y==1){
// This is where and when I want to draw GFXDice
}
}}
Now the other class, all imports left out for readability.
public class Board extends JPanel{
Image GFXDice1;
public Board() {
ImageIcon Dice1;
Dice1 = new ImageIcon(this.getClass().getResource("GFX/Dice1"));
GFXDice1 = Dice1.getImage();
}
Now the graphics part
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(GFXDice, 100, 100, null);
}
Now for the question - I want to use the method paint from the Class Board in the Class FrameWork - But can't get it to work - any ideas ? I'm offering a bazillion units of good karma to anyone who has an idea.
The general way to do most Swing drawing is via passive graphics. This means:
Do the drawing itself in the paintComponent(Graphics g) method of a JPanel or JComponent.
In your MouseListener change the state of some of the fields of the class. In your mouseClicked method you are setting the state of some local variables, and I recommend that you instead make your x and y fields, not local.
Then when the mouse listener is done making changes, call repaint() on the JPanel.
Then in the paintComponent method, use those fields that were changed in the mouse listener to do your drawing.
Don't forget to call the super's paintComponent method in your paintComponent override.
Don't forget to read tutorials on Swing Graphics to get the fine points.
Edit
For example, please have a look at a small graphics program that I created for an answer to another recent question.
The drawing occurs in the main class, SpaceShip, which extends JPanel. I add an anonymous inner MouseAdapter class for my Mouse Listener, and inside of the MouseAdapter, I call a method called moveIt, passing in the MouseEvent object.
MouseAdapter myMouseAdapter = new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
moveIt(evt);
count = count + 1;
}
#Override
public void mouseDragged(MouseEvent evt) {
moveIt(evt);
}
};
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
All moveIt(MouseEvent evt) does is to change the state of two fields, myX and myY, and then calls repaint() on the current class:
public void moveIt(MouseEvent evt) {
myY = evt.getY() - sprite.getHeight() / 2;
myX = evt.getX() - sprite.getWidth() / 2;
repaint();
}
And then in the class's paintComponent method, I first call the super's paintComponent to allow it to erase any previous old out of date images, then I paint a background image, background, then I draw a sprite that uses the myX and myY variables to tell it where to draw, then I draw some yellow rectangles at locations that are determined by the JPanel's size:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
font1 = new Font("Serif", Font.BOLD, 36);
g.drawImage(background, 0, 0, this);
g.drawImage(sprite, myX, myY, this);
g.setColor(Color.yellow);
int rectCount = 10;
int height = getHeight() / rectCount;
int width = 272;
int x = getWidth() - width;
for (int i = 0; i < rectCount; i++) {
int y = i * height;
g.drawRect(x, y, width, height);
}
g.setFont(font1);
g.drawString(Integer.toString(count), 500, 100);
}
The whole thing looks like this:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.lang.String;
import java.awt.Font;
#SuppressWarnings("serial")
public class SpaceShip extends JPanel {
private static final String BACKGROUND_PATH = "http://www.thatsreallypossible.com/"
+ "wp-content/uploads/2012/12/Space-Colonialisation.jpg";
private static final String SPRITE_PATH = "http://www.pd4pic.com/"
+ "images250_/ufo-flying-saucer-spacecraft-spaceship-alien.png";
private Font font1;
int myX = 100;
int myY = 400;
int count = 0;
private BufferedImage background;
private BufferedImage sprite;
public SpaceShip() throws IOException {
URL backgroundUrl = new URL(BACKGROUND_PATH);
URL spriteUrl = new URL(SPRITE_PATH);
background = ImageIO.read(backgroundUrl);
sprite = ImageIO.read(spriteUrl);
MouseAdapter myMouseAdapter = new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
moveIt(evt);
count = count + 1;
}
#Override
public void mouseDragged(MouseEvent evt) {
moveIt(evt);
}
};
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
#Override
public Dimension getPreferredSize() {
if (background != null) {
return new Dimension(background.getWidth(), background.getHeight());
}
return super.getPreferredSize();
}
public void moveIt(MouseEvent evt) {
myY = evt.getY() - sprite.getHeight() / 2;
myX = evt.getX() - sprite.getWidth() / 2;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
font1 = new Font("Serif", Font.BOLD, 36);
g.drawImage(background, 0, 0, this);
g.drawImage(sprite, myX, myY, this);
g.setColor(Color.yellow);
int rectCount = 10;
int height = getHeight() / rectCount;
int width = 272;
int x = getWidth() - width;
for (int i = 0; i < rectCount; i++) {
int y = i * height;
g.drawRect(x, y, width, height);
}
g.setFont(font1);
g.drawString(Integer.toString(count), 500, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Basic Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SpaceShip ex;
try {
ex = new SpaceShip();
frame.getContentPane().add(ex);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
ex.requestFocus();
} catch (IOException e) {
e.printStackTrace();
}
}
}