d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Newb Needs Help. > Making A Wagon.
12Next
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 02:41pm
So for my java class I am doing shapes and attempting to make a wagon. My program the class uses is JCreator. I am having trouble making circles for the tires and I keep getting an error and am unsure why. The circles or arcs don't post but the upper rectangle and line do.

Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Wagon extends JFrame
   implements ActionListener {

   private JButton button;
   private JPanel panel;

   public static void main(String[] args) {
       Wagon frame = new Wagon();
       frame.setSize(1000, 1000);
       frame.createGUI();
       frame.setVisible(true);
   }

   private void createGUI() {
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       Container window = getContentPane();
       window.setLayout(new FlowLayout() );

       panel = new JPanel();
       panel.setPreferredSize(new Dimension(500, 500));
       panel.setBackground(Color.white);
       window.add(panel);

       button = new JButton("Press me");
       window.add(button);
       button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent event) {
       Graphics g = panel.getGraphics();
     g.setColor(Color.blue);
       g.drawLine(50, 60, 100, 100);
     //  g.drawLine( 100, 100, 0 , 100);
     //   g.drawLine( 0 ,0, 0 , 100);
      setBackground(Color.yellow);
g.setColor(Color.cyan);
  // g.fillRect(120,120,200,100);
 g.setColor(Color.red);
   g.fillRect(120,120,200,100);
   // g.fillRect(180,20,60,60);
   }
   
   public void actionPerformed(ActionEvent event) {
       Graphics g = panel.getGraphics();
       g.drawOval(0, 0, 100, 100);
        g.drawOval(500, 100, 200, 100);
        g.drawRect(0,0,70,90);
        g.fillArc(0,0,70,90,180,180);
        g.setColor(Color.red);
        g.drawArc(0,0,100,100,80,80);
   }
}



Ideas?

The top public void will display but the bottom public void errors out and shows nothing. Lost in the sauce here.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 4 2013 02:50pm
curious but why do they have the same name and same args?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 02:56pm
Quote (Eep @ 4 Feb 2013 15:50)
curious but why do they have the same name and same args?


I just realized my problem. lol

Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Wagon extends JFrame
  implements ActionListener {

  private JButton button;
  private JPanel panel;

  public static void main(String[] args) {
      Wagon frame = new Wagon();
      frame.setSize(1000, 1000);
      frame.createGUI();
      frame.setVisible(true);
  }

  private void createGUI() {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      Container window = getContentPane();
      window.setLayout(new FlowLayout() );

      panel = new JPanel();
      panel.setPreferredSize(new Dimension(500, 500));
      panel.setBackground(Color.white);
      window.add(panel);

      button = new JButton("Press me");
      window.add(button);
      button.addActionListener(this);
  }

  public void actionPerformed(ActionEvent event) {
      Graphics g = panel.getGraphics();
    g.setColor(Color.blue);
      g.drawLine(50, 60, 100, 100);
    //  g.drawLine( 100, 100, 0 , 100);
    //   g.drawLine( 0 ,0, 0 , 100);
     setBackground(Color.yellow);
g.setColor(Color.cyan);
 // g.fillRect(120,120,200,100);
g.setColor(Color.red);
  g.fillRect(120,120,200,100);
  // g.fillRect(180,20,60,60);
      g.drawOval(0, 0, 100, 100);
       g.drawOval(500, 100, 200, 100);
       g.drawRect(0,0,70,90);
       g.fillArc(0,0,70,90,180,180);
       g.setColor(Color.red);
       g.drawArc(0,0,100,100,80,80);
  }
}


It should look like this. Is there a way to clean it up so it doesn't look so messy all closed together? Also my other problem is I can see my Oval (Which will be a circle). So far I have this coded as the last part.

Code
   public void actionPerformed(ActionEvent event) {
       Graphics g = panel.getGraphics();
     g.setColor(Color.blue);
       g.drawLine(50, 60, 100, 100);
     //  g.drawLine( 100, 100, 0 , 100);
     //   g.drawLine( 0 ,0, 0 , 100);
      setBackground(Color.yellow);
g.setColor(Color.cyan);
  // g.fillRect(120,120,200,100);
 g.setColor(Color.red);
   g.fillRect(120,120,200,100);
   // g.fillRect(180,20,60,60);
       g.fillOval(140, 140, 50, 50);
       g.setColor(Color.green);
   }
}


I got rid of the top stuff and just added the oval. I tried draw and fill yet the oval(circle) won't appear.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 4 2013 03:01pm
why are you commenting out so many lines of code? why not just delete them?


also, in the bottom code you posted, it looks like you never draw anything. You attempt to fill an oval which (at least to me) doesn't seem to exist yet...


keep your indenting consistent IMO. Don't have lines of code appearing farther left than your method dec.

This post was edited by Eep on Feb 4 2013 03:02pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 03:01pm
Now I'm having trouble making circles for wheels and lining the objects together.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 03:04pm
Quote (Eep @ 4 Feb 2013 16:01)
why are you commenting out so many lines of code? why not just delete them?


also, in the bottom code you posted, it looks like you never draw anything. You attempt to fill an oval which (at least to me) doesn't seem to exist yet...


Oh the comments were from another program piece I need to delete and you are right on the oval. Thank you.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 4 2013 03:08pm
Quote (NinjaSushi2 @ Feb 4 2013 04:04pm)
Oh the comments were from another program piece I need to delete and you are right on the oval. Thank you.


np I am just now learning java this semester but it is pretty similar to c++ so I can still spot stuff like that
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 03:20pm
Here is a SS and the code so far.


Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Wagon extends JFrame
   implements ActionListener {

   private JButton button;
   private JPanel panel;

   public static void main(String[] args) {
       Wagon frame = new Wagon();
       frame.setSize(1000, 1000);
       frame.createGUI();
       frame.setVisible(true);
   }

   private void createGUI() {
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       Container window = getContentPane();
       window.setLayout(new FlowLayout() );

       panel = new JPanel();
       panel.setPreferredSize(new Dimension(500, 500));
       panel.setBackground(Color.white);
       window.add(panel);

       button = new JButton("Press me");
       window.add(button);
       button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent event) {
       Graphics g = panel.getGraphics();
     g.setColor(Color.blue);
       g.drawRect(50, 60, 50, 100);
  g.setColor(Color.red);
       g.fillRect(120,120,200,100);
     g.setColor(Color.black);      
    g.fillOval(95, 200, 50, 50);
       g.fillOval(295, 200, 50, 50);
   }
}






The question is... How can I take that left box, turn it diagonally and make it a handle?

(I blew it up in pain so it looks all blocky.)

This post was edited by NinjaSushi2 on Feb 4 2013 03:20pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 4 2013 03:21pm
Well I made the wagon smaller and moved it to be center with the wheels. Still having trouble on a handle.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Feb 4 2013 03:54pm
not sure. Maybe check the Rotate methods in the java G2D class


Quote
abstract  void rotate(double theta)
          Concatenates the current Graphics2D Transform with a rotation transform.
abstract  void rotate(double theta, double x, double y)
          Concatenates the current Graphics2D Transform with a translated rotation transform.


This post was edited by Eep on Feb 4 2013 03:55pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll