custom Graphics2D class

I've made the following code but i'm having some problems with the parameters, can anyone help please!

This is what i have made of the class and main program so far:

Class:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Game

{

JFrame jf;

public void window(int x,int y)

{

jf = new JFrame();

jf.setSize (500, 235);

jf.setLocation(300,200);

jf.setForeground(Color.green);

jf.setTitle ("Roquefort's attempt - <(''<)");

jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Container con = jf.getContentPane();

Color col = new Color (204,230,227);

con.setBackground(col);

jf.setVisible(true);

}

public void painter(Graphics g,int x,int y)

{

g.setColor(Color.red);

g.fillRect(x,y,50,50);

}

}

and the main program

public class UseGame

{

public static void main (String[]args)

{

Game g = new Game();

g.window(800, 600);

g.paint(50,100);

}

}

I would be deeply grateful!

[1137 byte] By [Roqueforta] at [2007-11-27 6:13:37]
# 1

import java.awt.*;

import javax.swing.*;

public class UseGameRx

{

public static void main (String[]args)

{

Game game = new Game();

JFrame jf = new JFrame();

jf.setSize (500, 235);

jf.setLocation(300,200);

jf.setTitle ("Roquefort's attempt - <(''<)");

jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Container con = jf.getContentPane();

con.add(game); // default center section

jf.setVisible(true);

}

}

class Game extends JPanel

{

int x = 100;

int y = 100;

public void paintComponent(Graphics g)

{

super.paintComponent(g); // clears background

g.setColor(Color.red);

g.fillRect(x,y,50,50);

}

}

crwooda at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 2
okay thats kind of what i wanted, but i want to be able to make lots of shapes without making lots of code (i.e. a method) , and i want these shapes to be able to move, hence the parameters for the paint method.
Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 3
and the keeping the original background would be good as well.
Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 4

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class UseGameRx implements ActionListener

{

Game game;

GameAnimator animator;

public void actionPerformed(ActionEvent e)

{

animator.start();

}

private JPanel getGamePanel()

{

game = new Game();

animator = new GameAnimator(game);

return game;

}

private JPanel getControlPanel()

{

JButton start = new JButton("start");

start.addActionListener(this);

JPanel panel = new JPanel();

panel.add(start);

return panel;

}

public static void main (String[]args)

{

UseGameRx app = new UseGameRx();

JFrame jf = new JFrame();

jf.setSize (500, 235);

jf.setLocation(300,200);

jf.setTitle ("Roquefort's attempt - <(''<)");

jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Container con = jf.getContentPane();

con.add(app.getGamePanel()); // default center section

con.add(app.getControlPanel(), "Last");

jf.setVisible(true);

}

}

class Game extends JPanel

{

int x = 100;

int y = 100;

int s = 50;

public Game()

{

Color col = new Color (204,230,227);

setBackground(col);

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(Color.red);

g2.fillRect(x, y, s, s);

}

public void moveToken(int x, int y)

{

this.x = x;

this.y = y;

repaint();

}

}

class GameAnimator implements Runnable

{

Game game;

Thread thread;

boolean animating = false;

int dx = 2;

int dy = 3;

final int DELAY = 100;

public GameAnimator(Game game)

{

this.game = game;

}

public void run()

{

while(animating)

{

try

{

Thread.sleep(DELAY);

}

catch(InterruptedException e)

{

System.out.println("interrupt");

stop();

}

moveAhead();

}

}

private void moveAhead()

{

checkBoundries();

int x = game.x + dx;

int y = game.y + dy;

game.moveToken(x, y);

}

private void checkBoundries()

{

if(game.x + dx < 0 || game.x + game.s + dx > game.getWidth())

dx *= -1;

if(game.y + dy < 0 || game.y + game.s + dy > game.getHeight())

dy *= -1;

}

public void start()

{

if(!animating)

{

animating = true;

thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

}

private void stop()

{

animating = false;

if(thread != null)

thread.interrupt();

thread = null;

}

}

crwooda at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 5

okay dude i'm going to be honest, that code is insane, but sadly it is out of my league, i'm only a Gr11 learner.

I have made a program that had an oval bouncing around a screen, but that was using a method in a supplied class (hsa) which doesn't work with JGrasp.

you had to instantiate a console and then you could use the methods it supplied:

Console c = new Console (something like this)

then you could do this:

c.fillOval(x,y,width,height);

this is more like what i am looking for.

sorry bud but thanks for trying, i should have been more specific

Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 6
do you know how to make a painting method like c.fillRect(x,y,width,height) ?i mean the program that you made is kinda what i want to have as a final result but i just want to make it simpler.
Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 7
do you know how to make a painting method like c.fillRect(x,y,width,height) ?Probably. What is c. Where do you want the method (which class) and where do you want to call it from?
crwooda at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 8

I'm so sorry that I didn't reply sooner, I haven't been able to get onto the net until now. You raised the following questions:

Q.)What is c

A.)'c' is the constructor, like if i make a class called Window i can do this in the main method 'Window w = new Window();' and to call the methods in the class all i would have to do is 'w.methodName();'.

Q.)Where do you want the method (which class)

A.)Not sure. Maybe something like 'Shapes'. But that can always be changed.

Q.)where do you want to call it from?

A.)Not quite sure what you mean, umm whatever is best.

If you are willing to do this for me, and I know beggars can't be choosers, could you maybe add a method that allows me to change the colours of a shape?, and if it is drawn or filled?

Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...
# 9
>where do you want to call it from?i want to be able to call the methods in the class from a main program, separate to that of the class file. If that is what you mean. otherwise not quite sure what you on about.
Roqueforta at 2007-7-12 17:22:19 > top of Java-index,Security,Cryptography...