该程序非常简单。我正在尝试让3-10张随机面孔在Java框架中的面板上打印。问题是面板上不会打印出面孔。我对此很陌生,所以我不确定自己到底在想什么,而且一段时间以来我一直在努力寻找解决方案。有帮助吗?
//Leonard
//Random Face Drawer(3-10)
//Last Modified: 10/6/18
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
//Main Class
public class FaceDraw {
public static void main(String[] args) {
int width = 900;
int height = 600;
Random rand = new Random();
ArrayList<Face> myFaceList = new ArrayList<Face>();
for( int i = 1; i < (rand.nextInt(8)+3); i ++) {
myFaceList.add(new Face(rand.nextInt(width-50)+50,rand.nextInt(height-50)+50,rand.nextInt(101),rand.nextInt(101)));
System.out.print(myFaceList);
}
FaceFrame myFaceFrame = new FaceFrame(myFaceList, width, height);
myFaceFrame.setVisible(true);
}
}
class OvalDraw extends Oval{
public OvalDraw () {
super(0,0,0,0);
}
public OvalDraw (int positionXIn, int positionYIn, int widthIn, int heightIn) {
super(positionXIn, positionYIn, widthIn, heightIn);
}
public void paintComponent(Graphics g) {
g.drawOval(getPositionX(),getPositionY(),getWidth(),getHeight());
System.out.format("OvalDraw.paintComponent(x = %d, y = %d, w = %d, h = %d)", getPositionX(),getPositionY(),getWidth(),getHeight());
}
}
//Face Class, extends from OvalDraw makes the face
class Face extends OvalDraw {
private OvalDraw eye1;
private OvalDraw eye2;
private Random smile;
private int smileStatus;
public Face () {
super(0, 0, 0, 0);
eye1 = new OvalDraw(0,0,0,0);
eye2 = new OvalDraw(0,0,0,0);
smileStatus = smile.nextInt(2);
}
public Face (int positionXIn, int positionYIn, int widthIn, int heightIn) {
super(positionXIn, positionYIn, widthIn, heightIn);
// variables to set my eyes to be the same size but in two different spots on the same y axis
int eyeHeight = heightIn/12;
int eyeWidth = widthIn/10;
int eye1PositionX = positionXIn + positionXIn/3;
int eyePositionY = positionYIn + positionYIn/12;
int eye2PositionX = eye1PositionX + (positionXIn/3)*2;
eye1 = new OvalDraw(eye1PositionX,eyePositionY,eyeWidth,eyeHeight);
eye2 = new OvalDraw(eye2PositionX,eyePositionY,eyeWidth,eyeHeight);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
eye1.paintComponent(g);
eye2.paintComponent(g);
if(smileStatus == 0) {
g.drawArc(getPositionX(), getPositionY() + getHeight()/2, getWidth(), getHeight(), 45, 90);
}else if(smileStatus == 1){
g.drawArc(getPositionX(), getPositionY() + getHeight()/2, getWidth(), getHeight(), 45, 90);
}else{
g.drawArc(getPositionX(), getPositionY() + getHeight()/2, getWidth(), getHeight(), 45, 90);
}
}
}
class FacePanel extends JPanel{
private ArrayList<Face> FaceList;
public void setFaceList(ArrayList<Face> FaceListIn) {
FaceList = FaceListIn;
}
//draw panel
FacePanel(){
super();
assert false:"unexpected error...(shape draw panel)";
}
FacePanel(ArrayList<Face> FaceListIn){
setFaceList(FaceList);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Face oD : FaceList) {
oD.paintComponent(g);
}
}
}
class FaceFrame extends JFrame{
private FacePanel myFacePanel;
public FaceFrame(ArrayList<Face> faceListIn, int width, int height) {
setBounds(100,100,900,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FacePanel myFacepanel = new FacePanel(faceListIn);
}
}
//Main class for my Shapes
abstract class Shape {
//setting of position
//positioning for x
public final void setPositionX(int positionXIn) {
positionX = positionXIn;
}
public final int getPositionX() {
return positionX;
}
private int positionX;
//positioning for y
public final int getPositionY() {
return positionY;
}
public final void setPositionY(int positionYIn) {
positionY = positionYIn;
}
private int positionY;
//The Width of the Shape
public final void setWidth(int widthIn) {
width = OneOrGreater(widthIn);
}
public final int getWidth() {
return width;
}
private int width;
//The Height of the Shape
public final void setHeight(int heightIn) {
height = OneOrGreater(heightIn);
}
public final int getHeight() {
return height;
}
private int height;
//function for shape
public Shape() {
this(0,0,0,0);
}
public Shape(int positionXIn, int positionYIn, int widthIn, int heightIn) {
setPositionX(positionXIn);
setPositionY(positionYIn);
setWidth(widthIn);
setHeight(heightIn);
}
protected static int OneOrGreater(int valueIn) {
assert valueIn>=1:"Shape parameter is unexeptedly less than 1.";
int returnValue = valueIn;
if (valueIn < 1) {
returnValue = 1;
}
return returnValue;
}
//strings
public String toString() {
return String.format("positionX=%d, positionY=%d, width=%d, height=%d", positionX, positionY, getWidth(), getHeight());
}
abstract public double CalcArea();
abstract public double CalcPerimeter();
}
//The Rectangle class that inherits from Shape
class Rectangle extends Shape {
public Rectangle() {
this(0,0);
}
public Rectangle(int widthIn, int heightIn) {
setWidth(widthIn);
setHeight(heightIn);
}
public Rectangle(int positionXIn, int positionYIn, int widthIn, int heightIn) {
super(positionXIn, positionYIn, widthIn, heightIn);
}
//calculating area for rectangle is base * height
public double CalcArea() {
return getWidth() * getHeight();
}
//calculating rectangle perimeter is 2(width+height)
public double CalcPerimeter() {
return getWidth() + getWidth() + getHeight() + getHeight();
}
}
//Class for Oval that inherits from Shape
class Oval extends Shape {
Oval () {
super();
}
Oval(int positionXIn, int positionYIn, int widthIn, int heightIn) {
super(positionXIn, positionYIn, widthIn, heightIn);
}
//Calculating area of oval with oval formula
public double CalcArea() {
return Math.PI * (getWidth()/2) * (getHeight()/2);
}
//The perimeter of an oval is 2 pi * square root of ((a^2+b^2)/2)
public double CalcPerimeter() {
double a = getWidth() / 2.0;
double b = getHeight() / 2.0;
double perimeter =2*Math.PI*Math.sqrt((Math.pow(a,2)+Math.pow(b,2))/2);
return perimeter;
}
}
答案 0 :(得分:2)
class FaceFrame extends JFrame {
private FacePanel myFacePanel;
public FaceFrame(ArrayList<Face> faceListIn, int width, int height) {
setBounds(100, 100, 900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FacePanel myFacepanel = new FacePanel(faceListIn);
}
}
将myFacepanel
添加到JFrame
可能是一个不错的开始...
public FaceFrame(ArrayList<Face> faceListIn, int width, int height) {
setBounds(100, 100, 900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FacePanel myFacepanel = new FacePanel(faceListIn);
// This might be a good place to start
add(myFacepanel);
}
FacePanel(ArrayList<Face> FaceListIn){
setFaceList(FaceList);
}
您将FaceList
分配给自己(您没有使用FaceListIn
)。
我将摆脱static
并更新代码...
class FacePanel extends JPanel {
private ArrayList<Face> FaceList;
public void setFaceList(ArrayList<Face> FaceListIn) {
FaceList = FaceListIn;
}
//draw panel
FacePanel() {
super();
}
FacePanel(ArrayList<Face> FaceListIn) {
setFaceList(FaceListIn);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Face oD : FaceList) {
oD.paintComponent(g);
}
}
}