Java Tutorial for Beginners - 45 - GUI - Graphics, Colors, and the Draw method

preview_player
Показать описание
In this tutorial we will take a look at custom colors and draw method.
Рекомендации по теме
Комментарии
Автор

Source:

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

public class Colors extends JPanel {
JFrame frame;

Color brightYellow = new Color (240, 253, 2);
Color darkGreen = new Color (28, 89, 71);
    
    public void fireUpScreen () { 
        
    frame = new JFrame ();
    frame.setVisible(true);
    frame.setSize (600, 600);
    frame.add(this);
    }
    
public void paintComponent (Graphics g) {
 
Graphics2D g2d = (Graphics2D) g;

g2d.setColor(darkGreen);
g2d.fillRect(0, 0, 200, 100);
g2d.setColor(brightYellow);
g2d.drawString("This is a test", 5, 10);

}
    public static void main(String[] args) {
        Colors go = new Colors ();
        go.fireUpScreen();
    }

}

EJMedia
Автор

How do you make PanelComponent method work? In my program the method just doesn't get executed...

datsnek
Автор

Hi, so when i finished writing the code, Colors (from the class) was underlined and when I hovered over it, it said this: The serializable class Colors does not declare a static final serialVersionUID field of type long. The program still works, but I was just wondering what it was. Thanks!

brandonlynch
Автор

im a beginner, have a question, why did you cast the Graphics object which is "g" to Graphics2D? cant u just create an object of Graphics2D and calls some methods from it?

edmeralarte
Автор

So by saying ( Graphics2D g2d = (Graphics2D) g), it just means that g2d is a new instance of g?

MrAirPork