PDA

View Full Version : java applet drawing


tricolaire
08-18-2006, 06:40 PM
I wrote this simple applet, you can substitute the url image for w/e is most conveniant for you:

//
// image_tracer.java
// image tracer
//
// Created by Erik Schmidt on 8/17/06.
// Copyright (c) 2006 __MyCompanyName__. All rights reserved.
// A simple Java applet
//

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Image;
import java.net.*;
import java.awt.image.ImageObserver;
import java.util.*;

public class image_tracer extends Applet {

private Image image;
private ImageObserver ob;
int[] first,second;

public void init(){

try{

image=getImage(new URL("http://erik-schmidt90s-ibook-g4.local/~erikschmidt90/face.jpg"));

}catch(MalformedURLException ex){
//do nothing
}

setLayout(null);
first=new int[1];
second=new int[1];

first[0]=-1;
second[0]=-1;

}

public void paint(Graphics g){

resize(image.getWidth(ob),image.getHeight(ob));
g.drawImage(image,0,0,null,ob);
g.setColor(Color.black);

for(int i=0;i<first.length;i++){

g.fillOval(first[i],second[i],20,20);

}

}

public boolean mouseMove(Event evt, int x, int y){

int[] i=new int[first.length+1];
int[] j=new int[second.length+2];

int l=0;

for(int k : first){

i[l]=k;

l++;

}

l=0;

for(int k : second){

j[l]=k;
l++;

}

i[first.length]=x;
j[second.length]=y;

first=new int[i.length];
second=new int[j.length];

first=i;
second=j;

repaint();

return true;

}

}

so the problem I'm facing?

the ovals draw mostly correctly, though they're hard to control. sometimes the go in the opposite direction as the mouse, and also, there is this unrelated set of ovals being drawn at the top of the screen. any ideas?

tricolaire
08-19-2006, 04:54 AM
ok, I fixed all that, but I am faced with another problem, how to print out the graphics

I have created this class:

import java.awt.*;
import java.awt.print.*;

public class printer implements Printable {

private Graphics p;

public printer(Graphics g){

p=g;

}

public printer(){

//do nothing

}

void printPage(){

PrinterJob printerJob=PrinterJob.getPrinterJob();
Book book=new Book();
book.append(new printer(),new PageFormat());

printerJob.setPageable(book);

printerJob.printDialog();

try{

printerJob.print();

}catch(Exception ex){
//do nothing
}

}

public int print(Graphics g,PageFormat format,int pageIndex){

g=p;

return Printable.PAGE_EXISTS;

}

}

my run log tells me that there was a security exception at the line that says :

"PrinterJob.getPrinterJob()"

my run log says that I dont have sufficient permissions to do that

how do I get this thing to print!

any help is greatly appreciated

tricolaire
08-19-2006, 05:38 AM
never mind, solved

Aradon
08-20-2006, 03:56 AM
glad we could help...

I think. :P