Choosing the jpanel to draw on?
I'm trying to draw on the JPanel panel, but for some reason nothing shows up when I run the code. Is it because I have to set the panel on which to draw? If so, can someone please tell me how to do this, or just tell me what the problem is lol. Thanks
(Btw I do have a main method that makes a new PongDrawer and runs the display() method)
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class PongDrawer extends JPanel implements ActionListener, KeyListener {
JFrame table;
JLabel score;
JPanel panel;
JPanel scorePanel;
Timer ballTimer;
int ballSpeed;
public PongDrawer(){
table = new JFrame("Pong");
panel = new JPanel();
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setContentPane(panel);
scorePanel = new JPanel();
scorePanel.setSize(700,50);
table.add(scorePanel, BorderLayout.NORTH);
panel.setBackground(Color.GRAY);
score = new JLabel("score Test");
scorePanel.add(score, BorderLayout.CENTER);
ballSpeed = 500;
ballTimer = new Timer(500, this);
addKeyListener(this);
}
@SuppressWarnings("deprecation")
public void display(){
table.pack();
table.resize(700,500);
table.setVisible(true);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, 100, 700, 100);
}
public void actionPerformed(ActionEvent e){
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
//does the corresponding action for each key pressed
switch(keyCode){
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_W:
break;
case KeyEvent.VK_S:
break;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
Re: Choosing the jpanel to draw on?
How does anyone test this code? You need a main() method and a JFrame that compiles, executes and shows the problem.
Re: Choosing the jpanel to draw on?
Lol sorry I should have said that I already have all that stuff (I thought it would be implied lol)
Re: Choosing the jpanel to draw on?
Have you solved the problem now?
If you want anyone to help you with the problem, you need to provide: main() method and a JFrame that compiles, executes and shows the problem.
Re: Choosing the jpanel to draw on?
No as in I had that before you told me lol. I still have the problem, which is that it's not drawing on the JPanel panel for some reason.
Re: Choosing the jpanel to draw on?
Ok , if you don't want to provide code for testing, then good luck with it.
Re: Choosing the jpanel to draw on?
This is my main class
Code :
public class PongMain {
public static void main(String[] args) {
PongDrawer game = new PongDrawer();
game.display();
}
}
Re: Choosing the jpanel to draw on?
Where do you add the the JPanel with the paintComponet method to the JFrame?
What is the size of the JPanel when it is added to the JFrame?
Add a println to the paintComponent method to show the size of the component and its location.
Re: Choosing the jpanel to draw on?
Well what I'm really asking is how I would add the paintComponent to the JPanel panel. Would I have to make the JFrame and JPanel and such in the main class to do that?
Re: Choosing the jpanel to draw on?
You have added a paintComponent method to the class.
Re: Choosing the jpanel to draw on?
Yea but I'm saying how would I specify so that it will draw on panel. I think I remember something about using the getGraphics() method, but I don't remember where to put it. Lol sorry for my noobishness.
Re: Choosing the jpanel to draw on?
Your code draws on the panel.
However the panel is not visible on the JFrame. Where do you add it?
Re: Choosing the jpanel to draw on?
Ok I just added panel to the JFrame before I made it the content pane but it's still not drawing anything. I also did table.getContentPane().add(panel); instead of table.setContentPane(panel);
Re: Choosing the jpanel to draw on?
Please post the new code.
Re: Choosing the jpanel to draw on?
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class PongDrawer extends JPanel implements ActionListener, KeyListener {
JFrame table;
JLabel scoreLabel;
JPanel panel;
JPanel scorePanel;
Timer ballTimer;
int ballSpeed;
int score;
public PongDrawer(){
table = new JFrame("Pong");
panel = new JPanel();
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.add(panel);
table.getContentPane().add(panel);
scorePanel = new JPanel();
scorePanel.setSize(700,50);
table.add(scorePanel, BorderLayout.NORTH);
panel.setBackground(Color.GRAY);
scorePanel.setBackground(Color.GRAY);
scoreLabel = new JLabel("score Test");
scorePanel.add(scoreLabel, BorderLayout.CENTER);
ballSpeed = 500;
ballTimer = new Timer(500, this);
addKeyListener(this);
}
@SuppressWarnings("deprecation")
public void display(){
table.pack();
table.resize(700,500);
table.setVisible(true);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, 100, 700, 100);
g.setColor(Color.RED);
g.drawRect(100, 100, 50, 50);
}
public void actionPerformed(ActionEvent e){
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
//does the corresponding action for each key pressed
switch(keyCode){
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_W:
break;
case KeyEvent.VK_S:
break;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
I know that the panel is definitely visible because I can see it when I run the program. It's just that nothing is drawing on it.
Re: Choosing the jpanel to draw on?
Where do you add an instance of the PongDrawer class to the JFrame?
Re: Choosing the jpanel to draw on?
Yea I figured that was my problem. I just wanted to make the whole thing in one class, but yea I guess that's not possible. Thanks for the help! :cool:
Re: Choosing the jpanel to draw on?
Quote:
I just wanted to make the whole thing in one class
It is possible. That is how I have it now.
Remember the this variable.