【发布时间】:2017-01-06 03:16:32
【问题描述】:
我的意图是创建一个可以创建正方形的类,并允许我将它放在我的主类(带有 Canvas 和 JFrame 的 2 线程类)上,但它似乎不起作用(它什么都不做)。 .. 是否有可行的方法来实现它,或者我必须在显示它的同一个类中创建正方形?
PD:对不起我的英语不好
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class Quadrat extends Canvas{
public static int x;
public static int y;
public static int status;
private static int totalX;
private static int totalY;
public static boolean isRed;
public static boolean isBlue;
public Quadrat(int x, int y, int ampleTotal, int totalX, boolean isRed, boolean isBlue, int status) {
this.x = x;
this.y = y;
this.totalX = totalX;
this.totalY = totalY;
this.isRed = isRed;
this.isBlue = isBlue;
this.status = status;
}
public void paint(Graphics g) {
if (isRed) {
g.setColor(Color.RED);
}
else {
g.setColor(Color.BLUE);
}
g.fillRect(x, y, totalX/3 , totalY/3);
}
public static void main() {
System.out.println("Is working");
}
}
这是我在主类中初始化它的地方:
public void paint(Graphics g) {
/*
* Border
*/
g.setColor(Color.BLACK);
g.fillRect(AMPLE / 3, 0, GRUIX, ALTURA);
g.fillRect((AMPLE / 3) * 2, 0, GRUIX, ALTURA);
g.fillRect(0, ALTURA / 3, AMPLE, GRUIX);
g.fillRect(0, (ALTURA / 3) * 2, AMPLE, GRUIX);
/*
* Square
*/
Quadrat quadrat = new Quadrat(0, 0, AMPLE, ALTURA, true, false, 0);
}
【问题讨论】:
-
你为什么把所有东西都做了
static? -
能否贴出主类的完整代码?
-
主类它只是几个方法(其中一个paint())并扩展了画布
-
那么,你使用的主要方法是什么?
标签: java eclipse graphics jframe awt