Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By sunde887

Thread: drawing in java

  1. #1
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default drawing in java

    I'm was trying to make a class that will accept a graphics object and some parameters, apply methods on graphics object, and then return it back to the source to be 'published'.
    Is it possible?

    Java Code:
    import java.awt.*; 
    import java.awt.image.BufferedImage; 
    import javax.swing.*; 
    
    public class Drawing {
           
        public static void main(String[] args) {
            
            int x=30;
            int y=80;
                    
            Graphics2D g1=null; 
            Graphics2D g2=null;
            
            Lineing lineing = new Lineing();
            lineing.setObject(g1);
            g2 = lineing.painter(x,y);
                   
            BufferedImage bimage = g2.getDeviceConfiguration().createCompatibleImage(400, 400, Transparency.OPAQUE);
            ImagePanel contentPane = new ImagePanel(bimage);
                    
            JFrame f = new JFrame("Linea");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            f.setBounds(200, 200, 500, 500);
            f.setVisible(true);
            f.setResizable(false);
       }
    }


    Java Code:
    import java.awt.*;
    
    
    public class Lineing {
        
        Graphics2D panel = null;
            
        public void setObject(Graphics2D g){
            panel = g;
        }
          
         public Graphics2D painter(int a, int b){
            panel.setColor(new Color(50, 50,200));
            panel.setStroke(new BasicStroke(14F));
            panel.drawLine(a,a,b,b); 
            return panel;
         }    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: drawing in java

    I feel like it may be easier to just allow the class to draw itself. Then override the paintComponent of where you want to paint and call the method.

    Java Code:
    public class Ball{
      private int x, y;
      public void drawBall(Graphics2D g){
        g.fillOval(...);
      }
    }
    public class OverriddenJPanel extends JPanel{
      private Ball b;
      @Override public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        b.drawBall(g2d);
      }
    }

    To make this more general you can allow the class to not override any other class and have it's own method to draw everything. Then you can simply allow the class to be drawn on any class with an overridden paintComponent method.
    KevinWorkman likes this.

  3. #3
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default Re: drawing in java

    Well,the idea was to have a class that will prepare some graphic output and then return back that object without actually displaying it .
    Problem is that I'm not yet sure where do I need the output.
    There is gonna be lots of graphs and calculations.

    Is it possible to have a set of Graphics objects (g1, g2, g3...) that could be sent to that class, which draws upon 'em according to one set of parameters, and returns them back as (G1, G2, G3)
    so that I can decide later how to output them?

    In fact I need at least 2 classes like that because I need the outcomes of calculations presented in more different ways.
    The other class is supposed to take same (g1, g2, g3) with second set of parameters, and to give back objects (G1' G2' G3')....etc.

Similar Threads

  1. Java GUI drawing a line
    By seanfmglobal in forum New To Java
    Replies: 26
    Last Post: 04-26-2011, 05:52 AM
  2. Drawing coordinates in Java GUI
    By javausr in forum Java 2D
    Replies: 5
    Last Post: 12-29-2010, 10:49 AM
  3. Drawing strings in Java
    By LyraM in forum Java 2D
    Replies: 4
    Last Post: 05-11-2010, 11:15 PM
  4. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 06:35 PM
  5. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 10:35 AM

Posting Permissions

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