Reputation: 1291
I am currently writing a Java painting program using the Swing libraries and Graphics2D to paint. My main GUI class extends JComponent, and I am trying to put it inside a JPanel, and the JPanel inside a JFrame, in order to show it on the screen. When starting up the program though, the JComponent appears to be just a black line (the border, which is set to be a black line around the component). I cannot see why this is happening, and I've been debugging it for hours. If somebody could find the error in this program, I'd be very happy. Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintGUI extends JComponent {
private static final long serialVersionUID = 1L;
JButton red, green, blue, clear;
Image img;
Graphics2D gfx;
JFrame drawFrame;
JPanel btnPan, drawPan;
MyListener ml;
Action act;
int x, y, prevX, prevY;
public PaintGUI(){
//Initialisering av panel, frame og content
drawFrame = new JFrame("IFIPaint");
drawFrame.setLayout(new BorderLayout());
btnPan = new JPanel();
drawPan = new JPanel();
btnPan.setLayout(new FlowLayout());
drawPan.setLayout(new BorderLayout());
this.setEnabled(true);
//Setter størrelser
btnPan.setPreferredSize(new Dimension(30, 60));
btnPan.setMinimumSize(new Dimension(30, 60));
btnPan.setMaximumSize(new Dimension(30, 60));
//Ordner knappene
red = new JButton("Rød");
green = new JButton("Grønn");
blue = new JButton("Blå");
clear = new JButton("Slett alt");
//Putter knappene på panelet
btnPan.add(red);
btnPan.add(green);
btnPan.add(blue);
btnPan.add(clear);
//Legger på action listeners
act = new Action();
red.addActionListener(act);
green.addActionListener(act);
blue.addActionListener(act);
clear.addActionListener(act);
//Fullfører vindu og setter synlighet
drawFrame.setSize(500, 500);
drawPan.setBounds(0, 0, 400, 400);
this.setBounds(0, 0, 400, 400);
drawPan.add(this);
this.setBackground(Color.RED);
drawFrame.add(drawPan, BorderLayout.NORTH);
drawFrame.add(btnPan, BorderLayout.SOUTH);
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.setVisible(true);
drawPan.setVisible(true);
btnPan.setVisible(true);
drawFrame.setVisible(true);
this.paintComponent(gfx);
drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
draw();
}
public void draw() {
ml = new MyListener();
this.addMouseListener(ml);
this.addMouseMotionListener(ml);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(img == null){
img = createImage(this.getWidth(),this.getHeight());
gfx = (Graphics2D) img.getGraphics();
gfx.setPaint(Color.RED);
gfx.fillRect(0, 0, this.getSize().width, this.getSize().height);
gfx.setPaint(Color.RED);
gfx.dispose();
}
gfx.drawImage(img, 0, 0, this);
}
class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == red){
gfx.setPaint(Color.RED);
repaint();
} else if (e.getSource() == green){
gfx.setPaint(Color.GREEN);
repaint();
} else if (e.getSource() == blue) {
gfx.setPaint(Color.BLUE);
repaint();
} else if (e.getSource() == clear) {
gfx.clearRect(0, 0, drawFrame.getWidth(), drawFrame.getHeight());
repaint();
}
}
}
class MyListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
System.out.println("o ye");
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
gfx.drawLine(prevX, prevY, x, y);
repaint();
prevX = x;
prevY = y;
}
}
}
Upvotes: 0
Views: 132
Reputation: 57391
You added the PaintGUI to frame but LayoutManager don't know the size and can't set desired size.
Either set the preferred size (or override getPreferredSize to return desired size)
or add to the PaintGUI instance some component(s) with preferred size (e.g. buttons)
Upvotes: 2