What I should get in the end is a grid with squares that have stable colors if I resize the window. So far I get a grid that has random colors but as the everything is redrawn the colors are too. I'm thinking, maybe an array that stores the colors could work but I don't really know how to implement it in what I have so far.
public class GridRandomColors extends JFrame {
private static class Board extends JPanel {
private Rectangle MAIN_RECT;
private double BRICK_WIDTH, BRICK_HEIGHT;
private int COLS = 8;
private int ROWS = 8;
public Board() {
setBackground(Color.gray);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawRectangle(g);
drawBricks(g);
}
private void drawRectangle(Graphics g) {
if (getHeight() > getWidth()) {
MAIN_RECT = new Rectangle(0, 0, getWidth(), getWidth());
g.fillRect(0, (getHeight()-getWidth())/2, getWidth(), getWidth());
x = 0;
y = (getHeight()-getWidth())/2;
} else {
x = (getWidth()-getHeight())/2;
y = 0;
MAIN_RECT = new Rectangle(0, 0, getHeight(), getHeight());
g.fillRect((getWidth()-getHeight())/2, 0, getHeight(), getHeight());
}
BRICK_WIDTH = (float) MAIN_RECT.getWidth() / COLS;
BRICK_HEIGHT = (float) MAIN_RECT.getHeight() / ROWS ;
}
double spacing = 0.2;
private double x;
private double y;
private Color color;
private void drawBricks(Graphics g) {
Graphics2D brick = (Graphics2D) g.create();
for (int j = 0; j < ROWS; j++) {
for (int a = 0; a < COLS; a++) {
Random rand = new Random();
color = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
Color oldColor = g.getColor();
brick.setColor(color);
Rectangle2D.Double rect = new Rectangle2D.Double(x, y, BRICK_WIDTH - spacing*(COLS-1), BRICK_HEIGHT- spacing*(ROWS-1));
brick.fill(rect);
brick.setColor(oldColor);
x += BRICK_HEIGHT+spacing;
}
if (getHeight() > getWidth()) {
x = 0;
}
else {
x = (getWidth() - getHeight()) / 2;
}
y += BRICK_HEIGHT+spacing;
}
}
}
public GridRandomColors() {
setDefaultCloseOperation(EXIT_ON_CLOSE); //mai bine cu exit on close
setSize(800, 820);
add(new Board());
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new GridRandomColors().setVisible(true);
}
});
}
}
You can use a simple two dimensional array to store the colors and generate it in your constructor:
private static class Board extends JPanel {
// ...
private int COLS = 8;
private int ROWS = 8;
private Color[][] colors;
public Board() {
// ...
Random rand = new Random();
colors = new Color[ROWS][COLS];
for (int a = 0; a < ROWS; a++) {
for (int b = 0; b < COLS; b++) {
colors[a][b] = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
}
}
}
private void drawBricks(Graphics g) {
// ...
for (int j = 0; j < ROWS; j++) {
for (int a = 0; a < COLS; a++) {
Color oldColor = g.getColor();
brick.setColor(colors[j][a]);
// ...
}
// ...
}
}
}
Related
I am having trouble replicating the picture for my assignment. I would appreciate any tips or solutions. I believe I have the general idea down, but I am having a hard time figuring out the math to replicate the image and doing everything in a single loop.
My program needs to meet these criteria:
Match the Image
Generate a random color for each pair of parabolic curve
Work with any width or height
Use a single loop to draw the entire figure.
Here is the image:
This is what I have tried so far
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static final int LINE_INCREMENT = 5;
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT);
Graphics g = panel.getGraphics();
int d = 0;
int iterations = HEIGHT/LINE_INCREMENT;
Random rand = new Random();
int red = 0, green = 0, blue = 0;
red = rand.nextInt(128) + 128;
green = rand.nextInt(128) + 128;
blue = rand.nextInt(128) + 128;
g.setColor(new Color(red,green,blue));
for(int y = 0; y < iterations; y++) {
g.drawLine(0, d, d, HEIGHT);
g.drawLine(WIDTH, d, d, 0);
d += LINE_INCREMENT;
}
red = rand.nextInt(128) + 128;
green = rand.nextInt(128) + 128;
blue = rand.nextInt(128) + 128;
g.setColor(new Color(red,green,blue));
d = 0;
for (int x = 0; x < iterations/2; x++) {
g.drawLine(WIDTH/4, d + HEIGHT/4, d + WIDTH/4, HEIGHT - HEIGHT/4);
g.drawLine(d + WIDTH/4, WIDTH/4, WIDTH - WIDTH/4, d + WIDTH/4);
d += LINE_INCREMENT;
}
}
The output:
The way to implement it is by overriding paintComponent:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ParabolicCurves extends JFrame {
private static final int SIZE = 600;
private static final int LINE_INCREMENT = 5;
private static final int NUM_OF_PATTERNS = 4;
private Random rand = new Random();
ParabolicCurves() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel panel = new DrawingPanel(SIZE);
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
class DrawingPanel extends JPanel{
public DrawingPanel(int size) {
setPreferredSize(new Dimension(size, size));
setBackground(Color.WHITE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int red, green, blue, delta , iterations;
int height, width ,startX, startY, endX, endY ;
Rectangle boundingRrectangle = getBounds();
for(int pattern = 0 ; pattern < NUM_OF_PATTERNS; pattern++) {
red = rand.nextInt(128) + 128;
green = rand.nextInt(128) + 128;
blue = rand.nextInt(128) + 128;
g.setColor(new Color(red,green,blue));
height = (int) boundingRrectangle.getHeight();
width = (int) boundingRrectangle.getWidth();
startX = (int) boundingRrectangle.getX();
startY = (int) boundingRrectangle.getY();
endX = startX+width;
endY = startY+ height;
iterations = Math.min(width, height)/LINE_INCREMENT;
delta = 0;
for (int x = 0; x < iterations ; x++) {
g.drawLine(startX, startY+delta, startX+delta, endY);
g.drawLine(endX, startY+delta, startX+delta, startY);
delta += LINE_INCREMENT;
}
//change bounding rectangle
boundingRrectangle = new Rectangle(startX+(width/4),
startY+(width/4), width/2, height/2);
}
}
}
public static void main(String[] args) {
new ParabolicCurves();
}
}
Output:
Within my program, I have the following code:
package io.github.AdmiralSbs.DiceWars;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Scanner;
public class HexDisplay extends JPanel {
private static final long serialVersionUID = 1L;
public static final int SIZE = 200;
private int height;
private int width;
private Hex[][] hex;
private BufferedImage myImage;
private Graphics drawer;
public HexDisplay(File f) throws IOException {
Scanner key = new Scanner(f);
int[] temp = commaSplit(key.nextLine());
height = temp[0];
width = temp[1];
hex = new Hex[width][height];
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
//temp = commaSplit(key.nextLine());
if (h % 2 == 0)
hex[w][h] = new Hex((int) (SIZE * (w + 0.5)),
(int) (SIZE * (h + 0.5)), SIZE);
else
hex[w][h] = new Hex((int) (SIZE * (w + 1.5)),
(int) (SIZE * (h + 0.5)), SIZE);
System.out.println(hex[w][h].getX() + " " + hex[w][h].getY());
}
}
key.close();
starting();
ImageIO.write(myImage, "jpg", new File("outPic.jpg"));
}
public void starting() {
setPreferredSize(new Dimension(400,400));
setLayout(new FlowLayout());
myImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
drawer = myImage.getGraphics();
drawer.setColor(Color.BLUE);
drawer.fillRect(0, 0, 400, 400);
drawStuff(drawer);
repaint();
}
public int[] commaSplit(String s) {
String[] str = s.split(",");
int[] ret = new int[str.length];
for (int i = 0; i < str.length; i++) {
ret[i] = Integer.parseInt(str[i]);
}
return ret;
}
public void paintComponents(Graphics g) {
g.drawImage(myImage, 0, 0, this);
System.out.println("Painted");
}
public void drawStuff(Graphics g) {
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
hex[w][h].draw(g);
}
}
System.out.println("Drew");
revalidate();
repaint();
paintComponents(g);
}
}
What I would expect to happen is for the frame that contains this JPanel to display the image, but it does not. All System.out.println() methods are called as expected, and I am able to save the image as a .jpg file. However, the image is not displayed in the GUI. What can be done?
You have a typo:
// v
public void paintComponents(Graphics g) {
It should be paintComponent.
And should contain a call to super method:
super.paintComponent(g);
as the first line in the method
so i have a little Problem and i try to solve it since hours.
I have a BufferedImage and i want to change the colour in a fluent way f.e. from red to white.
My main:
public static void main(String[] args) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x != width; x++) {
for (int y = 0; y != height; y++) {
image.setRGB(x, y, color12(x, y));
try {
,,,
My method to change the color:
static int color12(int x, int y) {
int size = 100;
if (Math.abs(width / 2 - x) < size / 2 && Math.abs(height / 2 - y) < size / 2)
return new Color(255, 0, 0).getRGB();
else
return new Color(200, 200, 255).getRGB();
}
}
So i played around with the method, but i cant get it done.
My best "solution" was this:
int r = 0 ;
int b = 0;
int g = 0;
for (int i = 1; i < 255; i++)
r++;
else return new Color(r,g,b).getRGB();
Can anyone help me?
I'm not sure how you want the gradient (e.g. horizontal, vertical or diagonal), but here's an example using linear interpolation for a horizontal or vertical gradient.
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ExampleFrame extends JFrame {
private static enum GradientOrientation {
HORIZONTAL, VERTICAL
};
private static BufferedImage createGradientImg(int width, int height, Color startColor, Color endColor, GradientOrientation o) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int pos = o.equals(GradientOrientation.HORIZONTAL) ? x : y;
int size = o.equals(GradientOrientation.HORIZONTAL) ? width : height;
image.setRGB(x, y, getGradientRGB(startColor, endColor, pos, size));
}
}
return image;
}
private static int getGradientRGB(Color startColor, Color endColor, int pos, int size) {
double perc = (double) pos / size;
int newRed = (int) (endColor.getRed() * perc + startColor.getRed() * (1 - perc));
int newGreen = (int) (endColor.getGreen() * perc + startColor.getGreen() * (1 - perc));
int newBlue = (int) (endColor.getBlue() * perc + startColor.getBlue() * (1 - perc));
return new Color(newRed, newGreen, newBlue).getRGB();
}
public void createAndShow() {
BufferedImage img1 = createGradientImg(200, 100, Color.RED,
Color.WHITE, GradientOrientation.HORIZONTAL);
BufferedImage img2 = createGradientImg(200, 100, Color.BLUE,
Color.YELLOW, GradientOrientation.HORIZONTAL);
BufferedImage img3 = createGradientImg(200, 100, Color.WHITE,
Color.YELLOW, GradientOrientation.VERTICAL);
BufferedImage img4 = createGradientImg(200, 100, Color.BLACK,
Color.WHITE, GradientOrientation.VERTICAL);
BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
getContentPane().setLayout(layout);
getContentPane().add(new JLabel(new ImageIcon(img1)));
getContentPane().add(new JLabel(new ImageIcon(img2)));
getContentPane().add(new JLabel(new ImageIcon(img3)));
getContentPane().add(new JLabel(new ImageIcon(img4)));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ExampleFrame ef = new ExampleFrame();
ef.createAndShow();
}
});
}
}
I'm trying to write an algorithm to satisfy this challenge. I've double, triple, and quadruple checked my logic, but I think I'm missing something obvious. This program should group each color next to similar colors, but it produces something more akin to noise.
This is sort of what I expect (taken from a similar answer):
And this is what I'm actually getting:
public class AllColors extends JFrame {
private static final int WIDTH = 256;
private static final int HEIGHT = 128;
private static long TOTAL_ITERATIONS = (WIDTH * HEIGHT) * 185000;
private static int VALUES_PER_CHANNEL =32;
private static int CHANNEL_DELTA = 256/VALUES_PER_CHANNEL;
static BufferedImage image;
private static final int SCALE = 5;
static int[][] kernel = { { 0, 0, 1, 0, 0 },
{ 0, 2, 3, 2, 0 },
{ 1, 3, 0, 3, 1 },
{ 0, 2, 3, 2, 0 },
{ 0, 0, 1, 0, 0 } };
public static void main(String[] args) {
AllColors f = new AllColors();
f.setTitle("All Colors");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
image = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_3BYTE_BGR);
init();
//gui stuff
JPanel p = new JPanel(){
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.scale(SCALE, SCALE);
g2.drawImage(image, 0, 0, null);
}
};
p.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
f.add(p);
f.pack();
f.setVisible(true);
group(p);
}
//makes an image of all colors
private static void init() {
int x = 0;
int y = 0;
for(int r = 0; r < VALUES_PER_CHANNEL; r+= 1){
for(int g = 0; g < VALUES_PER_CHANNEL; g+= 1){
for(int b = 0; b < VALUES_PER_CHANNEL; b+= 1){
x++;
if(x % WIDTH == 0){
y++;
x = 0;
}
if(y >= HEIGHT)
return;
image.setRGB(x, y, new Color(r*CHANNEL_DELTA,g*CHANNEL_DELTA,b*CHANNEL_DELTA).getRGB());
}
}
}
}
//group together similar pixels
private static void group(JPanel panel){
Random rand = new Random();
for(long i = 0; i < TOTAL_ITERATIONS; i++){
Point first = new Point(rand.nextInt(WIDTH), rand.nextInt(HEIGHT));
Point second = new Point(rand.nextInt(WIDTH), rand.nextInt(HEIGHT));
trySwitch(first, second);
if(i % (WIDTH * HEIGHT) == 0){
System.out.println(i / (WIDTH * HEIGHT));
panel.repaint();
}
}
}
private static void swap(Point first, Point second){
int temp = image.getRGB(second.x, second.y);
image.setRGB(second.x, second.y, image.getRGB(first.x, first.y));
image.setRGB(first.x, first.y, temp);
}
//get how similar the neighbors are
private static int getNeighborDelta(int imageX, int imageY){
Color center = new Color(image.getRGB(imageX, imageY));
int sum = 0;
for (int x = 0; x < kernel[0].length; x++)
{
for (int y = 0; y < kernel.length; y++)
{
int weight = kernel[x][y];
if (weight <= 0)
{
continue;
}
int xOffset = x - (kernel[0].length / 2);
int yOffset = y - (kernel.length / 2);
try{
sum += getDistance(new Color(image.getRGB(imageX + xOffset, imageY + yOffset)), center) * weight;
}catch(ArrayIndexOutOfBoundsException e){
//if out of image
}
}
}
return sum;
}
//switches if the neighbors will be more similar
private static void trySwitch(Point first, Point second){
double firstDistance = getNeighborDelta(first.x, first.y);
swap(first, second);
double secondDistance = getNeighborDelta(first.x, first.y);
if(secondDistance > firstDistance)
swap(first, second);
}
//get similarity between colors
private static double getDistance(Color one, Color two){
int r = Math.abs(two.getRed() - one.getRed());
int g = Math.abs(two.getGreen() - one.getGreen());
int b = Math.abs(two.getBlue() - one.getBlue());
return r + g + b;
}
}
I'm designing a strip chart, but it isn't displaying. Its encapsulating JFrame is displaying, and paintComponent() is being called, but no gray grid lines are appearing. Why is this?
class ChartArea extends JPanel {
private int Yscl, Xscl;
private static final Color BACKGROUND_COLOR = Color.BLACK;
private static final Color LINE_COLOR = Color.GREEN;
private static final Color GRIDLINE_COLOR = Color.GRAY;
ChartArea() {
setBackground(BACKGROUND_COLOR);
setForeground(LINE_COLOR);
Yscl = 20;
Xscl = 20;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int height = getHeight();
int width = getWidth();
double max = data.getMax(); //gets the maximum value in the data set
try {
g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
//put the origin in the bottom right. Positive is up and left
g2d.translate(width, height);
g2d.scale(-1d, -1d);
//if the data set exceeds the window height, scale it down
if (max > height) {
g2d.scale(1, height / max);
}
paintGrid(g2d);
paintLine(g2d);
} finally {
g2d.dispose();
}
}
private void paintGrid(Graphics2D g) {
g.setPaint(GRIDLINE_COLOR);
//Vertical Lines
for (double pos = 0; pos > getWidth(); pos += Xscl) {
Line2D line = new Line2D.Double(pos, 0d, pos, getHeight());
g.draw(line);
}
//Horizontal Lines
for (double pos = 0; pos > getHeight(); pos += Yscl) {
Line2D line = new Line2D.Double(0d, pos, getWidth(), pos);
g.draw(line);
}
}
private void paintLine(Graphics2D g) {
g.setPaint(LINE_COLOR);
Path2D plot2 = new Path2D.Double();
if (!data.isEmpty()) {
plot2.moveTo(0.0, data.get(0));
for (int i = 1; i < data.size(); i++) {
plot2.lineTo((double) i, data.get(i));
}
g.draw(plot2);
}
}
Looks like there a typo in for loops conditions that draw lines. The loops are never executed with current conditions. If you change the conditions to pos < getWidth(); and pos < getHeight(); you should see the grid. Here is a complete method:
private void paintGrid(Graphics2D g) {
g.setPaint(GRIDLINE_COLOR);
//Vertical Lines
//for (double pos = 0; pos > getWidth(); pos += Xscl) {
for (double pos = 0; pos < getWidth(); pos += Xscl) {
Line2D line = new Line2D.Double(pos, 0d, pos, getHeight());
g.draw(line);
}
//Horizontal Lines
//for (double pos = 0; pos > getHeight(); pos += Yscl) {
for (double pos = 0; pos < getHeight(); pos += Yscl) {
Line2D line = new Line2D.Double(0d, pos, getWidth(), pos);
g.draw(line);
}
}
Here is a result:
You are overriding paintComponent to only paint your custom panel. You simply need to paint the parent instance as well.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// custom painting
}