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

Username:   Password: 
 RegisterRegister   
 drawing in java applets
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zylum




PostPosted: Sun Jan 16, 2005 11:17 pm   Post subject: drawing in java applets

can anyone just show me a quick example of how to draw a polygon with moving vertecise? i want to convert my turing 3d engine into java... i just need to know how to draw. i know how to do the rest but the book that i have doesnt cover drawing in appets.
Sponsor
Sponsor
Sponsor
sponsor
McKenzie




PostPosted: Mon Jan 17, 2005 4:16 pm   Post subject: (No subject)

You need to understand how Applets work before you worry about graphics in them. You may find out that because of security limitations you can not do what you want to for example. Here is an example of drawing a polygon (from SAMS 21 Days)


Map1b.java
 Description:

Download
 Filename:  Map1b.java
 Filesize:  725 Bytes
 Downloaded:  168 Time(s)

rizzix




PostPosted: Mon Jan 17, 2005 5:35 pm   Post subject: (No subject)

hmm there's a "faster" way to draw polygons.. do a search in the java-help section.. Homer_simpson originally had asked the same questions.. you will find your answers in some of his threads.

take a look at this thread in perticular: http://www.compsci.ca/v2/viewtopic.php?t=3740&highlight=
zylum




PostPosted: Mon Jan 17, 2005 7:31 pm   Post subject: (No subject)

i dont know what im doing wrong... it compiles but it doesnt run Sad

code:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Color;

abstract class BufferedCanvas extends Canvas {
   
    private BufferedImage bImage;    // offscreen buffer
    private Graphics2D bg = null;           // offscreen graphics context
    private Graphics2D g2d = null;      // onscreen graphics context
    private int w, h;               // width and height of canvas (Component)
    private Container parent;
   
    BufferedCanvas(Container parent) {
        super();
        setBackground(Color.black);
        this.parent = parent;
    }
   
    public void paint(Graphics g) {
        g2d    = (Graphics2D) g;               // cast to Graphics2D (just in case)
        bImage = new BufferedImage(maxx(), maxy(), BufferedImage.TYPE_INT_RGB);  // create a new buffer image
        bg     = bImage.createGraphics();         // get Graphics context for image
       
        offscreenPaint();
       
        bg.dispose();                    // dispose graphics context
        bImage.flush();                  // flush (empty) image
        bg = null;
    }
   
    abstract protected void offscreenPaint();
   
    public void update() {
        g2d.drawImage(bImage, 0, 0, this);  // load buffer to screen
    }
   
    public void setColor(Color clr) {
        if (bg != null) {
            bg.setColor(clr);
        }
    }
   
    public void setXORMode(Color clr) {
        if (bg != null) {
            bg.setXORMode(clr);
        }
    }
   
    public void drawoval(int x, int y, int w, int h) {
        if (bg != null) {
            bg.drawOval(x, y, w, h);
        }
    }
   
    public void delay(int msec) {
        try {
            Thread.sleep(msec);
        } catch (Exception e) {
            System.err.println(e);
        }
    }
   
    public int maxx() {
        return parent.getBounds().width;
    }
   
    public int maxy() {
        return parent.getBounds().height;
    }
}



class DrawView extends BufferedCanvas {
   
    DrawView(JFrame parent) {
        super(parent);
    }
   
    protected void offscreenPaint() {
        setXORMode(Color.white);      // set white as the XORed colour
       
        for (int i = 0; i < maxx(); i++)
        {
            drawoval(i, i % 200, 100, 100);
            update();
            drawoval(i, i % 200, 100, 100);
        }
    }
}


class drawTest {
    public static void main(String[] args) {
        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.getContentPane().add(new DrawView(frame));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
    }
}
rizzix




PostPosted: Mon Jan 17, 2005 8:21 pm   Post subject: (No subject)

oops here try this code:
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public abstract class BufferedCanvas extends Canvas {
   
    private Image bImage;   // offscreen buffer
    private Graphics bg = null;   // offscreen graphics context
    private Graphics2D g2d = null// onscreen graphics context
    private int w, h;               // width and height of canvas (Component)
    private Container parent;
   
    BufferedCanvas(Container parent) {
        super();
        setBackground(Color.black);
        this.parent = parent;
    }
   
    public void paint(Graphics g) {
        g2d    = (Graphics2D) g;         // cast to Graphics2D (just in case)
        // create a new buffer image
        bImage = createImage(maxx(), maxy())
        bg     = bImage.getGraphics();// get Graphics context for image
       
        offscreenPaint();
       
        bg.dispose();                    // dispose graphics context
        bImage.flush();                  // flush (empty) image
        bg = null;
    }
   
    abstract protected void offscreenPaint();
   
    public void update() {
        g2d.drawImage(bImage, 0, 0, this)// load buffer to screen
    }
   
    public void setColor(Color clr) {
        if (bg != null) {
            bg.setColor(clr);
        }
    }
   
    public void setFont(Font fnt) {
        if (bg != null) {
            bg.setFont(fnt);
        }
    }
   
    public void setXORMode(Color clr) {
        if (bg != null) {
            bg.setXORMode(clr);
        }
    }
   
    public void drawline(int x1, int y1, int x2, int y2) {
        if (bg != null) {
            bg.drawLine(x1, y1, x2, y2);
        }
    }
   
    public void drawline(int x1, int y1, int x2, int y2, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawLine(x1, y1, x2, y2);
            bg.setColor(currentColor);
        }
    }
   
    public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
        if (bg != null) {
            bg.drawArc(x, y, w, h, startAngle, arcAngle);
        }
    }
   
    public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawArc(x, y, w, h, startAngle, arcAngle);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
        if (bg != null) {
            bg.fillArc(x, y, w, h, startAngle, arcAngle);
        }
    }
   
    public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillArc(x, y, w, h, startAngle, arcAngle);
            bg.setColor(currentColor);
        }
    }
   
    public void drawoval(int x, int y, int w, int h) {
        if (bg != null) {
            bg.drawOval(x, y, w, h);
        }
    }
   
    public void drawoval(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawOval(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfilloval(int x, int y, int w, int h) {
        if (bg != null) {
            bg.fillOval(x, y, w, h);
        }
    }
   
    public void drawfilloval(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillOval(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawroundbox(int x, int y, int w, int h, int arcw, int arch) {
        if (bg != null) {
            bg.drawRoundRect(x, y, w, h, arcw, arch);
        }
    }
   
    public void drawroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawRoundRect(x, y, w, h, arcw, arch);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch) {
        if (bg != null) {
            bg.fillRoundRect(x, y, w, h, arcw, arch);
        }
    }
   
    public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillRoundRect(x, y, w, h, arcw, arch);
            bg.setColor(currentColor);
        }
    }
   
    public void drawbox(int x, int y, int w, int h) {
        if (bg != null) {
            bg.drawRect(x, y, w, h);
        }
    }
   
    public void drawbox(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawRect(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillbox(int x, int y, int w, int h) {
        if (bg != null) {
            bg.fillRect(x, y, w, h);
        }
    }
   
    public void drawfillbox(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillRect(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void draw3Dbox(int x, int y, int w, int h, boolean raised) {
        if (bg != null) {
            bg.draw3DRect(x, y, w, h, raised);
        }
    }
   
    public void draw3Dbox(int x, int y, int w, int h, boolean raised, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.draw3DRect(x, y, w, h, raised);
            bg.setColor(currentColor);
        }
    }
   
    public void drawString(String str, int x, int y) {
        if (bg != null) {
            bg.drawString(str, x, y);
        }
    }
   
    public void delay(int msec) {
        try {
            Thread.sleep(msec);
        } catch (Exception e) {
            System.err.println(e);
        }
    }
   
    public int maxx() {
        return parent.getBounds().width;
    }
   
    public int maxy() {
        return parent.getBounds().height;
    }
}
zylum




PostPosted: Wed Jan 19, 2005 11:21 pm   Post subject: (No subject)

when i try running it, it says no suchMethodError: main

bah! i'm probably being stupid... sorry

code:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Color;



abstract class BufferedCanvas extends Canvas {
   
    private Image bImage;   // offscreen buffer
    private Graphics bg = null;   // offscreen graphics context
    private Graphics2D g2d = null;  // onscreen graphics context
    private int w, h;               // width and height of canvas (Component)
    private Container parent;
   
    BufferedCanvas(Container parent) {
        super();
        setBackground(Color.black);
        this.parent = parent;
    }
   
    public void paint(Graphics g) {
        g2d    = (Graphics2D) g;         // cast to Graphics2D (just in case)
        // create a new buffer image
        bImage = createImage(maxx(), maxy()); 
        bg     = bImage.getGraphics();// get Graphics context for image
       
        offscreenPaint();
       
        bg.dispose();                    // dispose graphics context
        bImage.flush();                  // flush (empty) image
        bg = null;
    }
   
    abstract protected void offscreenPaint();
   
    public void update() {
        g2d.drawImage(bImage, 0, 0, this);  // load buffer to screen
    }
   
    public void setColor(Color clr) {
        if (bg != null) {
            bg.setColor(clr);
        }
    }
   
    public void setFont(Font fnt) {
        if (bg != null) {
            bg.setFont(fnt);
        }
    }
   
    public void setXORMode(Color clr) {
        if (bg != null) {
            bg.setXORMode(clr);
        }
    }
   
    public void drawline(int x1, int y1, int x2, int y2) {
        if (bg != null) {
            bg.drawLine(x1, y1, x2, y2);
        }
    }
   
    public void drawline(int x1, int y1, int x2, int y2, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawLine(x1, y1, x2, y2);
            bg.setColor(currentColor);
        }
    }
   
    public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
        if (bg != null) {
            bg.drawArc(x, y, w, h, startAngle, arcAngle);
        }
    }
   
    public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawArc(x, y, w, h, startAngle, arcAngle);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
        if (bg != null) {
            bg.fillArc(x, y, w, h, startAngle, arcAngle);
        }
    }
   
    public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillArc(x, y, w, h, startAngle, arcAngle);
            bg.setColor(currentColor);
        }
    }
   
    public void drawoval(int x, int y, int w, int h) {
        if (bg != null) {
            bg.drawOval(x, y, w, h);
        }
    }
   
    public void drawoval(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawOval(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfilloval(int x, int y, int w, int h) {
        if (bg != null) {
            bg.fillOval(x, y, w, h);
        }
    }
   
    public void drawfilloval(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillOval(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawroundbox(int x, int y, int w, int h, int arcw, int arch) {
        if (bg != null) {
            bg.drawRoundRect(x, y, w, h, arcw, arch);
        }
    }
   
    public void drawroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawRoundRect(x, y, w, h, arcw, arch);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch) {
        if (bg != null) {
            bg.fillRoundRect(x, y, w, h, arcw, arch);
        }
    }
   
    public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillRoundRect(x, y, w, h, arcw, arch);
            bg.setColor(currentColor);
        }
    }
   
    public void drawbox(int x, int y, int w, int h) {
        if (bg != null) {
            bg.drawRect(x, y, w, h);
        }
    }
   
    public void drawbox(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.drawRect(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void drawfillbox(int x, int y, int w, int h) {
        if (bg != null) {
            bg.fillRect(x, y, w, h);
        }
    }
   
    public void drawfillbox(int x, int y, int w, int h, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.fillRect(x, y, w, h);
            bg.setColor(currentColor);
        }
    }
   
    public void draw3Dbox(int x, int y, int w, int h, boolean raised) {
        if (bg != null) {
            bg.draw3DRect(x, y, w, h, raised);
        }
    }
   
    public void draw3Dbox(int x, int y, int w, int h, boolean raised, Color clr) {
        if (bg != null) {
            Color currentColor = bg.getColor();
            bg.setColor(clr);
            bg.draw3DRect(x, y, w, h, raised);
            bg.setColor(currentColor);
        }
    }
   
    public void drawString(String str, int x, int y) {
        if (bg != null) {
            bg.drawString(str, x, y);
        }
    }
   
    public void delay(int msec) {
        try {
            Thread.sleep(msec);
        } catch (Exception e) {
            System.err.println(e);
        }
    }
   
    public int maxx() {
        return parent.getBounds().width;
    }
   
    public int maxy() {
        return parent.getBounds().height;
    }
}



class DrawView extends BufferedCanvas {
   
    DrawView(JFrame parent) {
        super(parent);
    }
   
    protected void offscreenPaint() {
        setXORMode(Color.white);      // set white as the XORed colour
       
        for (int i = 0; i < maxx(); i++)
        {
            drawoval(i, i % 200, 100, 100);
            update();
            drawoval(i, i % 200, 100, 100);
        }
    }
}


class drawTest {
    public static void main(String[] args) {
        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.getContentPane().add(new DrawView(frame));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
    }
}


p.s. sorry for all the code, i'll delete it when i get the problem solved
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: