Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,313 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,055 people online right now. Registration is fast and FREE... Join Now!




Graphics and radios not showing

 

Graphics and radios not showing

Synth

1 Mar, 2008 - 05:57 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 44



Thanked: 1 times
My Contributions
I'm trying to make a towers of hanoi game and i've got the general layout setup and everything but i can't get the graphics to show properly along with the two sets of radio buttons. the graphics are created under the radio and radio2 methods and called under the the public Panel cp() method within the first 4 or so sets of codes. The graphics are called within the first method and in the public void paint method. they both showed at one point and they stopped showing up all of a sudden. can some one please help me?

CODE

/* Marc Mendoza
* AP Java 3rd Block
* Program #3 Towers of Hanoi*/

import javax.swing.*;//imports utilities
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Hanoi extends JFrame implements ActionListener, ItemListener
{
    private JRadioButton[] radioButton = new JRadioButton[3];
    private JRadioButton[] radioButton2 = new JRadioButton[3];
    JPanel radio = new JPanel();
    JPanel radio2 = new JPanel();
    JFrame mw = new JFrame();//variables initialized
    Panel cp = new Panel();
    Panel cp2 = new Panel();
    Container stand = new Container();
    JScrollPane textArea = new JScrollPane();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//calls screensize dimension
    
    public Hanoi()
    {
        mw.add(cp2());//calls various methods and adds it to the contentpane which is then added to the main window
        mw.add(cp());
        mw();
        stand = mw.getContentPane();
        Graphics g = stand.getGraphics();
        g.setColor(Color.black);
        g.drawRect(50,50,200,100);
        g.fillRect( 50, 50, 200, 100);
    }
    
    public JFrame mw()//creates main window
    {
        mw.setBounds(d.width / 4, d.height / 30, 750, 1000);
        mw.setVisible(true);
        mw.setResizable(false);
        mw.setTitle("Towers of Hanoi");
        mw.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mw.setLayout(new GridLayout(0, 1));
        JLabel image = new JLabel(new ImageIcon("ship.jpg"));//image added to label and to container
        mw.setIconImage(new ImageIcon("hicon.jpg").getImage());
        return mw;
    }
    
    public JPanel radio()//creates radio button
    {
        String[] rtext = {"A", "B", "C"};
        for(int i = 0; i < rtext.length; i++)
        {
            radioButton[i] = new JRadioButton(rtext[i]);
            radioButton[i].addItemListener(this);
            radio.add(radioButton[i]);
        }
        return radio;
    }
    
    public JPanel radio2()//creates radio button
    {
        String[] rtext = {"a", "b", "c"};
        for(int i = 0; i < rtext.length; i++)
        {
            radioButton2[i] = new JRadioButton(rtext[i]);
            radioButton2[i].addItemListener(this);
            radio2.add(radioButton[i]);
        }
        return radio;
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.black);
        g.drawRect( 50, 50, 200, 100);
    }
    
    public Panel cp()//creates right side of mw
    {
        cp.setLayout(new BorderLayout());
        
        Panel main = new Panel();
        main.setLayout(new GridLayout(0, 1));
        
        Panel first = new Panel();
        first.setLayout(new GridLayout(1, 0));
        JLabel source = new JLabel();
        source.setText("Source:");
        main.add(source);
        first.add(radio());
        main.add(first);
        
        Panel second = new Panel();
        second.setLayout(new GridLayout(1, 0));
        JLabel dest = new JLabel();
        dest.setText("Destination:");
        main.add(dest);
        second.add(radio2());
        main.add(radio2());
        
        JButton moveb = new JButton("Move");
        moveb.addActionListener(this);
        JButton resetb = new JButton("Reset");
        resetb.addActionListener(this);
        JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout());
        buttons.add(moveb);
        buttons.add(resetb);
        
        JLabel number = new JLabel();
        number.setText("# of disks:");
        Panel disk = new Panel();
        JTextField in = new JTextField(22);
        in.addActionListener(this);
        in.setText("");
        in.setHorizontalAlignment(JTextField.RIGHT);
        disk.add(number);
        disk.add(in);
        
        main.add(buttons);
        main.add(disk);
        cp.add(main, BorderLayout.CENTER);
        cp.add(textArea(), BorderLayout.EAST);
        return cp;
    }
    
    public Panel cp2()//creates header and towers
    {
        JLabel header = new JLabel();
        header.setText("Towers of Hanoi");//creates header
        header.setSize(150, 25);
        cp2.add(header);//adds header to content pane
        return cp2;
    }
    
    public JScrollPane textArea()//creates textarea
    {
        JTextArea text = new JTextArea();
        text.setColumns(20);
        text.setRows(10);
        text.setEditable(false);
        JScrollPane scrollText = new JScrollPane(text);
        scrollText.setSize(200, 465);
        return scrollText;
    }
    
    public void actionPerformed(ActionEvent event)//action listener
    {
        
    }
    
    public void itemStateChanged(ItemEvent event)
    {
        
    }
    
    public static void main(String[] args)//runs the hanoi method
    {
        Hanoi h = new Hanoi();
    }
}


User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Graphics And Radios Not Showing

2 Mar, 2008 - 12:33 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You have some serious issues with how to break up your tasks properly. I have sat here for 15 minutes trying to figure where you have even put everything. I see statements that should be in one central function scattered all over the program.

It is this problem that it is leading to your troubles now. You have to remember that you should build all your controls (panels etc) and add them to the form and do the setVisible(true) statement for the frame towards the very end so that "realizes" all the elements and shows them.

I have noticed that you created a mw JFrame and then add stuff to it. Why didn't you just do that with Hanoi itself? After all, you state it is a decendant of JFrame (aka Hanoi extends JFrame). Then you setup your cp panels, then call your windows, then attempt to draw after you realized the window. It is all very confusing and unorganized.

But as far as your radio buttons concerned, you can do the following to get them to show up...

1) Go to your radio2() function. Notice when you go to add to radio2 panel, you specified radioButton[i] and not radioButton2[i]. Then the panel you return needs to be "radio2" and not "radio".

2) After those changes, go to cp() and to the following lines...

java

main.add(dest);
second.add(radio2());
main.add(radio2());


See where you call main.add with radio2()? Change that to main.add(second); so that it will add the second panel and not repeat your radio buttons.

After these changes you should start seeing your radio buttons appearing. Your images not showing is because you created your JLabel "image" but you never add it to anything. You defined it be that is it. Add it to a panel like first or something down in cp()...

java

main.add(source);
first.add(radio());

// Here we defined the image and add it to "first" panel.
JLabel image = new JLabel(new ImageIcon("flower.jpg"));//image added to label and to container

// Now add it to the first panel
first.add(image);
main.add(first);


So there you go. That should fix both problems. Now clean up that code and organize it up again. It is a disgrace! wink2.gif

"At DIC we be java radio and graphic placing code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

baavgai

RE: Graphics And Radios Not Showing

2 Mar, 2008 - 03:12 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,260



Thanked: 389 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Overriding paint on the some container that you're adding swing components too is probably not ideal.

Also, your Hanoi object has an identity crisis. It wants to be a frame be you never use it as such, instead creating mw for no particular reason.

You can change one line, like so:
java

//JFrame mw = new JFrame();//variables initialized
JFrame mw = this;


Which makes little functional difference at the moment, but now makes an extended JFrame make a little more sense.

Also, the variable size setting of the frame is a little odd: mw.setBounds(d.width / 4, d.height / 30, 750, 1000);. You've gone through the effort of determining my screen size and then you just ignore it where it counts. A hight of 1000 makes me move the frame to see if I'm missing anything.

You may really wish to use a fixed frame size and build on that, I usually go with 640x480, or 800x600, anything greater can loose people. Then you have the freedom to absolutely position things. I wouldn't even draw for this one, just create some JPanels and position them for my rings. No need to worry about painting or redrawing that way.

Hope this helps.

User is online!Profile CardPM
+Quote Post

jack1311

RE: Graphics And Radios Not Showing

30 Aug, 2009 - 04:44 AM
Post #4

New D.I.C Head
*

Joined: 23 Aug, 2009
Posts: 2

can you send me project of hanoi_tower, i really need it ?
here is my project, i don't know where is wrong
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 02:20PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month