Results 1 to 7 of 7
  1. #1
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default passing parameters to graphics

    I'm working on an application, and I want to pass the parameters int x, int y, int width, int height to a method (in a class other than main) to draw a square using those parameters. I have the method public void draw( Graphics g ) and I want to add those parameters (int x, int y, int width, int height) to the method, how would I do this? I tried making public void draw(Graphics g, int x, int y, int width, int height), but it didn't work.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You have replies to your previous threads that you've not yet acknowledged. Many here, me included, may hold off answering any more of your questions until they know that you actually read them.
    Best of luck.

    edit: in fact in several threads:
    convert a string to an object in java
    errors with do while loop
    get xml data from url
    Java method to run code from string
    java graphics within main method

    We are volunteers after all, and your thanks are our only payment. Also it is simple common courtesy.

    Again, best of luck.
    Last edited by Fubarable; 02-02-2010 at 04:31 PM.

  3. #3
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default

    oh crud sorry I start threads and often forget about them... thanks for reminding me

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for your responses.

    Back to your problem: Say you've got a JPanel called DrawRectPanel that draws a rectangle from 10, 10 to a variable width, say rectW and a variable height, say rectH, with a paintComponent method that looks similar to this:
    Java Code:
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(10, 10, rectW, rectH);
      }

    The best way to do this (that I know of) is to make rectW and rectH class variables with public setter methods. Then you can change the rectW and rectH states via the setter methods and call repaint on the panel.

    Here's an example of what I meant:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    // main method containing class that creates the JFrame and ties all together
    public class GraphicsParam {
      private static final Dimension DR_PANEL_SIZE = new Dimension(220, 220);
    
      private static void createAndShowUI() {
        // the JPanel that draws the rectangle
        DrawRectPanel drawRectPanel = new DrawRectPanel();
        drawRectPanel.setPreferredSize(DR_PANEL_SIZE);
        
        // the JPanel/class that controls the drawing panel above
        final DrawPanelController gppController = 
          new DrawPanelController(drawRectPanel);
        
        JFrame frame = new JFrame("Graphics Parameters");
        frame.getContentPane().add(drawRectPanel, BorderLayout.CENTER);
        frame.getContentPane().add(gppController, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    
    class DrawRectPanel extends JPanel {
      public static final int INSET = 10;
      private int rectW = 100;
      private int rectH = 100;
      
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(INSET, INSET, rectW, rectH);
      }
    
      public void setRectW(int rectW) {
        this.rectW = rectW;
      }
    
      public void setRectH(int rectH) {
        this.rectH = rectH;
      }
    
    }
    
    class DrawPanelController extends JPanel {
      private DrawRectPanel drPanel;
      private JSlider widthSlider = new JSlider(0, 200, 100);
      private JSlider heightSlider = new JSlider(0, 200, 100);
      
      public DrawPanelController(DrawRectPanel drPanel) {
        this.drPanel = drPanel;
        
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(new JLabel("Width"));
        add(widthSlider);
        add(new JLabel("Height"));
        add(heightSlider);
        
        SliderListener sliderListener = new SliderListener();
        widthSlider.addChangeListener(sliderListener);
        heightSlider.addChangeListener(sliderListener);
      }
      
      private class SliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
          JSlider slider = (JSlider) e.getSource();
          if (slider == widthSlider) {
            drPanel.setRectW(slider.getValue());
          } else {
            drPanel.setRectH(slider.getValue());
          }
          drPanel.repaint();
        }
      }
    }

  5. #5
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default thanks

    thanks, that helps alot :)

  6. #6
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default

    What does the @Override do???

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jforce93 View Post
    thanks, that helps alot
    You're quite welcome.

    What does the @Override do???
    It tells the compiler to warn me if the method that follows is not a true override. See what happens if you use the @Override annotation and change this line:
    Java Code:
    protected void paintComponent(Graphics g) {
    to this:
    Java Code:
    private void paintComponent(Graphics g) {
    Now remove the @Override and see what happens. It will compile but paintComponent will never be called since it isn't a true override.

Similar Threads

  1. passing parameters to next jsp using javascript
    By sridevisala in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 06:34 AM
  2. [SOLVED] Passing parameters
    By robocop in forum New To Java
    Replies: 3
    Last Post: 03-03-2009, 11:16 PM
  3. passing parameters to paint mtd
    By themburu in forum Java Applets
    Replies: 3
    Last Post: 07-14-2008, 09:15 PM
  4. passing parameters between two applets
    By veena1612 in forum Java Applets
    Replies: 1
    Last Post: 05-27-2008, 10:29 AM
  5. JNI - passing and returning parameters by value
    By java_to_c in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 09:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •