What is the simplest way to draw in Java?
import java.awt.*;
import javax.swing.*;
public class Canvas
{
private JFrame frame;
private Graphics2D graphic;
private JPanel canvas;
public Canvas()
{
frame = new JFrame("A title");
canvas = new JPanel();
frame.setContentPane(canvas);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g){
BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Grapics2D g2 = offImg.createGraphics();
g2.setColor(new Color(255,0,0));
g2.fillRect(10,10,200,50);
}
}
This doesn't work and I have no idea how to get anything to appear.
Easiest way:
public class Canvas extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// painting code goes here.
}
}
You simply need to extend JPanel
and override the paintComponent
method of the panel.
I'd like to reiterate that you should not be overriding the paint
method.
Here is a very minimalistic example that works.
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, 100, 100);
}
};
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
// No. 1
// Create a graphics context on the buffered image
Graphics2D g2d = bimage.createGraphics();
// Draw on the buffered image
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
g2d.dispose();
// No.2
// In case the buffered image supports transparency
g2d = bimage.createGraphics();
// Transparency is created on all the filled pixels
Color transparent = new Color(0, 0, 0, 0);
g2d.setColor(transparent);
g2d.setComposite(AlphaComposite.Src);
g2d.fill(new Rectangle2D.Float(20, 20, 100, 20));
g2d.dispose();
jjnguy already wrote how to do it right ... but here why it does not work in your example:
import java.awt.*;
import javax.swing.*;
public class Canvas
Here you have a class which does not relate in any way to Swing or AWT.
(By the way, you may want to select another name to avoid confusion with java.awt.Canvas
.)
{
private JFrame frame;
private Graphics2D graphic;
private JPanel canvas;
public Canvas()
{
frame = new JFrame("A title");
canvas = new JPanel();
frame.setContentPane(canvas);
Here you are creating a new JPanel (for confusion also named canvas
), and add it to the frame.
Is is this panel's paint
and paintComponent
methods which are called when the system shows your frame.
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g){
BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Grapics2D g2 = offImg.createGraphics();
g2.setColor(new Color(255,0,0));
g2.fillRect(10,10,200,50);
}
This paint method is never used at all (since it is not part of a component), and if it would be called, then you are only painting to some BufferedImage, not to the screen.
}
To make something appear in paint(Graphics g) you need to call the drawing methods (like fillRect) on that Graphics. You are creating a bitmap and then drawing to the bitmap, not the screen.
public void paint(Graphics g){
g.setColor(new Color(255,0,0));
g.fillRect(10,10,200,50);
}