-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Break the statement up into single steps.
setBounds() is a void method. You can not add(void) to a container.
Ok I got it but... I need to make the layout translucent... I changed my code a bit :
I Created objects of class ball and pad I don't really know if it's a good idea tho!
Code :
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class arkanoidGame extends JFrame {
FlowLayout layout = new FlowLayout();
ball theBall = new ball();
pad thePad = new pad();
JButton startButton = new JButton("Start Game");
private static final long serialVersionUID = 1L;
public arkanoidGame(String name) {
super(name);
}
public void addComponentsToPane(final Container pane) {
final JPanel panel = new JPanel();
JPanel controls = new JPanel();
controls.setLayout(new FlowLayout());
panel.setLayout(null);
controls.add(startButton);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.validate();
panel.repaint();
}
});
//pane.add(controls,BorderLayout.SOUTH);
pane.add(theBall);
theBall.setBounds(0,0,800,600);
pane.add(thePad);
thePad.setBounds(0,500,800,100);
}
private static void createAndShowGui() {
arkanoidGame frame = new arkanoidGame("Arkanoid Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(800,600);
frame.setBounds(0,0,800,600);
frame.addComponentsToPane(frame.getContentPane());
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGui();
}
});
}
}
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
I Created objects of class ball and pad I don't really know if it's a good idea
Yes that's good.
The question is: should they extend a component or should they not extend any component and use the play area's paintComponent() method call their drawing methods so they can all draw on the same surface.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
They should the play area's paintComponent() method call their drawing methods so they can all draw on the same surface.
Yes all draw on the same surface
-
Re: Mutiple classfile, JPanel nothing shows up
That will remove the problems with setBounds().
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
That will remove the problems with setBounds().
I need to set the background Translucent.. And I have no method in theBall object to do it.
-
Re: Mutiple classfile, JPanel nothing shows up
Is that for a component? Or for a shape that you are drawing?
What is beneath it that you want to see?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Is that for a component? Or for a shape that you are drawing?
What is beneath it that you want to see?
Well theBall has to go anywhere on the screen so the bounds are 0,0,800,600 which takes all the screen so I can't see the thePad
-
Re: Mutiple classfile, JPanel nothing shows up
You missed the point of post#52.
The playing surface is the whole screen on which are drawn the ball and pad. There is one component: the playing surface. The ball and pad are not components added to a container. The have drawMe methods that are called by the playing surface's paintComponent() method.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
You missed the point of post#52.
The playing surface is the whole screen on which are drawn the ball and pad. There is one component: the playing surface. The ball and pad are not components added to a container. The have drawMe methods that are called by the playing surface's paintComponent() method.
I understand this... The playing surface is the JPanel or the FlowLayout?
even if I do something like this :
panel.setBackground(Color.TRANSLUCENT) or
layout.setBackground(Color.TRANSLUCENT)
None of those are Component so I get an error.
-
Re: Mutiple classfile, JPanel nothing shows up
Please post the full text of the error messages.
Why do you need a translucent component?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Please post the full text of the error messages.
Why do you need a translucent component?
Well you right I don't need a translucent component. I got my two object showing right now I changed the bound for theBall.setBounds(0,0,200,200) and the pad showed up because it was underneath it.
Now If I want movement for them... If I want the pad to move
-
Re: Mutiple classfile, JPanel nothing shows up
The ball (and pad) class should not extend a component and should not cover anything.
If you want a shape (pad) to move, change the x,y vaues for the location where it is being drawn.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
The ball (and pad) class should not extend a component and should not cover anything.
So why if the bounds of the ball is the full screen it hides the pad?
-
Re: Mutiple classfile, JPanel nothing shows up
The ball object (and pad) is not a component. It does not hide anything. The ball and pad are represented by shapes drawn on the playing surface.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
The ball (and pad) class should not extend a component and should not cover anything.
If you want a shape (pad) to move, change the x,y vaues for the location where it is being drawn.
Well in my class pad some methods are in there to make it move but they are not being called
-
Re: Mutiple classfile, JPanel nothing shows up
If you don't call them, what are they for?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
If you don't call them, what are they for?
I created a new method in arkanoidGame
public void actionPerformed(ActionEvent e) {
thePad.actionPerformed(e);
System.out.println("The Pad is being Called");
}
doesn't print anything :mad:
-
Re: Mutiple classfile, JPanel nothing shows up
Sorry, I have no idea what you are trying to do or how those few lines of code you posted fit with the rest of the program.
-
Re: Mutiple classfile, JPanel nothing shows up
arkanoidGame.java
Code :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class arkanoidGame extends JFrame implements ActionListener {
FlowLayout layout = new FlowLayout();
ball theBall = new ball();
pad thePad = new pad();
private static final long serialVersionUID = 1L;
public arkanoidGame(String name) {
super(name);
}
public void addComponentsToPane(final Container pane) {
final JPanel panel = new JPanel();
panel.setLayout(null);
pane.add(theBall);
theBall.setBounds(50,50,75,75);
pane.add(thePad);
}
public void actionPerformed(ActionEvent e) {
thePad.actionPerformed(e);
System.out.println("The Pad is being Called");
}
private static void createAndShowGui() {
arkanoidGame frame = new arkanoidGame("Arkanoid Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(800,600);
frame.setBounds(0,0,800,600);
frame.addComponentsToPane(frame.getContentPane());
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGui();
}
});
}
}
pad.java
Code :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import javax.swing.Timer;
public class pad extends JPanel implements KeyListener, ActionListener {
Timer t = new Timer(5, this);
double rectX = 15;
double rectY = 535;
int rectWidth = 80;
int rectHeight = 20;
double rectVeloX = 0.0;
double rectVeloY = 0.0;
private static final long serialVersionUID = 1L;
public void paint(Graphics draw2D){
super.paint(draw2D);
Graphics2D draw = (Graphics2D) draw2D;
draw.setColor(Color.red);
draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
}
public void actionPerformed (ActionEvent e) {
repaint();
rectX += rectVeloX;
rectY += rectVeloY;
}
public void left() {
if (rectX <= 0)
{
rectVeloX = 0;
rectX = 0;
}
else {
rectVeloX = -1.5;
rectVeloY = 0;
}
}
public void right() {
if ((rectX+rectWidth) >= 800)
{
rectVeloX = 0;
rectX = 800-rectWidth;
}
else {
rectVeloX = 1.5;
rectVeloY = 0;
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT ) {
right();
} if (key == KeyEvent.VK_LEFT ) {
left();
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
rectVeloX = 0;
rectVeloY = 0;
}
if (key == KeyEvent.VK_LEFT) {
rectVeloX = 0;
rectVeloY = 0;
}
}
public void keyTyped(KeyEvent e) {}
}
ball.java
Code :
import javax.swing.*;
import java.awt.*;
public class ball extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
int ballX = 50;
int ballY = 50;
int ballWH = 25;
public void init() {
setBackground(Color.BLUE);
}
public void start() {
// define a new thread
Thread th = new Thread (this);
// start this thread
th.start ();
}
public void stop() { }
public void destroy() { }
public void paint (Graphics g) {
super.paint(g);
g.setColor(Color.blue);
g.fillOval(ballX,ballY,ballWH,ballWH);
System.out.println("ball bounds = " + getBounds());
}
public void run() {
// lower ThreadPriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while (true) this means in our case "always"
while (true)
{
ballX++;
ballY++;
repaint();
try
{
// Stop thread for 20 milliseconds
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
}
-
Re: Mutiple classfile, JPanel nothing shows up
Can you explain what the code in post#67 is supposed to do?
What problems are you trying to solve now?
I see that ball and pad still extend JPanel. That was supposed to have been removed.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Can you explain what the code in post#67 is supposed to do?
Well I was trying to move the pad somehow
Quote:
What problems are you trying to solve now?
I see that ball and pad still extend JPanel. That was supposed to have been removed.
I removed the JPanel and now a loads of error popup
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
The method add(Component) in the type Container is not applicable for the arguments (ball)
The method setBounds(int, int, int, int) is undefined for the type ball
The method add(Component) in the type Container is not applicable for the arguments (pad)
-
Re: Mutiple classfile, JPanel nothing shows up
Those two classes are not components now so you need to remove all the component class's methods and usages.