Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help with Java code

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Java code

    Hello, I'm making a Java code for school but unfortunately I have some issues.

    This is my script til now on:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
     
    public class Cirkels extends JFrame{
    	public static void main(String[] args) {
     
    		Cirkels w = new Cirkels();
    		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		w.setSize(800,650);
    		w.setVisible(true);
     
    			}
     
    	private JSlider slider;
    	private JLabel tekstslider;
    	private Paneel mijnpaneel;
     
     
    	public Cirkels(){
     
     
    		mijnpaneel = new Paneel();
    		mijnpaneel.setBackground(Color.GRAY);
    		mijnpaneel.setOpaque(true);
     
     
    		slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
    		slider.setMajorTickSpacing(10);
    		slider.setPaintTicks(true);
     
    		slider.addChangeListener(
    				new ChangeListener(){
    					public void stateChanged(ChangeEvent e){
    					mijnpaneel.setD(slider.getValue());
     
    					}
    				}
    				);
     
    		add(slider, BorderLayout.SOUTH) ;
    		add(mijnpaneel, BorderLayout.CENTER) ;
     
    			}
     
    public class Paneel extends JPanel{
     
    	private int d  = 10;
    	public void paintComponent (Graphics g) {
    		super.paintComponent (g);
    		g.setColor(Color.WHITE);
    		g.drawOval(25, 100, d, d);
    		g.drawOval(175, 100, d, d);
    		g.drawOval(325, 100, d, d);
    		g.drawOval(475, 100, d, d);
    		g.drawOval(625, 100, d, d);
    		g.drawOval(100, 200, d, d);
    		g.drawOval(250, 200, d, d);
    		g.drawOval(400, 200, d, d);
    		g.drawOval(550, 200, d, d);
     
     
    	}
     
    	public void setD(int newD){
    	d = (newD >= 0 ? newD : 10); 
    	repaint();
    	}
     
    	public Dimension getPrefferedSize() { 
    		return new Dimension(200,200);
    	}
     
    	public Dimension getMinimumSize() {
    		return getPrefferedSize();
    	}
     
     
    	 }
    }

    I want to add a JButton to make the ovals different colors, so for example:
    JButton for Red and a JButton for Blue, but when I add the buttons the whole layout mess up and no buttons appear...
    Can someone help me with adding the buttons?

    Sorry for my English, I'm Dutch


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Help with Java code

    Please use the highlight tags to preserve formatting. Unformatted code is pretty hard to read.

    Recommended reading: http://docs.oracle.com/javase/tutori...ts/button.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code

    Sorry but how can I use the highlight tags?
    And I know the general rules for adding a JButton but I can show with a picture what happens.

    Before adding a simple JButton:
    goed.jpg

    After adding JButton:
    fout.jpg

    The whole layout changes, and the ovals dissapear.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Help with Java code

    Use the highlight tags by checking out the forum rules you were linked to upon joining, or by quoting another post that uses them to see how they work.

    More recommended reading: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code

    Thank you for the guides, I'm really a beginner in Java and I find it also hard to read English articles but I read them and I understand what they mean but like when I try to fix my code it won't happen. I changed the code btw with the tags

    --- Update ---

    I think there is not very much code needed to add 2 buttons which sets the color of the Oval

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Help with Java code

    You're right; there isn't much code required. But we can't just do your homework for you.

    You already use a JSlider to change the diameter of the circles. Changing the colors would be done in a very similar way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code

    Programming is frustrating haha, I worked for 5 hours on this piece of code.

    g.setColor(Color.RED);

    This will set the ovals to another color, but the problem is that I can't add a JButton to the script if I know how to do that without messing everything up I think I can solve it myself.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Help with Java code

    See how you save the diameter to a value named d, which you change using a JSlider?

    You can do the same thing with a Color variable:

    Color c = new Color(255, 0, 0); //Color.RED
    g.setColor(c);


    Programming is all about breaking things down into smaller pieces. I would start by creating a separate program that simply displays a button and prints something out when you click it. When you get that working, you can work on using a layout to add the JButton to your program with the correct size and position. Then you can work on linking it to the color variable in the same way you change the diameter value.

    Recommended reading: How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code

    Okay thank you, I'm further now and I managed to make a button.
    My code:

    		JButton zwart = new JButton("Zwart");
    		add(zwart, BorderLayout.PAGE_START);
    		KnopHandler kh = new KnopHandler();
    		zwart.addActionListener(kh)(
    		new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    						g.setColor(Color.BLACK);
    			}
    		}
    		);

    It gives 2 errors at KnopHandler kh = new KnopHandler();
    and at zwart.addActionListener(kh)(

    It says: KnopHandler can't not be resolved in a type

    --- Update ---

    Okay thank you, I'm further now and I managed to make a button.
    My code:

    		JButton zwart = new JButton("Zwart");
    		add(zwart, BorderLayout.PAGE_START);
    		KnopHandler kh = new KnopHandler();
    		zwart.addActionListener(kh)(
    		new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    						g.setColor(Color.BLACK);
    			}
    		}
    		);

    It gives 2 errors at KnopHandler kh = new KnopHandler();
    and at zwart.addActionListener(kh)(

    It says: KnopHandler can't not be resolved in a type

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Help with Java code

    Well, what is the KnopHandler class?

    Also, that syntax is incorrect. What are you trying to do here:

    zwart.addActionListener(kh)(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    g.setColor(Color.BLACK);
    }
    }
    );

    You call zwart.addActionListener(kh), but then you have another block of code inside parenthesis for some reason? This won't compile.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: May 23rd, 2013, 05:35 PM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 12:36 PM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM