Computer Science Canada

calin out to da masters:worked wif java for 2 hrs.Need Help!

Author:  Homer_simpson [ Sun Feb 08, 2004 11:51 pm ]
Post subject:  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...

Author:  Paul [ Mon Feb 09, 2004 2:19 pm ]
Post subject: 

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:
code:

//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...

Author:  rizzix [ Mon Feb 09, 2004 3:59 pm ]
Post subject: 

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
code:

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)
code:

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 here and also look up it's super class's (Graphics) documentation here


Integrating the DrawView into an applet..
code:

import javax.swing.*;

public class MyApplet extends JApplet {
    public MyApplet() {
        getContentPane().add(new DrawView());
    }
}



Displaying the applet...
code:

<html>
    <body>
        <applet code="MyApplet.class" codebase="./" align="BASELINE" width="400" height="400" />
    </body>
</html>

Author:  Tony [ Mon Feb 09, 2004 9:31 pm ]
Post subject: 

stuff like that just makes you appreciate the simplicity of turing's graphics, doesn't it? Laughing

Author:  rizzix [ Mon Feb 09, 2004 10:42 pm ]
Post subject: 

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.

Author:  Prince [ Mon Feb 09, 2004 10:53 pm ]
Post subject: 

tony wrote:
stuff like that just makes you appreciate the simplicity of turing's graphics, doesn't it? Laughing

im startin to miss turing already lol Laughing


: