
-----------------------------------
Homer_simpson
Sun Feb 08, 2004 11:51 pm

calin out to da masters:worked wif java for 2 hrs.Need Help!
-----------------------------------
i've worked with java around 2 hours now... so i know the basics... i tried graphics but appearantly u can only use graphics on a gui window... can somebody show me a sample code that draw a simple line somewhere...

-----------------------------------
Paul
Mon Feb 09, 2004 2:19 pm


-----------------------------------
I only know how to draw stuff in an applet, where you have to see it with appletviewer. This is how I do it in an applet:

//draw a line
import javax.swing.JApplet;
import java.awt.Graphics;

public class Line extents JApplet {
   public void paint ( Graphics g )
{
        g.drawLine ( 15, 10, 210, 10 );
}
}


Don't know if this helps you...

-----------------------------------
rizzix
Mon Feb 09, 2004 3:59 pm


-----------------------------------
extending the example in the swing tutorial I wrote.. In the most simplest possible way I can demonstrate drawing in swing would be this:

The Application Class

import javax.swing.*;
import java.awt.Dimension;

public class Test {
    public static void main(String[] args) {
        // initialised with the title for the title bar
        JFrame frame = new JFrame("Test Application");
        Dimension screenSize = frame.getToolkit().getScreenSize();
        frame.setBounds(screenSize.width/4, screenSize.height/4,
                        screenSize.width/2, screenSize.height/2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        
        frame.getContentPane().add(new DrawView());
        frame.show();
    }
}



The DrawView Class (the component on which we will be drawing)

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;

class DrawView extends Canvas {

   DrawView() {
      super();
      setBackground(Color.white);
   }

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setPaint(Color.RED);
		g2d.drawLine(10, 20, 200, 150);
	}
}


basically your extending a component class (Canvas to be more specific) and overload its public void paint(Graphics g); method.

for more information on what methods you can call from the Graphics2D object look up the documentation 
import javax.swing.*;

public class MyApplet extends JApplet {
    public MyApplet() {
        getContentPane().add(new DrawView());
    }
}



Displaying the applet...


    
        
    



-----------------------------------
Tony
Mon Feb 09, 2004 9:31 pm


-----------------------------------
stuff like that just makes you appreciate the simplicity of turing's graphics, doesn't it? :lol:

-----------------------------------
rizzix
Mon Feb 09, 2004 10:42 pm


-----------------------------------
well the application class and the applet class will mostly be like that example above (with exception to the size of the window) almost all the time.

all u need to do is change stuff in ur DrawView class. (which is basically a Jcomponent) and that too usually in the paint() method.

-----------------------------------
Prince
Mon Feb 09, 2004 10:53 pm


-----------------------------------
stuff like that just makes you appreciate the simplicity of turing's graphics, doesn't it? :lol:
im startin to miss turing already lol :lol:
