public static void border (Graphics g)
{
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 0);
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 352);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 0, y);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 365, y);
}
public static void box (Graphics g, int x, int y)
{
g.setColor (new Color (117, 64, 17));
g.fillRect (10 + x, 50 + y, 25, 15);
}
public static void main (String args[]) throws IOException
{
MAZE application = new MAZE ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
i tried frame.getContentPane ().add (c); but i still had the same output.
Sponsor Sponsor
rizzix
Posted: Wed May 31, 2006 3:22 pm Post subject: (No subject)
delete the "extends JFrame"
jin
Posted: Wed May 31, 2006 4:08 pm Post subject: (No subject)
By removing the extends JFrame i get errors on these lines:
super ("MAZE");
No applicable overload was found for a constructor of type "java.lang.Object". Perhaps you wanted the overloaded version "Object[];" instead?
setSize (403, 420);
No method named "setSize" was found in type "MAZE".
show ();
No method named "show" was found in type "MAZE".
super.paint (g);
No method named "paint" was found in type "java.lang.Object".
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
No method named "setDefaultCloseOperation" was found in type "MAZE".
Any ideas on what i should do now.
rizzix
Posted: Wed May 31, 2006 5:01 pm Post subject: (No subject)
Oh right... well in that case I don;t understand why you are creating a second frame (note how you have a "JFrame frame = new JFrame();") instead of adding the components to "this" frame.
Actually I believe you don't know what you are doing. May I suggest you learn Java first, and then attempt to try Swing?
wtd
Posted: Wed May 31, 2006 5:11 pm Post subject: (No subject)
He's right. If you don't truly understand what "extends" is about, then you have some more fundamental Java learning to do.
jin
Posted: Wed May 31, 2006 6:55 pm Post subject: (No subject)
Sorry i dont have a good teacher. and this is my original code. but for some reason the menu doesnt show until i change the frame dimension a bit and when i click on the "File" tab the menuitems appear but are behind the graphics (border in this case) until i pass over them. so i thought to make a new frame and then add the graphics to it. if u have any ideas on how to fix this code. plz let me know. thnx
public class MAZE extends JFrame
{
public MAZE ()
{
super ("MAZE");
setSize (403, 430);
show ();
Container c = new Container ();
c.setLayout (new BorderLayout ());
JMenuBar menubar;
JMenu file, help;
JMenuItem newI, close, helpI, about;
newI = new JMenuItem ("New");
close = new JMenuItem ("Exit");
helpI = new JMenuItem ("Help");
about = new JMenuItem ("About");
file = new JMenu ("File");
help = new JMenu ("Help");
menubar = new JMenuBar ();
file.add (newI);
file.addSeparator ();
file.add (close);
help.add (helpI);
help.add (about);
menubar.add (file);
menubar.add (help);
setJMenuBar (menubar);
}
public static void border (Graphics g)
{
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 0);
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 352);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 0, y);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 365, y);
}
public static void box (Graphics g, int x, int y)
{
g.setColor (new Color (117, 64, 17));
g.fillRect (10 + x, 60 + y, 25, 15);
}
public static void main (String args[]) throws IOException
{
MAZE application = new MAZE ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
HellblazerX
Posted: Wed May 31, 2006 7:57 pm Post subject: (No subject)
I think you should keep the graphics portion and the JFrame separate, by placing the graphics inside a JPanel, and then adding the JPanel to the JFrame.
rizzix
Posted: Wed May 31, 2006 8:13 pm Post subject: (No subject)
While this is much better, there is still quite a few problems, specifically coding related.
Anyway I'll address only the Swing problems here.
First: the "show();" at the top is depricated, use "setVisible(true);" instead.
Secondly: only call "setVisible()", "show()" (now depricated) and "pack()" at the last line in your setup sub-routine, which in your case is the constructor itself. (Why we do this, is for efficiency reasons. That is, we call it only once, after we have finished creating the view, since a call to any of those three methods is deeply recursive to all the "added components" (or children) to that parent component.)
So what you need to do is remove that "show()" and add a setVisible(true); at the very end of the constructor's body.
Sponsor Sponsor
jin
Posted: Wed May 31, 2006 8:57 pm Post subject: (No subject)
I changed the show() to setVisible(true) and moved it to the bottom now i am able to see the menu tabs but the problem still with the menu items still remains.
I know how to add buttons, textarea, textfield, labels, etc. but not how to add graphics.
can u show me an example to see how to do it.
jin
Posted: Wed May 31, 2006 10:19 pm Post subject: (No subject)
i read something on the website on how to add graphics to a panel and tried it but it did not work. i tried both adding it to a panel then to the container and straight to the container none of them worked.
public class MAZE extends JFrame
{
public MAZE ()
{
super ("MAZE");
Container c = new Container ();
c.setLayout (new BorderLayout ());
JMenuBar menubar;
JMenu file, help;
JMenuItem newI, close, helpI, about;
newI = new JMenuItem ("New");
close = new JMenuItem ("Exit");
helpI = new JMenuItem ("Help");
about = new JMenuItem ("About");
file = new JMenu ("File");
help = new JMenu ("Help");
menubar = new JMenuBar ();
file.add (newI);
file.addSeparator ();
file.add (close);
help.add (helpI);
help.add (about);
menubar.add (file);
menubar.add (help);
setJMenuBar (menubar);
JPanel panel = new JPanel ();
panel.add (new PaintComponenet ());
c.add (panel);
//c.add (new PaintComponenet ());
setSize (403, 430);
setVisible (true);
}
private class PaintComponenet extends java.awt.Canvas
{
public void paint (Graphics g)
{
super.paint (g);
border (g);
}
public void border (Graphics g)
{
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 0);
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 352);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 0, y);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 365, y);
}
public void box (Graphics g, int x, int y)
{
g.setColor (new Color (117, 64, 17));
g.fillRect (10 + x, 60 + y, 25, 15);
}
}
public static void main (String args[]) throws IOException
{
MAZE application = new MAZE ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
is this the correct way that you meant when you said
Quote:
placing the graphics inside a JPanel, and then adding the JPanel to the JFrame.
HellblazerX
Posted: Thu Jun 01, 2006 4:12 pm Post subject: (No subject)
What I meant was to create a separate container class that extends JPanel, have your graphics to be drawn in there, and then add that JPanel to the JFrame. The reason why the menu tabs are shown behind the maze is because when you click on them, repaint () is automatically called to draw that tab. You need to keep the repainting of the maze and the repainting of the menu tabs separate, which is why you place your graphics components in a separate JPanel. An example of this class would be this:
code:
private class PaintComponent extends JPanel
{
public PaintComponent ()
{
setPreferredSize (new Dimension (403, 430));
}
public void border (Graphics g)
{
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 0);
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 352);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 0, y);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 365, y);
}
public void box (Graphics g, int x, int y)
{
g.setColor (new Color (117, 64, 17));
g.fillRect (x,y, 25, 15);
}
}
Also, this line:
code:
Container c = new Container ();
You've made a new container to hold the picture, but this container does not apply at all to your JFrame. What it should look like is this:
code:
Container c = getContentPane ();
This way, when you add things, they'll show up on your screen.
jin
Posted: Thu Jun 01, 2006 5:25 pm Post subject: (No subject)
I made the changes but now the menu items do not show up at all. the tab gets selected but the list does not appear.
public void border (Graphics g)
{
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 0);
for (int x = 0 ; x < 375 ; x += 26)
box (g, x, 352);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 0, y);
for (int y = 0 ; y < 350 ; y += 16)
box (g, 365, y);
}
public void box (Graphics g, int x, int y)
{
g.setColor (new Color (117, 64, 17));
g.fillRect (x, y, 25, 15);
}
}
public static void main (String args[]) throws IOException
{
MAZE application = new MAZE ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
HellblazerX
Posted: Thu Jun 01, 2006 5:52 pm Post subject: (No subject)
It works fine when I ran it. Your problem might be that you didn't spell component right:
code:
c.add (new PaintComponenet ());
should be:
code:
c.add (new PaintComponent ());
jin
Posted: Thu Jun 01, 2006 6:01 pm Post subject: (No subject)
Sorry bout that. stupid mistake on my part. java did not even give me an error.lol. thnx for all your help guys.
Oh yea quick question is there a easier way to make all the contents expand or decrease with the screen size or do i have to set up a scale factor.
magicman
Posted: Fri Jun 02, 2006 8:11 am Post subject: (No subject)
i have a question about this, im doing the same thing basicyl... but i need help with one small thing. When i want a new game, what do i put so that the program will make a new one. Like i have something like this for exiting the program.
code:
quitItem.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent ae)
{
System.exit (0);
}
}
i think i know the answer to this question, but to make sure, will text show in the box it self, or will it make a new window, just for the text. If it does make a new window, how do i make it show up in the same window.