简体   繁体   English

Java:Graphics2D

[英]Java: Graphics2D

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

}
}

So I have been trying to solve this problem for 2 hours straight and do not seem to be able to problem solve. 因此,我一直在尝试连续2个小时解决此问题,但似乎无法解决问题。 This "program" is supposed to have an oval and a button come up at the bottom of the screen that allows me to randomize the color of the oval.I am using netbeans and whenever I click run I get this: 这个“程序”应该有一个椭圆形,并且在屏幕底部出现一个按钮,允许我随机化椭圆形的颜色。我正在使用netbeans,每当单击运行时,我都会得到: 截图 Does anyone have any solutions to fix my problems? 有人有解决我问题的解决方案吗? Sorry for wasting your time if it is a stupid question. 如果这是一个愚蠢的问题,很抱歉浪费您的时间。

Spelling matters: paintComponet != paintComponent 拼写很重要: paintComponet != paintComponent

Always preface your overridden methods with @Override . 始终使用@Override覆盖方法的@Override If you'd have done this, you the compiler would warned you about your error. 如果这样做,编译器会警告您有关错误。

So change this: 所以改变这个:

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

to this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM