使用 Graphics2D 为 2 个椭圆着色
Coloring 2 Ovals with Graphics2D
我想用 Graphics2D 为 Java 中的 2 个椭圆上色。 Oval1(point
) 绿色和 Oval2(point2
) 蓝色。问题是 Oval2 变为绿色而 Oval1 变为黑色。有人知道问题出在哪里吗?
左(椭圆 1) 右(椭圆 2)
这是 SSCCE:
GameView.java
import javax.swing.JFrame;
import Whosebug.GameView;
public class GameView extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private Game game = new Game();
public GameView(){
JFrame gameFrame = new JFrame("Game");
gameFrame.setSize(500, 300);
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.add(game);
game.runGame();
}
public static void main(String[] args) throws InterruptedException {
new GameView();
}
}
Game.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import Whosebug.Point;
public class Game extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Point point = new Point(Color.GREEN);
private Point point2 = new Point(Color.BLUE);
public void runGame(){
repaint();
point2.setX(300);
}
public void paint(Graphics g) {
point.paint(g);
point2.paint(g);
}
}
Point.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class Point {
private int x = 100;
private int y = 100;
private int width = 10;
private int height = 10;
private Color color;
public Point(Color color){
this.color = color;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
更改这些调用的顺序...
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
你应该在 JPanel.paintComponent
中绘画。
我想用 Graphics2D 为 Java 中的 2 个椭圆上色。 Oval1(point
) 绿色和 Oval2(point2
) 蓝色。问题是 Oval2 变为绿色而 Oval1 变为黑色。有人知道问题出在哪里吗?
这是 SSCCE:
GameView.java
import javax.swing.JFrame;
import Whosebug.GameView;
public class GameView extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private Game game = new Game();
public GameView(){
JFrame gameFrame = new JFrame("Game");
gameFrame.setSize(500, 300);
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.add(game);
game.runGame();
}
public static void main(String[] args) throws InterruptedException {
new GameView();
}
}
Game.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import Whosebug.Point;
public class Game extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Point point = new Point(Color.GREEN);
private Point point2 = new Point(Color.BLUE);
public void runGame(){
repaint();
point2.setX(300);
}
public void paint(Graphics g) {
point.paint(g);
point2.paint(g);
}
}
Point.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class Point {
private int x = 100;
private int y = 100;
private int width = 10;
private int height = 10;
private Color color;
public Point(Color color){
this.color = color;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
更改这些调用的顺序...
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
你应该在 JPanel.paintComponent
中绘画。