首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java: Graphics2D

Java: Graphics2D
EN

Stack Overflow用户
提问于 2016-09-06 10:40:02
回答 1查看 258关注 0票数 2
代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SimpleGui3C implements ActionListener {

JFrame frame;

public static void main(String[] args) {

    SimpleGui3C gui = new SimpleGui3C();
    gui.go();
}

public void go() {

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("Change Colors");
    button.addActionListener(this);

    MyDrawPanel drawPanel = new MyDrawPanel();

    frame.getContentPane().add(BorderLayout.SOUTH, button);
    frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    frame.setSize(300, 300);
    frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
    frame.repaint();
}
}

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

class MyDrawPanel extends JPanel {

public void paintComponet(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    int red = (int) (Math.random() * 256);
    int green = (int) (Math.random() * 256);
    int blue = (int) (Math.random() * 256);
    Color startColor = new Color(red, green, blue);

    red = (int) (Math.random() * 256);
    green = (int) (Math.random() * 256);
    blue = (int) (Math.random() * 256);
    Color endColor = new Color(red, green, blue);

    GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70, 70, 100, 100);

}
}

所以我已经连续两个小时尝试解决这个问题,但似乎无法解决问题。这个“程序”应该有一个椭圆形,并在屏幕底部出现一个按钮,允许我随机化椭圆形的颜色。我正在使用netbeans,每当我单击run时,我都会得到这样的结果:

有没有人能解决我的问题?如果这是一个愚蠢的问题,很抱歉浪费您的时间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-06 10:59:55

拼写很重要:paintComponet != paintComponent

总是在被覆盖的方法前面加上@Override。如果你这样做了,你的编译器会警告你你的错误。

因此,请更改以下内容:

代码语言:javascript
复制
public void paintComponet(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    int red = (int) (Math.random() * 256);
    int green = (int) (Math.random() * 256);
    int blue = (int) (Math.random() * 256);
    Color startColor = new Color(red, green, blue);

    red = (int) (Math.random() * 256);
    green = (int) (Math.random() * 256);
    blue = (int) (Math.random() * 256);
    Color endColor = new Color(red, green, blue);

    GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70, 70, 100, 100);
}

要这样做:

代码语言:javascript
复制
@Override // don't forget this
protected void paintComponent(Graphics g) { // spelling matters. Also make it protected

    // !!!! don't forget this!
    super.paintComponent(g); // to have the JPanel do housekeeping painting

    Graphics2D g2d = (Graphics2D) g;

    int red = (int) (Math.random() * 256);
    int green = (int) (Math.random() * 256);
    int blue = (int) (Math.random() * 256);
    Color startColor = new Color(red, green, blue);

    red = (int) (Math.random() * 256);
    green = (int) (Math.random() * 256);
    blue = (int) (Math.random() * 256);
    Color endColor = new Color(red, green, blue);

    GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70, 70, 100, 100);
}    
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39339860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档