1

I tried to run this code to make an image using JFrame

Main class

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

public class main {

    public static void main(String[] args){
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("kek");

        f.add (new GraphicsSurface());

        f.setSize(512, 512);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

GraphicsSurface class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class GraphicsSurface extends JComponent {

    public GraphicsSurface() {
    }

    public void paint (Graphics g) {
        int end = 0;
        long redint = 0, greenint = 0, blueint = 0;
        int xstart = 0, ystart = 0, xend = 0, yend = 0;
        double y = 0, x = 0;
        double width = 80;
        double angle = 0;
        double endpoint = 255;
        double curveintensitya = 1.4, curveintensityb = -0.9;
        double curvestarta = 30, curvestartb = 150;
        double curvelengtha = 15, curvelengthb = 30;
        double redvalue = 0, greenvalue = 0, bluevalue = 0;
        while (end == 0) {          

            Graphics2D g2d = (Graphics2D) g;            
            redvalue = 100 + (y / 2.56);
            greenvalue = 50 + (y / 1.28);
            bluevalue = 200 + (y / 5.12);
            redint = Math.round(redvalue);
            greenint = Math.round(greenvalue);
            blueint = Math.round(bluevalue);

            if (y >= curvestarta && y < (curvestarta + curvelengtha)) {
                angle = angle + (curveintensitya / curvelengtha);
            }
            if (y >= curvestartb && y < (curvestartb + curvelengthb)) {
                angle = angle + (curveintensityb / curvelengthb);
            }

            width = width + angle;
            if (width > 512) {
                width = 512;
            }
            x = Math.round(width);
            xend = (int) x;
            ystart = (int) y;
            yend = ystart;
            y = y + 1;

            if (y > endpoint) {
                end = 1;
            }

            g2d.setColor(new Color(redint, greenint, blueint, 255));

            g2d.drawLine(xstart, ystart, xend, yend);
        }
    }
}

But I got this in the console:

at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I'd like to know what caused the problem and what I should do to fix it.

1
  • Off-topic: 1) You shouldn't override paint() method but paintComponent() instead. See A Closer Look at the Paint Mechanism. 2) Instead of setting frame's preferred size give your component a preferred size by overriding getPreferredSize() method. 3) Swing components must be created/updated in the Event Dispatch Thread. See Initial Threads. Commented Nov 12, 2014 at 23:01

1 Answer 1

2

The issue is with this line

g2d.setColor(new Color(redint, greenint, blueint, 255));

The type of redint, greenint and blueint in your code is of type long. But the constructor for Color takes in either int or long.

You can refer to the documentation here

So the fix to your problem: change these types from long to int and do appropriate casting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.