开发者

Java Graphics Multiple Classes

i have a slight problem. I am trying to write a program that draws a box of crayons. I want to have a method i can call from a main applet that draws a crayon. Currently, my main program looks like this:

package Crayons;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;

@SuppressWarnings("serial")
public class CrayonBox extends JApplet 
{
 public void paint (Graphics page)
 {
  final int CENTER = 250;
  final int SQUARE_SIZE = 31;

  Crayon.drawCrayon(CENTER-5*SQUARE_SIZE, CENTER+5*SQUARE_SIZE, 9*SQUARE_SIZE,Color.red);
  page.set
 }
    }

and my supporting program that has methods to draw the crayon looks like this:

package Crayons;
import java.awt.Color;
import java.awt.Polygon;

@SuppressWarnings("serial")
public class Crayon extends CrayonBox
{


public static void drawCrayon (int x, int y, int height, Color color)
 {
  Polygon crayonTip = new Polygon();
  crayonTip.addPoint(x+15, y);
  crayonTip.addPoint(x+46, y);
  c开发者_C百科rayonTip.addPoint(x+62, y-62);
  crayonTip.addPoint(x+62, y);
  page.setColor(color);
 }
    }

i now need the second program to draw the poylgon i created using

page.drawPolygon(crayonTip);

and change the color using

page.setColor(color);

but it says that page cannot be resolved. so it cannot control the page command. Which is very annoying. Is ther a way around this?

Thanks!


The paint method is passed a Graphics object to handle drawing. You can pass this object to any helper methods by passing the page object to those methods. Your drawCrayon method then becomes:

public static void drawCrayon (int x, int y, int height, Color color, Graphics page)
 {
  ...
  page.setColor(color);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜