Results 1 to 3 of 3
  1. #1
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default Writing classes in graphics

    Hey,

    Have been practising writing classes and having been used passing some parameters through a methd. Now, I am faced with graphics : (

    How or what can I pass through in my main for each of the parameters I have defined i.e. Graphics page, int x, int y, int rad, Color color. Obviosuly I know that x, y and rad are intergers but not sure what to pass thru for a graphics page and how to pass thru a color??

    Also, I have deliberatley not put a return type in my method but with my new instance in main I am not sure what type to use for the new circle object??

    Code:
    package Chapter4;

    import java.awt.*;
    // assumes java.awt.* is imported
    public class DrawCircle{
    public DrawCircle(){
    }
    public DrawCircle (Graphics page, int x, int y, int rad, Color color)
    {
    page.setColor(color);
    page.drawOval(x, y, rad, rad);
    }
    public static void main(String[]args){
    DrawCircle circle = new DrawCircle();
    newcircle = circle.drawcircle(4,4,4,blue);?????

    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class DrawingGraphics {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GraphicComponent());
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class GraphicComponent extends JPanel {
        DrawCircle drawCircle = new DrawCircle();
    
        // Java supplies a graphics context which becomes available
        // (non–null) when/after the component becomes visible.
        protected void paintComponent(Graphics g) {
            // Where does the graphics context it come from?
            System.out.println("g = " + g.getClass().getName());
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            // Now we have a real, live graphics context that we
            // can use for custom drawing/graphics.
            // Draw a resizable, centered circle:
            int w = getWidth();
            int h = getHeight();
            int diameter = 2*Math.min(w, h)/3;
            int x = (w - diameter)/2;
            int y = (h - diameter)/2;
            drawCircle.drawCircle(g, x, y, diameter, Color.red);
        }    
    }
    
    class DrawCircle {
        public DrawCircle() { }
    
        public void drawCircle (Graphics page, int x, int y,
                                int rad, Color color) {
            page.setColor(color);
            page.drawOval(x, y, rad, rad);
        }
    
        public static void main(String[] args){
            final DrawCircle circle = new DrawCircle();
            JPanel panel = new JPanel() {
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    circle.drawCircle(g,25,25,50,Color.blue);
                }
            };
            panel.setPreferredSize(new Dimension(100,100));
            JOptionPane.showMessageDialog(null, panel, "test", -1);
        }
    }

  3. #3
    CyberFrog is offline Member
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Default DrawGraphics

    Hi Hardwired,
    Thanks for your code but am a bit confused since all I wanted to do was to simply manipulate my main the the simplest possible way so that my method could pick it up? Also how can I do this if I did not want to return anything in my method i.e. not even a void?

    Thanks

Similar Threads

  1. Classes in graphics
    By CyberFrog in forum New To Java
    Replies: 0
    Last Post: 04-02-2008, 09:11 PM
  2. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  3. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  4. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM
  5. Updating Graphics
    By Greedful in forum Java 2D
    Replies: 2
    Last Post: 07-20-2007, 07:12 PM

Posting Permissions

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