Results 1 to 5 of 5
Thread: drawing in java
- 03-22-2012, 12:22 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
drawing in java
im trying to draw a random line or swiggle in the white space.i have tryed loads of code but it fails to draw.any ideas?
Java Code:public class mirrordraw extends JFrame implements ActionListener{ private Color color=(Color.WHITE); private JButton hjbutton; private JButton vjbutton; private JButton djbutton; private JButton cjbutton; private JCheckBox cbutton; private JButton rjbutton; private JButton gjbutton; private JButton bjbutton; //JLabel idLabel = new JLabel("Status bar:", JLabel.LEFT); JFrame f=new JFrame("mirrordraw"); JPanel y=new JPanel(); JPanel x=new JPanel(); JPanel b=new JPanel(); JPanel c=new JPanel();//f //private static canvas Canvas = new canvas(); public mirrordraw(){ f.getContentPane().setLayout(new GridLayout(1,2)); x.setLayout(new GridLayout(6,1)); b.setLayout(new GridLayout(2,2)); JPanel drawing=new JPanel (); drawing.setBackground(Color.white); f.add(drawing, BorderLayout.EAST); drawing.setPreferredSize(new Dimension(400, 0)); drawing.setBorder(BorderFactory.createLineBorder (Color.blue, 2)); drawing.setBackground(Color.white); f.add(drawing, BorderLayout.EAST); f.getGraphics(); hjbutton=new JButton ("Horizontal "); x.add(hjbutton,BorderLayout.EAST); vjbutton=new JButton ("Vertical "); x.add(vjbutton,BorderLayout.EAST); djbutton=new JButton ("Diagonal"); x.add(djbutton,BorderLayout.EAST); cbutton=new JCheckBox ("crosshairs "); cbutton.setBackground(Color.GRAY); cbutton.setBorder(null); x.add(cbutton,BorderLayout.EAST); rjbutton=new JButton (); rjbutton.setBackground(Color.red); rjbutton.setBorder(null); b.add(rjbutton,BorderLayout.WEST); rjbutton.setBorder(null); b.add(rjbutton,BorderLayout.WEST); rjbutton.setBorder(null); b.add(rjbutton,BorderLayout.WEST); gjbutton=new JButton (); gjbutton.setBackground(Color.green); gjbutton.setBorder(null); b.add(gjbutton,BorderLayout.EAST); bjbutton=new JButton (); bjbutton.setBackground(Color.blue); bjbutton.setBorder(null); b.add(bjbutton,BorderLayout.WEST); bjbutton=new JButton (); bjbutton.setBackground(Color.black); bjbutton.setBorder(null); b.add(bjbutton,BorderLayout.EAST); x.add(b); cjbutton=new JButton ("clear"); x.add(cjbutton,BorderLayout.EAST); f.getContentPane().add(x); f.setVisible(true);} public void actionPerformed(ActionEvent e) { if(e.getSource()==cbutton) getContentPane().getGraphics(); getGraphics(); cbutton.setVisible(true); } public static void main (String [] args){ JFrame frame = new mirrordraw(); frame.setSize(400,400); frame.setLocationRelativeTo(null); // Center frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static class canvas extends JPanel{ private int pointCount=0; private Point[] points=new Point[20000]; public int x,y; { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { if (pointCount<points.length); {points[pointCount]=event.getPoint(); ++pointCount; repaint(); } } } );} public void paintComponent(Graphics g) { super.paint(g); for (int i=0;i<pointCount;i++) g.fillOval(points[i].x,points[i].y,4,4); } public void mouseMoved(MouseEvent e) { x=e.getX(); y=e.getY(); repaint(); } public void mouseDragged(MouseEvent e) { x=e.getX(); y=e.getY(); } } }
- 03-22-2012, 12:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: drawing in java
Don't call the super.paint( ... ) method in line #187 of your code; call super.paintComponent( ... ) instead.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-22-2012, 10:33 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
-
Re: drawing in java
Maybe you need to revalidate the layout after you've done your painting? I haven't tried your code but that would be something I'd check out
- 03-23-2012, 03:05 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
drawing in java
By Ike in forum New To JavaReplies: 2Last Post: 11-07-2011, 06:39 PM -
Drawing coordinates in Java GUI
By javausr in forum Java 2DReplies: 5Last Post: 12-29-2010, 10:49 AM -
Drawing strings in Java
By LyraM in forum Java 2DReplies: 4Last Post: 05-11-2010, 11:15 PM -
drawing shapes in java help
By alphajoseph in forum Java 2DReplies: 2Last Post: 09-29-2009, 06:35 PM -
Java Drawing PUZZLE
By Cyorxamp in forum AWT / SwingReplies: 3Last Post: 06-09-2008, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks