I was thinking about making a GUI with a text input field, and on the right have a bufferedImage, which updates on the input i enter in the "console". Is it possible to have these two in the same GUI?
What I was thinking about was this example:
I've made a code where I only have a bufferedImage, but I want to add the left part of the image above, and I'm a bit stuck on how?
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.lang.Math;
// From SO post: https://stackoverflow.com/a/3325804/12989146
public class DirectDrawDemo extends JPanel {
final private BufferedImage canvas;
final private Color def_c = Color.BLACK; final private Color def_bg = Color.WHITE;
public DirectDrawDemo(int width, int height) {
canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
fillCanvas(def_bg);
drawCircle(def_c,300,300,100);
}
public Dimension getPreferredSize() {
return new Dimension(canvas.getWidth(), canvas.getHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, null, null);
}
public void fillCanvas(Color c) {
int color = c.getRGB();
for (int x = 0; x < canvas.getWidth(); x++) {
for (int y = 0; y < canvas.getHeight(); y++) {
canvas.setRGB(x, y, color);
}
}
repaint();
}
public void drawCircle(Color c, int x_center, int y_center, int r){
// inspiration : https://www.gatevidyalay.com/mid-point-circle-drawing-algorithm/
int x=r, y=0, p = 1-r;
// checking for radius of circle
if(r==0){
drawPoint(x_center,y_center,c);
}
if(r<0)
return;
// initialising point p
while(x>y){
y++;
if (p < 0) {
p = p + 2 * y + 1;
} else {
x--;
p = p+2*y-2*x+1;
}
//print all octaves
drawPoint(x + x_center,y + y_center,c); // 1. (x,y)
drawPoint(y + x_center,x + y_center,c); // 1. (y,x)
drawPoint(y + x_center,-x + y_center,c); // 4. (y,-x)
drawPoint(x + x_center,-y + y_center,c); // 4. (x,-y)
drawPoint(-x + x_center,-y + y_center,c); // 3. (-x,-y)
drawPoint(-y + x_center,-x + y_center,c); // 3. (-y,-x)
drawPoint(-y + x_center,x + y_center,c); // 2. (-y,x)
drawPoint(-x + x_center,y + y_center,c); // 2. (-x,y)
}
//show
repaint();
}
void drawPoint(int x,int y,Color c){
canvas.setRGB(x,y, c.getRGB());
}
public static void main(String[] args) {
int width = 1280;
int height = 940;
JFrame frame = new JFrame("Direct draw demo");
//JPanel panel = new JPanel();
DirectDrawDemo panel2 = new DirectDrawDemo(width, height);
//panel.setLayout(new GridLayout(0,1));
//JLabel label1 = new JLabel();
//label1.setText("Enter x");
//JTextField textField1 = new JTextField();
frame.add(panel2);
//panel.add(label1);
//panel.add(textField1);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
EDIT
So I've tried what was told in the comments and it seems that i have the idea on how to place the different BorderLayouts as I want, but i have one issue now.. When I run my program The window is totaly BLANK (just white), but when I resize the window just a little or something everything pops up?
Here is my new code I've made:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class GUI extends JPanel {
final private BufferedImage canvas;
final private Color def_c = Color.BLACK; final private Color def_bg = Color.WHITE;
public static void main(String[] args){
int width = 1280; int height = 720;
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setLayout(new BorderLayout(3,3));
frame.setVisible(true);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel1.setPreferredSize(new Dimension(200, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
/*----- Sub panels -----*/
GUI panel_Canvas = new GUI(width, height);
//Add sub panels to this CENTER layout
panel2.setLayout(new BorderLayout());
//Adding to panel 2
panel2.add(panel_Canvas, BorderLayout.CENTER);
frame.add(panel1,BorderLayout.WEST);
frame.add(panel2,BorderLayout.CENTER);
frame.add(panel3,BorderLayout.SOUTH);
}
public GUI(int width, int height) {
canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
fillCanvas(def_bg);
int x0 = 0;
int x1 = 500;
int y0 = 0;
int y1 = 300;
//drawRect(Color.RED, 0, 0, width/2, height/2);
drawLine(def_c, x0, y0, x1, y1);
//drawLineRecInit(Color.yellow, x0, y0+2, x1, y1+2);
drawCircle(def_c,300,300,100);
}
public Dimension getPreferredSize() {
return new Dimension(canvas.getWidth(), canvas.getHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, null, null);
}
public void fillCanvas(Color c) {
int color = c.getRGB();
for (int x = 0; x < canvas.getWidth(); x++) {
for (int y = 0; y < canvas.getHeight(); y++) {
canvas.setRGB(x, y, color);
}
}
repaint();
}
// Implementation from Wikipedia: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
public void drawLine(Color c, int x0, int y0, int x1, int y1)
{
int dx = Math.abs(x1-x0);
int sx = x0 < x1 ? 1 : -1;
int dy = -Math.abs(y1-y0);
int sy = y0 < y1 ? 1 : -1;
int err = dx + dy; /* error value e_xy */
while (true) /* loop */
{
canvas.setRGB(x0, y0, c.getRGB());
if (x0 == x1 && y0 == y1) break;
int e2 = 2*err;
if (e2 >= dy) /* e_xy+e_x > 0 */
{
err += dy;
x0 += sx;
}
if (e2 <= dx) /* e_xy+e_y < 0 */
{
err += dx;
y0 += sy;
}
}
repaint();
}
public void drawLineRec(Color c, int x0, int y0, int x1, int y1, int dx, int dy, int err)
{
canvas.setRGB(x0, y0, c.getRGB());
if(!(x0 == x1 && y0 == y1))
{
if((2*err) >= dy)
{
if((2*err) <= dx)
{
drawLineRec(c, x0 + (x0 < x1 ? 1 : -1), y0 + (y0 < y1 ? 1 : -1), x1, y1, dx, dy, err + dx + dy);
}
else
{
drawLineRec(c, x0 + (x0 < x1 ? 1 : -1), y0, x1, y1, dx, dy, err + dy);
}
}
else if((2*err) <= dx)
{
drawLineRec(c, x0, y0 + (y0 < y1 ? 1 : -1), x1, y1, dx, dy, err + dx);
}
}
}
public void drawCircle(Color c, int x_center, int y_center, int r){
// inspiration : https://www.gatevidyalay.com/mid-point-circle-drawing-algorithm/
int x=r, y=0, p = 1-r;
// checking for radius of circle
if(r==0){
drawPoint(x_center,y_center,c);
}
if(r<0)
return;
// initialising point p
while(x>y){
y++;
if (p < 0) {
p = p + 2 * y + 1;
} else {
x--;
p = p+2*y-2*x+1;
}
//print all octaves
drawPoint(x + x_center,y + y_center,c); // 1. (x,y)
drawPoint(y + x_center,x + y_center,c); // 1. (y,x)
drawPoint(y + x_center,-x + y_center,c); // 4. (y,-x)
drawPoint(x + x_center,-y + y_center,c); // 4. (x,-y)
drawPoint(-x + x_center,-y + y_center,c); // 3. (-x,-y)
drawPoint(-y + x_center,-x + y_center,c); // 3. (-y,-x)
drawPoint(-y + x_center,x + y_center,c); // 2. (-y,x)
drawPoint(-x + x_center,y + y_center,c); // 2. (-x,y)
}
//show
repaint();
}
void drawPoint(int x,int y,Color c){
canvas.setRGB(x,y, c.getRGB());
}
}
UPDATE
I added a button
Then I added the action listener in the GUI main
On a button click this is called, but doesn't update the view?