【问题标题】:I'm trying to create a class that creates a square and could be called in my main function我正在尝试创建一个创建正方形并且可以在我的主函数中调用的类
【发布时间】: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


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢!但这似乎不起作用...您在哪里调用paintComponent方法?
    • paintComponent 被自动调用,因为 Main 扩展了 JPanel。我测试了代码,它正在工作并绘制一个红色方块。
    • 我发布了另一个答案,其中 MainCanvas 扩展了 Canvas 并具有 paint 方法。
    • 有没有办法做同样的事情,但只使用 Canvas?
    • Canvas 代表一个矩形区域,应用程序可以在其中绘制一些东西。您需要一个 JPanel 或 JFrame(带有标题和边框的窗口)来放置画布。
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2021-10-14
    • 2021-10-09
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多