为什么在参数正确的情况下绘制方法报错?

huangapple go评论80阅读模式
英文:

Why do i get an an error on paint method when the parameters are correct?

问题

import javax.swing.*;
import java.awt.*;

class Main extends JFrame {

    class App extends JPanel {

        Stage stage;

        public App() {
            setPreferredSize(new Dimension(900, 720));
            stage = new Stage();
        }

        @Override
        public void paint(Graphics g) {
            stage.paint(g, getMousePosition());
        }
    }

    public static void main(String[] args) throws Exception {
        Main window = new Main();
        window.run();
    }

    private Main() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        App canvas = new App();
        this.setContentPane(canvas);
        this.pack();
        this.setVisible(true);
    }

    public void run() {
        while (true) {
            this.repaint();
        }
    }
}

import java.awt.*;
import java.util.Random;

public class Stage {

    Grid grid;
    Terrain sand;
    Terrain oasis;
    Terrain palm_tree;
    Terrain wall;
    Random RandomNo = new Random();
    int number;

    public Stage() {
        grid = new Grid();
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                number = 1 + RandomNo.nextInt(10);
                if (number <= 4) {
                    sand = new Sand(grid.cellAtColRow(i, j));
                } else if (number == 1) {
                    oasis = new Oasis(grid.cellAtColRow(i, j));
                } else if (number == 2) {
                    wall = new Wall(grid.cellAtColRow(i, j));
                } else if (number == 3) {
                    palm_tree = new PalmTree(grid.cellAtColRow(i, j));
                }
            }
        }
    }

    public void paint(Graphics g, Point mouseLoc) {
        grid.paint(g, mouseLoc);
        sand.paint(g);
        oasis.paint(g);
        wall.paint(g);
        palm_tree.paint(g);
    }
}

import java.awt.*;

class Grid {
    //fields
    Cell[][] cells = new Cell[20][20];

    // constructor
    public Grid() {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = new Cell(10 + 35 * i, 10 + 35 * j);
            }
        }
    }

    // methods
    public void paint(Graphics g, Point mousePos) {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j].paint(g, mousePos);
            }
        }
    }

    public Cell cellAtColRow(int c, int r) {
        return cells[c][r];
    }
}

import java.awt.*;

class Cell extends Rectangle {
    // fields

    static int size = 35;

    //constructors
    public Cell(int x, int y) {
        super(x, y, size, size);
    }

    //methods
    void paint(Graphics g, Point mousePos) {
        if (contains(mousePos)) {
            g.setColor(Color.GRAY);
        } else {
            g.setColor(Color.WHITE);
        }
        g.fillRect(x, y, size, size);
        g.setColor(Color.BLACK);
        g.drawRect(x, y, size, size);
    }

    public boolean contains(Point p) {
        if (p != null) {
            return super.contains(p);
        } else {
            return false;
        }
    }
}
英文:

I am facing an issue with the paint method in the below program. The error "void Stage.paint(Graphic g, Point mouseloc)" and the error "paint cannot be resolved to a type". I have inputted the correct parameters and have set the fields and constructors properly. The error are in italics.Sorry i am new to coding and hopefully you can help me

import javax.swing.*;
import java.awt.*;
class Main extends JFrame {
class App extends JPanel {
Stage stage;
public App() {
setPreferredSize(new Dimension(900, 720));
stage = new Stage();
}
@Override
public void paint(Graphics g) {
*stage.paint(g, getMousePosition());*
}
}
public static void main(String[] args) throws Exception {
Main window = new Main();
window.run();
}
private Main() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App canvas = new App();
this.setContentPane(canvas);
this.pack();
this.setVisible(true);
}
public void run() {
while (true) {
this.repaint();  
}
}
}
import java.awt.*;
import java.util.Random;
public class Stage {
Grid grid;
Terrain sand;
Terrain oasis;
Terrain palm_tree;
Terrain wall;
Random RandomNo = new Random();
int number;
public Stage() {
grid = new Grid();
for(int i = 0;i&lt;20;i++)
{
for (int j = 0; j &lt; 20; j++) {
number = 1 + RandomNo.nextInt(10);
if (number &lt;= 4) {
sand = new Sand(grid.cellAtColRow(i, j));
} else if (number == 1) {
oasis = new Oasis(grid.cellAtColRow(i, j));
} else if (number == 2) {
wall = new Wall(grid.cellAtColRow(i, j));
} else if (number == 3) {
palm_tree = new PalmTree(grid.cellAtColRow(i, j));
}
}
}
*public paint(Graphics g, Point mouseLoc){*
grid.paint(g, mouseLoc);
sand.paint(g);
oasis.paint(g);
wall.paint(g);
palm_tree.paint(g);
}
}
}
import java.awt.*;
class Grid {
//fields
Cell[][] cells = new Cell[20][20];
// constructor
public Grid(){
for(int i = 0; i &lt; cells.length; i++){
for(int j = 0; j &lt; cells[i].length; j++){
cells[i][j] = new Cell(10+35*i,10+35*j);
}
}
}
// methods
public void paint(Graphics g, Point mousePos){
for(int i = 0; i &lt; cells.length; i++){
for(int j = 0; j &lt; cells[i].length; j++){
cells[i][j].paint(g, mousePos);
}
}
}
public Cell cellAtColRow(int c, int r){
return cells[c][r];
}
}
import java.awt.*;
class Cell extends Rectangle {
// fields
static int size = 35;
//constructors
public Cell(int x, int y){
super(x,y,size,size);
}
//methods
void paint(Graphics g, Point mousePos){      
if(contains(mousePos)){
g.setColor(Color.GRAY);
} 
else {
g.setColor(Color.WHITE);
}
g.fillRect(x,y,size,size);
g.setColor(Color.BLACK);
g.drawRect(x,y,size,size);
}
public boolean contains(Point p){
if (p != null){
return super.contains(p);
} else {
return false;
}
}
}

答案1

得分: 0

第一个问题...

首先,public paint(Graphics g, Point mouseLoc){ 不是一个有效的声明,你缺少了返回类型。

应该是 public void paint(Graphics g, Point mouseLoc) {

第二个问题,你有一个放置错误的 },这意味着 paint 实际上被声明在构造函数内部...

public Stage() {
    grid = new Grid();
    for(int i = 0;i<20;i++)
    {
        for (int j = 0; j < 20; j++) {
            number = 1 + RandomNo.nextInt(10);
            if (number <= 4) {
                sand = new Sand(grid.cellAtColRow(i, j));
            } else if (number == 1) {
                oasis = new Oasis(grid.cellAtColRow(i, j));
            } else if (number == 2) {
                wall = new Wall(grid.cellAtColRow(i, j));
            } else if (number == 3) {
                palm_tree = new PalmTree(grid.cellAtColRow(i, j));
            }

        }
    }

    public paint(Graphics g, Point mouseLoc){
        grid.paint(g, mouseLoc);
        sand.paint(g);
        oasis.paint(g);
        wall.paint(g);
        palm_tree.paint(g);

    }
}

应该更像是...

public Stage() {
    grid = new Grid();
    for(int i = 0;i<20;i++)
    {
        for (int j = 0; j < 20; j++) {
            number = 1 + RandomNo.nextInt(10);
            if (number <= 4) {
                sand = new Sand(grid.cellAtColRow(i, j));
            } else if (number == 1) {
                oasis = new Oasis(grid.cellAtColRow(i, j));
            } else if (number == 2) {
                wall = new Wall(grid.cellAtColRow(i, j));
            } else if (number == 3) {
                palm_tree = new PalmTree(grid.cellAtColRow(i, j));
            }

        }
    }
}

public void paint(Graphics g, Point mouseLoc){
    grid.paint(g, mouseLoc);
    sand.paint(g);
    oasis.paint(g);
    wall.paint(g);
    palm_tree.paint(g);
}
英文:

Two problems...

First public paint(Graphics g, Point mouseLoc){ is not a valid declaration, you're missing the return type.

It should be public void paint(Graphics g, Point mouseLoc) {

Second, you have a misplaced }, which means that paint is actually been declared inside the constructor...

public Stage() {
grid = new Grid();
for(int i = 0;i&lt;20;i++)
{
for (int j = 0; j &lt; 20; j++) {
number = 1 + RandomNo.nextInt(10);
if (number &lt;= 4) {
sand = new Sand(grid.cellAtColRow(i, j));
} else if (number == 1) {
oasis = new Oasis(grid.cellAtColRow(i, j));
} else if (number == 2) {
wall = new Wall(grid.cellAtColRow(i, j));
} else if (number == 3) {
palm_tree = new PalmTree(grid.cellAtColRow(i, j));
}
}
}
public paint(Graphics g, Point mouseLoc){
grid.paint(g, mouseLoc);
sand.paint(g);
oasis.paint(g);
wall.paint(g);
palm_tree.paint(g);
}
}

It should be more like...

public Stage() {
grid = new Grid();
for(int i = 0;i&lt;20;i++)
{
for (int j = 0; j &lt; 20; j++) {
number = 1 + RandomNo.nextInt(10);
if (number &lt;= 4) {
sand = new Sand(grid.cellAtColRow(i, j));
} else if (number == 1) {
oasis = new Oasis(grid.cellAtColRow(i, j));
} else if (number == 2) {
wall = new Wall(grid.cellAtColRow(i, j));
} else if (number == 3) {
palm_tree = new PalmTree(grid.cellAtColRow(i, j));
}
}
}
}
public void paint(Graphics g, Point mouseLoc){
grid.paint(g, mouseLoc);
sand.paint(g);
oasis.paint(g);
wall.paint(g);
palm_tree.paint(g);
}

huangapple
  • 本文由 发表于 2020年8月30日 09:43:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定