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

Thread: drawing in java

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

    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();
            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.setBounds(200, 200, 500, 500);

    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));
            return panel;

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

    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){
    public class OverriddenJPanel extends JPanel{
      private Ball b;
      @Override public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

    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 is offline Member
    Join Date
    Aug 2011
    Rep Power

    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, 06:52 AM
  2. Drawing coordinates in Java GUI
    By javausr in forum Java 2D
    Replies: 5
    Last Post: 12-29-2010, 11:49 AM
  3. Drawing strings in Java
    By LyraM in forum Java 2D
    Replies: 4
    Last Post: 05-12-2010, 12:15 AM
  4. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 07:35 PM
  5. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 11: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