Painting in a bufferedimage inside swing (Java)?

OK - here are a ..few changes. Import java.awt. *; import java.awt.image.

*; import java.awt.event. *; import javax.swing. *; import javax.swing.border.

*; import javax.swing.event. *; public class JavaPaintUI extends JFrame { private BufferedImage canvas; /** Creates new form JavaPaintUI */ public JavaPaintUI() { initComponents(); } //this was moved from the overriden paintComponent() // instead it update the canvas BufferedImage and calls repaint() public void updateCanvas() { Graphics2D g2 = canvas.createGraphics(); g2. SetRenderingKEY_ANTIALIASING,RenderingVALUE_ANTIALIAS_ON); g2.

SetPaint( getColor() ); if (tool == 1) { g2. FillOval(currentX - ((int) value / 2), currentY - ((int) value / 2), (int) value, (int) value); } else if (tool == 2) { g2. SetStroke(new BasicStroke((float) value, BasicStroke.

CAP_ROUND,BasicStroke. JOIN_ROUND)); g2. DrawLine(oldX, oldY, currentX, currentY); g2.

SetStroke(new BasicStroke(1.0f)); } repaint(); } // used in both the updateCanvas and 'clear' method. Private Color getColor() { Color c = null; if(color==1) c = Color. Black; else if(color==2) c = Color.

Gray; else if(color==3) c = Color. White; else if(color==4) c = Color. Red; else if(color==5) c = Color.

Green; else if(color==6) c = Color. Blue; return c; } // private void initComponents() { canvas = new BufferedImage(600,400,BufferedImage. TYPE_INT_RGB); buttonGroup1 = new ButtonGroup(); buttonGroup2 = new ButtonGroup(); jPanel4 = new JPanel(); jRadioButton9 = new JRadioButton(); jRadioButton10 = new JRadioButton(); jSlider2 = new JSlider(); jLabel1 = new JLabel(); jPanel2 = new JPanel(new GridBagLayout()); JLabel canvasLabel = new JLabel(new ImageIcon(canvas)); jPanel2.

Add(canvasLabel, null); jPanel3 = new JPanel(); jRadioButton3 = new JRadioButton(); jRadioButton4 = new JRadioButton(); jRadioButton5 = new JRadioButton(); jRadioButton6 = new JRadioButton(); jRadioButton7 = new JRadioButton(); jRadioButton8 = new JRadioButton(); jButton1 = new JButton(); jButton2 = new JButton(); setDefaultCloseOperation(WindowConstants. EXIT_ON_CLOSE); setTitle("JavaPaint ~ Nick R"); jPanel4. SetBorder(BorderFactory.

CreateTitledBorder("Tool")); buttonGroup1. Add(jRadioButton9); jRadioButton9. SetText("Pen"); jRadioButton9.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton9ActionPerformed(evt); } }); buttonGroup1. Add(jRadioButton10); jRadioButton10. SetText("Line"); jRadioButton10.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton10ActionPerformed(evt); } }); jSlider2. SetMajorTickSpacing(10); jSlider2. SetMaximum(51); jSlider2.

SetMinimum(1); jSlider2. SetMinorTickSpacing(5); jSlider2. SetPaintTicks(true); jSlider2.

AddChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent evt) { jSlider2StateChanged(evt); } }); jLabel1. SetText("Stroke Size (Radius)"); GroupLayout jPanel4Layout = new GroupLayout(jPanel4); jPanel4. SetLayout(jPanel4Layout); jPanel4Layout.

SetHorizontalGroup( jPanel4Layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddGroup(jPanel4Layout. CreateSequentialGroup() .addContainerGap() . AddGroup(jPanel4Layout.

CreateParallelGroup(GroupLayout.Alignment. LEADING) . AddComponent(jRadioButton9) .

AddComponent(jRadioButton10)) . AddPreferredGap(LayoutStyle. ComponentPlacement.

RELATED, 51, Short. MAX_VALUE) . AddGroup(jPanel4Layout.

CreateParallelGroup(GroupLayout.Alignment. TRAILING) . AddComponent(jLabel1) .

AddComponent(jSlider2, GroupLayout. PREFERRED_SIZE, 150, GroupLayout. PREFERRED_SIZE)) .addContainerGap()) ); jPanel4Layout.

SetVerticalGroup( jPanel4Layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddGroup(jPanel4Layout. CreateParallelGroup(GroupLayout.Alignment. TRAILING) .

AddComponent(jSlider2, GroupLayout. PREFERRED_SIZE, GroupLayout. DEFAULT_SIZE, GroupLayout.

PREFERRED_SIZE) . AddGroup(jPanel4Layout. CreateSequentialGroup() .

AddGroup(jPanel4Layout. CreateParallelGroup(GroupLayout.Alignment. BASELINE) .

AddComponent(jRadioButton9) . AddComponent(jLabel1)) . AddPreferredGap(LayoutStyle.

ComponentPlacement. UNRELATED) . AddComponent(jRadioButton10))) ); jPanel2.

SetBackground(new Color(128, 40, 128)); jPanel2. SetBorder(BorderFactory. CreateBevelBorder(BevelBorder.

RAISED)); // add the listeners to the label that contains the canvas buffered image canvasLabel. AddMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jPanel2MousePressed(evt); } public void mouseReleased(MouseEvent evt) { jPanel2MouseReleased(evt); } }); canvasLabel. AddMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { jPanel2MouseDragged(evt); } }); // this part of the code was not helping.

// either layout a container or custom paint it. // only attempt both if you are very confident of what you are doing. /* GroupLayout jPanel2Layout = new GroupLayout(jPanel2); jPanel2.

SetLayout(jPanel2Layout); jPanel2Layout. SetHorizontalGroup( jPanel2Layout. CreateParallelGroup(GroupLayout.Alignment.

LEADING) . AddGap(0, 596, Short. MAX_VALUE) ); jPanel2Layout.

SetVerticalGroup( jPanel2Layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddGap(0, 357, Short. MAX_VALUE) ); */ jPanel3. SetBorder(BorderFactory.

CreateTitledBorder("Color")); buttonGroup2. Add(jRadioButton3); jRadioButton3. SetText("Red"); jRadioButton3.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton3ActionPerformed(evt); } }); buttonGroup2. Add(jRadioButton4); jRadioButton4. SetText("Black"); jRadioButton4.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton4ActionPerformed(evt); } }); buttonGroup2. Add(jRadioButton5); jRadioButton5. SetText("Gray"); jRadioButton5.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton5ActionPerformed(evt); } }); buttonGroup2. Add(jRadioButton6); jRadioButton6. SetText("Green"); jRadioButton6.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton6ActionPerformed(evt); } }); buttonGroup2. Add(jRadioButton7); jRadioButton7. SetText("White"); jRadioButton7.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton7ActionPerformed(evt); } }); buttonGroup2. Add(jRadioButton8); jRadioButton8. SetText("Blue"); jRadioButton8.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jRadioButton8ActionPerformed(evt); } }); GroupLayout jPanel3Layout = new GroupLayout(jPanel3); jPanel3. SetLayout(jPanel3Layout); jPanel3Layout. SetHorizontalGroup( jPanel3Layout.

CreateParallelGroup(GroupLayout.Alignment. LEADING) . AddGroup(jPanel3Layout.

CreateSequentialGroup() .addContainerGap() . AddGroup(jPanel3Layout. CreateParallelGroup(GroupLayout.Alignment.

LEADING, false) . AddComponent(jRadioButton3, GroupLayout.Alignment. TRAILING, GroupLayout.

DEFAULT_SIZE, GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE) .

AddComponent(jRadioButton4, GroupLayout.Alignment. TRAILING, GroupLayout. DEFAULT_SIZE, 66, Short.

MAX_VALUE)) . AddPreferredGap(LayoutStyle. ComponentPlacement.

RELATED) . AddGroup(jPanel3Layout. CreateParallelGroup(GroupLayout.Alignment.

LEADING) . AddComponent(jRadioButton5, GroupLayout. DEFAULT_SIZE, 55, Short.

MAX_VALUE) . AddComponent(jRadioButton6, GroupLayout. DEFAULT_SIZE, GroupLayout.

DEFAULT_SIZE, Short. MAX_VALUE)) . AddPreferredGap(LayoutStyle.

ComponentPlacement. RELATED) . AddGroup(jPanel3Layout.

CreateParallelGroup(GroupLayout.Alignment. TRAILING, false) . AddComponent(jRadioButton8, GroupLayout.

DEFAULT_SIZE, GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE) .

AddComponent(jRadioButton7, GroupLayout. DEFAULT_SIZE, GroupLayout. DEFAULT_SIZE, Short.

MAX_VALUE)) .addContainerGap()) ); jPanel3Layout. SetVerticalGroup( jPanel3Layout. CreateParallelGroup(GroupLayout.Alignment.

LEADING) . AddGroup(GroupLayout.Alignment. TRAILING, jPanel3Layout.

CreateSequentialGroup() . AddGroup(jPanel3Layout. CreateParallelGroup(GroupLayout.Alignment.

BASELINE) . AddComponent(jRadioButton4) . AddComponent(jRadioButton5) .

AddComponent(jRadioButton7)) . AddPreferredGap(LayoutStyle. ComponentPlacement.

RELATED, 3, Short. MAX_VALUE) . AddGroup(jPanel3Layout.

CreateParallelGroup(GroupLayout.Alignment. BASELINE) . AddComponent(jRadioButton3) .

AddComponent(jRadioButton6) . AddComponent(jRadioButton8))) ); jButton1. SetText("Clear"); jButton1.

AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2. SetText("About"); jButton2. AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton2ActionPerformed(evt); } }); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().

SetLayout(layout); layout. SetHorizontalGroup( layout. CreateParallelGroup(GroupLayout.Alignment.

LEADING) . AddGroup(layout. CreateSequentialGroup() .addContainerGap() .

AddGroup(layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddComponent(jPanel2, GroupLayout.Alignment. TRAILING, GroupLayout. DEFAULT_SIZE, GroupLayout.

DEFAULT_SIZE, Short. MAX_VALUE) . AddGroup(layout.

CreateSequentialGroup() . AddComponent(jPanel4, GroupLayout. PREFERRED_SIZE, GroupLayout.

DEFAULT_SIZE, GroupLayout. PREFERRED_SIZE) . AddPreferredGap(LayoutStyle.

ComponentPlacement. UNRELATED) . AddComponent(jPanel3, GroupLayout.

PREFERRED_SIZE, GroupLayout. DEFAULT_SIZE, GroupLayout. PREFERRED_SIZE) .

AddPreferredGap(LayoutStyle. ComponentPlacement. RELATED) .

AddGroup(layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddComponent(jButton2, GroupLayout. DEFAULT_SIZE, 112, Short. MAX_VALUE) .

AddComponent(jButton1, GroupLayout. DEFAULT_SIZE, 112, Short. MAX_VALUE)))) .addContainerGap()) ); layout.

SetVerticalGroup( layout. CreateParallelGroup(GroupLayout.Alignment. LEADING) .

AddGroup(layout. CreateSequentialGroup() . AddGroup(layout.

CreateParallelGroup(GroupLayout.Alignment. LEADING, false) . AddGroup(layout.

CreateSequentialGroup() . AddGap(4, 4, 4) . AddComponent(jButton1, GroupLayout.

PREFERRED_SIZE, 30, GroupLayout. PREFERRED_SIZE) . AddPreferredGap(LayoutStyle.

ComponentPlacement. RELATED) . AddComponent(jButton2, GroupLayout.

PREFERRED_SIZE, 28, GroupLayout. PREFERRED_SIZE)) . AddComponent(jPanel4, GroupLayout.

DEFAULT_SIZE, GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE) .

AddComponent(jPanel3, GroupLayout. DEFAULT_SIZE, GroupLayout. DEFAULT_SIZE, Short.

MAX_VALUE)) . AddPreferredGap(LayoutStyle. ComponentPlacement.

RELATED) . AddComponent(jPanel2, GroupLayout. DEFAULT_SIZE, GroupLayout.

DEFAULT_SIZE, Short. MAX_VALUE) .addContainerGap()) ); pack(); }// // clear the canvas using the currently selected color. Private void jButton1ActionPerformed(ActionEvent evt) { System.out.

Println("You cleared the canvas. "); Graphics g = canvas.getGraphics(); g. SetColor( getColor() ); g.

FillRect(0,0,canvas.getWidth(),canvas.getHeight()); repaint(); } private void jButton2ActionPerformed(ActionEvent evt) { JOptionPane. ShowMessageDialog(null, "JavaPaint is a simple java based painting application. " + " Nick R 5/22/2011", "About", JOptionPane.

INFORMATION_MESSAGE); } int currentX, currentY, oldX, oldY; private void jPanel2MouseDragged(MouseEvent evt) { currentX = evt.getX(); currentY = evt.getY(); updateCanvas(); if (tool == 1) { oldX = currentX; oldY = currentY; System.out. Println("Using pen at " + currentX + ", " + currentY); } } private void jPanel2MousePressed(MouseEvent evt) { oldX = evt.getX(); oldY = evt.getY(); if (tool == 2) { currentX = oldX; currentY = oldY; } } //Tool Selection// int tool = 0; private void jRadioButton9ActionPerformed(ActionEvent evt) { tool = 1; System.out. Println("Using the pen tool."); } private void jRadioButton10ActionPerformed(ActionEvent evt) { tool = 2; System.out.

Println("Using the line tool. "); } //Slider Properties// double value = 5; private void jSlider2StateChanged(ChangeEvent evt) { value = jSlider2.getValue(); System.out. Println(value); } //COLOR CODE// int color = 1; private void jRadioButton4ActionPerformed(ActionEvent evt) { color = 1; // Black } private void jRadioButton5ActionPerformed(ActionEvent evt) { color = 2; // Grey } private void jRadioButton7ActionPerformed(ActionEvent evt) { color = 3; // White } private void jRadioButton3ActionPerformed(ActionEvent evt) { color = 4; // Red } private void jRadioButton6ActionPerformed(ActionEvent evt) { color = 5; // Green } private void jRadioButton8ActionPerformed(ActionEvent evt) { color = 6; // Blue } //mouse released// private void jPanel2MouseReleased(MouseEvent evt) { currentX = evt.getX(); currentY = evt.getY(); if (tool == 2) { System.out.

Println("line from " + oldX +", "+ oldY + " to " + currentX + ", " + currentY); } } //set ui visible// public static void main(String args) { EventQueue. InvokeLater(new Runnable() { public void run() { new JavaPaintUI(). SetVisible(true); } }); } // Variables declaration - do not modify private ButtonGroup buttonGroup1; private ButtonGroup buttonGroup2; private JButton jButton1; private JButton jButton2; private JLabel jLabel1; public JPanel jPanel2; private JPanel jPanel3; private JPanel jPanel4; private JRadioButton jRadioButton10; private JRadioButton jRadioButton3; private JRadioButton jRadioButton4; private JRadioButton jRadioButton5; private JRadioButton jRadioButton6; private JRadioButton jRadioButton7; private JRadioButton jRadioButton8; private JRadioButton jRadioButton9; public JSlider jSlider2; // End of variables declaration } Screen shot.

1 cannot find symbol in canvas. AddMouseListener(new MouseAdapter() {... and canvas. AddMouseMotionListener(new MouseMotionAdapter() {... method addMouseListener(anonymous java.awt.event.

MouseAdapter/MouseMotionAdapter) location: class java.awt.image. BufferedImage :-) – mKorbel May 26 at 8:18 @mKorbel: My bad - it should have been added to the canvasLabel. Answer edited.

– Andrew Thompson May 26 at 9:00 really good one +1 – mKorbel May 26 at 9:36.

Drawing into a buffer is just like drawing in a paintComponent(Graphics g). The difference is that you: 1) first create the buffer from any component (e.g. A Panel): Image image = panel. CreateImage(width, height); 2) get the graphics context for the image: Graphics g = image.getGraphics(); 3) finally when done drawing into the buffer: g.dispose(); Later, to draw the image onto your component: void paintComponent(Graphics g) { g.

DrawImage(image, x_offset, y_offset, null); }.

– Nick Rossetti May 26 at 2:58 if I drawimage in paint component, where do I create and get the image (1 and 2) – Nick Rossetti May 26 at 3:31 Once you have created your Panel2 instance, you can call panel2. CreateImage(w, h) on it to make the Image. When you want to draw do g = image.getGraphics(); drawing stuff; g.dispose(); – karmakaze May 26 at 15:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions