0

The Problem: I am having trouble with the Graphics class and paint(). Nothing displays when I run this code. I need help understanding the Graphics() object and how to use the paint() method. I don't understand why nothing is showing up here.

My Context: I used to work in Processing, but I haven't coded in a while. Now I'm getting back to it and starting to learn Java, so I'm pretty new and learning as I go.

package colorschemegen;

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class ColorSchemeGen {
    JFrame window= new JFrame("Color Scheme Generator");

    public ColorSchemeGen() {
        window.setTitle("Color Scheme Generator");
        window.setSize(600,600);
        window.setVisible(true);
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g){
        int w = (window.getWidth())/5;
        g.setColor(Color.getHSBColor(100,100,100));
        g.fillRect(100,100,200,200);
    }


   public static void main(String[] args) {
       ColorSchemeGen t= new ColorSchemeGen();
       t.paint(null);
   }

}
Brett
  • 1
  • 1
    Your ColorSchemeGen does not extend any Swing component, and thus your paint method does not override any components intrinsic painting method, and so does nothing. Swing painting is a bit convoluted, and you really must read the tutorials first to understand best practices, including how to override a painting method, which method to override, and such. Please start here: [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – Hovercraft Full Of Eels Feb 09 '17 at 19:47
  • 1
    You're not going about this in the right way. Do the swing tutorial - it will answer a lot of these questions: http://download.oracle.com/javase/tutorial/uiswing/ – ControlAltDel Feb 09 '17 at 19:48
  • 1
    Just quickly -- your class should extend JPanel, you should override paintComponent, you should never call the painting method directly, you should remember to call the super's method in your override... In other words, your code is chock full of errors, meaning (again) that you should first read the tutorials. – Hovercraft Full Of Eels Feb 09 '17 at 19:49
  • 1
    And the JPanel needs to be added to the JFrame, the JFrame needs to be shown, etc... – ControlAltDel Feb 09 '17 at 19:49
  • Yes I had already made all those specific comments before the question was closed. I guess I'll have to start typing 30 character answers and then saving them to make sure I have time to complete my answer. If you have to add all those comments, then the answer isn't a duplicate. – camickr Feb 09 '17 at 19:50
  • @camickr. You do understand that this is not a help site but rather a question and answer site. If you disagree with the closing, then bring this up on the meta site and let your voice and opinion be heard by community at large. – Hovercraft Full Of Eels Feb 09 '17 at 19:51
  • And how can I answer when the question is closed? The link answer is terrible because it just posts code. It doesn't provide a link to the tutorial for a more detailed answer. Why have part of the answer in a comment here and part of the answer somewhere else? – camickr Feb 09 '17 at 19:53
  • 1
    @camickr: Again, if you disagree with the closing, then post a meta question on this. Ours is a fundamental disagreement over how this site works. – Hovercraft Full Of Eels Feb 09 '17 at 19:54

0 Answers0