Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 calin out to da masters:worked wif java for 2 hrs.Need Help!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Homer_simpson




PostPosted: 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...
Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Mon Feb 09, 2004 2:19 pm   Post subject: (No 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...
rizzix




PostPosted: Mon Feb 09, 2004 3:59 pm   Post subject: (No 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>
Tony




PostPosted: Mon Feb 09, 2004 9:31 pm   Post subject: (No subject)

stuff like that just makes you appreciate the simplicity of turing's graphics, doesn't it? Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
rizzix




PostPosted: Mon Feb 09, 2004 10:42 pm   Post subject: (No 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.
Prince




PostPosted: Mon Feb 09, 2004 10:53 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: