我正在尝试创建一个创建正方形的类,可以在我的main函数中调用

时间:2016-08-30 00:52:33

标签: java eclipse graphics jframe awt

我的目的是创建一个可以创建一个正方形的类,并允许我将它放在我的主类(一个带有Canvas和JFrame的双线程类)上,但它似乎不起作用(它什么都不做)。 ..是否有可行的方法来实现它,或者我必须在同一个类中创建它将显示的方块?

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);

}

2 个答案:

答案 0 :(得分:0)

<强> Main.java

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {

    private int AMPLE = 300;
    private int ALTURA = 300;

    private Quadrat quadrat = new Quadrat(0, 0, AMPLE, ALTURA, true, false, 0);

    public static void main(String[] args) {
        Main main = new Main();

        JFrame frame = new JFrame("Main");
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); // maximize window
        frame.setLayout(new BorderLayout());
        frame.add(main, BorderLayout.CENTER);
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        quadrat.paint(g);
    }

}

<强> Quadrat.java

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

public class Quadrat extends Canvas {

    public int x;
    public int y;
    public int status;

    private int totalX;
    private int totalY;

    public boolean isRed;
    public boolean isBlue;

    public Quadrat(int x, int y, int totalX, int totalY, 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, totalY);
    }
}

答案 1 :(得分:0)

<强> Quadrat.java

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

public class Quadrat extends Canvas {

    public int x;
    public int y;
    public int status;

    private int totalX;
    private int totalY;

    public boolean isRed;
    public boolean isBlue;

    public Quadrat(int x, int y, int totalX, int totalY, 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, totalY);
    }
}

MainCanvas.java

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

public class MainCanvas extends Canvas {

    private Quadrat quadrat;

    private int AMPLE = 10;
    private int GRUIX = 300;
    private int ALTURA = 300;

    public MainCanvas() {
        quadrat = new Quadrat(0, 0, 100, 100, true, false, 0);
    }

    public void paint(Graphics g) {
        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);

        quadrat.paint(g);
    }

}

<强> MyPanel.java

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

private MainCanvas mainCanvas;

public static void main(String[] args) {
    MyPanel myPanel = new MyPanel();

    JFrame frame = new JFrame("Main");
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); // maximize window
    frame.setLayout(new BorderLayout());
    frame.add(myPanel, BorderLayout.CENTER);
    frame.setVisible(true);
}

public MyPanel() {
    mainCanvas = new MainCanvas();
}

@Override
protected void paintComponent(Graphics g) {
    mainCanvas.paint(g);
}

}