Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java and graphics........ughhhhhh

  • 10-12-2003 04:04PM
    #1
    Closed Accounts Posts: 7,132 ✭✭✭


    i got a wee program here, and i want to change the size of the line on the jpanel by using the jbuttons jb1-jb4

    what code do i put into the action performed area, and i need implements action listener in the main class as well after mouse listener ??????

    thanks guys......and gals.


    +++++++++++++++++++++++++++++++++++++++++++++++++++++++



    package drawframe;

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

    public class MyCanvas extends Canvas implements MouseListener {
    int fromX=0, fromY=0, toX=0, toY=0;

    public MyCanvas() {

    this.addMouseListener(this);
    this.setSize(256,256);
    toX= 150;
    toY= 200;
    }

    public void mouseClicked(MouseEvent e)
    {
    System.out.println("x pos: " + e.getX() + " y pos: " + e.getY());
    this.setBackground(new Color(0, e.getX(), e.getY()));
    }
    public void mouseEntered(MouseEvent e)
    {
    //this.setBackground(new Color(255, 0, 0));
    }
    public void mouseExited(MouseEvent e)
    {
    this.setBackground(new Color(255, 255, 255));
    }

    public void mousePressed(MouseEvent e)
    {
    fromX= e.getX();
    fromY = e.getY();
    }

    public void mouseReleased(MouseEvent e)
    {
    toX= e.getX();
    toY = e.getY();
    this.repaint();
    }

    public void mouseDragged(MouseEvent e){
    toX= e.getX();
    toY = e.getY();
    this.repaint();
    }

    public void mouseMoved(MouseEvent e){
    fromX= e.getX();
    fromY = e.getY();
    }

    public void paint(Graphics g){
    Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(new Color(244,0,0));
    // g.fillRect(10,10,60,60);
    // g.drawRect(1,1,100,100);
    g2d.setStroke(new BasicStroke(10));
    // Graphics2D g2d =
    g2d.drawLine(fromX,fromY,toX,toY);
    }
    public void actionPerformed(ActionEvent e) {

    if (e.getSource()==Size1);


    ???????????????????????????



    }


    }



    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    package drawframe;//class directory

    //Imports = includes
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JButton;

    public class DrawFrame{
    JFrame f1;
    JPanel jp1;
    JPanel jp2;
    JButton jb1,jb2,jb3,jb4;


    public DrawFrame(){
    f1 = new JFrame();
    jp1 = new JPanel();
    jp2 = new JPanel();

    JButton jb1 = new JButton();
    jb1.setText("Size 1");
    jb1.setBackground(Color.green);
    JButton jb2 = new JButton();
    jb2.setText("Size 2");
    jb2.setBackground(Color.yellow);

    JButton jb3 = new JButton();
    jb3.setText("Size 3");
    jb3.setBackground(Color.orange);
    JButton jb4 = new JButton();
    jb4.setText("Size 4");
    jb4.setBackground(Color.pink);


    jp1.setBackground(Color.black);
    jp2.setBackground(Color.white);
    jp1.setPreferredSize(new Dimension(200,300));
    jp2.setPreferredSize(new Dimension(80,30));
    jp1.setLayout(new FlowLayout());
    jp1.add(jb1);
    jp1.add(jb2);
    jp1.add(jb3);
    jp1.add(jb4);
    jp1.add(jp2);

    Container c = f1.getContentPane();
    c.setLayout(new FlowLayout());

    MyCanvas myCanvas = new MyCanvas ();
    c.add(jp1);
    c.add(myCanvas);
    f1.pack();
    f1.setVisible(true);
    }

    public static void main (String args[]){
    DrawFrame drawFrame = new DrawFrame();
    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Please use tabs to indent methods and if/for/while blocks, etc.

    It's very hard to tell what's going on otherwise and takes a lot longer to figure out (hence, don't know what "line" you're talking about). As for adding implemented interfaces you can just separate them with a comma:

    public class MyCanvas extends Canvas implements MouseListener, ActionListener

    Hope that helps!


  • Closed Accounts Posts: 94 ✭✭boo-boo


    if you had indentation in your code already and it was stripped out when posting to boards you could try prefixing the code segment with <pre> and adding </pre> to the end to preserve indenation


  • Closed Accounts Posts: 7,132 ✭✭✭x in the city


    thanks guys,

    the line of code i need altered is between the ??????? bit.

    (the actionlistener section, which needs to be added)
    all that happens when u run it, is you get a frame with 4 buttons and u can draw a line in a jpanel and i want to alter the size (width) of the line being drawn by using the buttons.......jbuton 1 = size 1 jbutton2 = size 2, etc........

    :)


  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Howdy,

    I have added a wee bit to the classes as shown below and it seems to work..
    I put the ActionListener into the DrawFrame class and it calls a drawLine1 method in myCanvas (I only did it for button 1.. you can fill in the rest yourself!). I added some (hopefully) helpful comments also.

    I also took out the package declaration at the start. I never learned to use packages like that so I don't know how that works!!

    You in college? Where?

    Good Luck..

    <PRE>

    //package drawframe;//class directory

    //Imports = includes
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JButton;
    import java.awt.event.*;

    public class DrawFrame implements ActionListener
    {
    JFrame f1;
    JPanel jp1;
    JPanel jp2;
    JButton jb1,jb2,jb3,jb4;
    MyCanvas myCanvas;

    public DrawFrame()
    {
    f1 = new JFrame();
    jp1 = new JPanel();
    jp2 = new JPanel();

    /* You had JButton JButton jb1 = new JButton() here..
    This creates a new JButton object which only exists in this constructor (method)
    Better to declare the buttons above so they can be used
    throughout the class (i.e. in actionPerformed also) */
    jb1 = new JButton();
    jb1.setText("Size 1");
    jb1.addActionListener(this);
    jb1.setBackground(Color.green);

    jb2 = new JButton();
    jb2.setText("Size 2");
    jb2.setBackground(Color.yellow);

    jb3 = new JButton();
    jb3.setText("Size 3");
    jb3.setBackground(Color.orange);

    jb4 = new JButton();
    jb4.setText("Size 4");
    jb4.setBackground(Color.pink);


    jp1.setBackground(Color.black);
    jp2.setBackground(Color.white);
    jp1.setPreferredSize(new Dimension(200,300));
    jp2.setPreferredSize(new Dimension(80,30));
    jp1.setLayout(new FlowLayout());
    jp1.add(jb1);
    jp1.add(jb2);
    jp1.add(jb3);
    jp1.add(jb4);
    jp1.add(jp2);

    Container c = f1.getContentPane();
    c.setLayout(new FlowLayout());

    myCanvas = new MyCanvas();
    c.add(jp1);
    c.add(myCanvas);
    f1.pack();
    f1.setVisible(true);
    }

    /* I added the actionPerformed to this class since the buttons exist here.
    This simply calls a method in myCanvas and draws a line from that method.
    I'm sure there's a way to call myCanvas -> actionPerformed with buttons in this class
    but I don't know it!!! */
    public void actionPerformed( ActionEvent e )
    {
    if( e.getSource() == jb1 )
    {
    myCanvas.drawLine1();
    }
    }

    public static void main (String args[])
    {
    DrawFrame drawFrame = new DrawFrame();
    }

    }

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //package drawframe;

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

    public class MyCanvas extends Canvas implements MouseListener
    {
    int fromX=0, fromY=0, toX=0, toY=0;

    public MyCanvas()
    {
    this.addMouseListener(this);
    this.setSize(256,256);
    toX= 150;
    toY= 200;
    }

    public void mouseClicked(MouseEvent e)
    {
    System.out.println("x pos: " + e.getX() + " y pos: " + e.getY());
    this.setBackground(new Color(0, e.getX(), e.getY()));
    }
    public void mouseEntered(MouseEvent e)
    {
    //this.setBackground(new Color(255, 0, 0));
    }
    public void mouseExited(MouseEvent e)
    {
    this.setBackground(new Color(255, 255, 255));
    }

    public void mousePressed(MouseEvent e)
    {
    fromX= e.getX();
    fromY = e.getY();
    }

    public void mouseReleased(MouseEvent e)
    {
    toX= e.getX();
    toY = e.getY();
    this.repaint();
    }

    public void mouseDragged(MouseEvent e)
    {
    toX= e.getX();
    toY = e.getY();
    this.repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
    fromX= e.getX();
    fromY = e.getY();
    }

    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(new Color(244,0,0));
    g2d.setStroke(new BasicStroke(10));
    g2d.drawLine(fromX,fromY,toX,toY);
    }

    /* This method is called from DrawFrame when button 1 is pressed */
    public void drawLine1()
    {
    fromX = 10;
    fromY = 10;
    toX = 100;
    toY = 10;
    this.repaint();
    }
    }

    </PRE>


  • Closed Accounts Posts: 7,132 ✭✭✭x in the city


    i going to a renowed institution in the west of ireland.........no not that type of institution ^^

    im only starting off with java, you seem rather prolific.

    i will have a gander with all this later and get back to you.
    cheers dude


    <><


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Please use the [ C O D E ] tag.. eg.
    this is code
       which is
          indented
    


  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Thanks Hobbes, was wondering how you do that!


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    just a word about action listeners. If you don't want to declare all the buttons at the top of the class, use actioncommands instead.
    e.g. instead of
    button1.addActionListener(this);
    use
    button1.setActionCommand("copy");

    then in actionPerformed you can just get the action command
    e.g.
    public void actionPerformed(ActionEvent e)
    if (e.getActionCommand().equals("copy")
    {
    ...
    }

    I find it a bit easier. It also means that two buttons can use the same code.


  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Yeh, good point! Forgot about that trick..


  • Closed Accounts Posts: 7,132 ✭✭✭x in the city


    ____________


    hi guys

    still trying to get the buttons to change the width of the line thats drawn on the canvas,

    this code should work, but i get loads of errors, do i have to set up a setlinesize class or something for this to work?

    ??

    thanks ^^




    public void actionPerformed( ActionEvent e ){



    if (ae.getSource() == jb1 ){
    System.out.println("button1 pushed");
    MyCanvas.setLineSize(2);

    }else
    if (ae.getSource() == jb2 ){
    System.out.println("button2 pushed");
    MyCanvas.setLineSize(4);
    }else
    if (ae.getSource() == jb3 ){
    System.out.println("button3 pushed");
    MyCanvas.setLineSize(7);
    }else
    if (ae.getSource() == jb4 ){
    System.out.println("button4 pushed");
    MyCanvas.setLineSize(10);
    }

    }











    ___________


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Originally posted by x in the city
    ____________

    public void actionPerformed( ActionEvent e ){

    if (ae.getSource() == jb1 ){
    System.out.println("button1 pushed");
    MyCanvas.setLineSize(2);

    }else
    if (ae.getSource() == jb2 ){
    System.out.println("button2 pushed");
    MyCanvas.setLineSize(4);
    }else
    if (ae.getSource() == jb3 ){
    System.out.println("button3 pushed");
    MyCanvas.setLineSize(7);
    }else
    if (ae.getSource() == jb4 ){
    System.out.println("button4 pushed");
    MyCanvas.setLineSize(10);
    }

    }

    ___________

    Hi 'x in the city',

    You have declared an attribute of type ActionEvent called 'e' as a parameter in the method above but you seem to refer to 'ae' in the body of the method. I pretty sure this should be 'e.getSource()' or 'ae', just make sure they're both the same.

    Also setLineSize() should be a method in myCanvas, not a separate class. Givus a shout if you need any help making this method or calling it or if you need any more clarification!

    Good luck,
    B


  • Closed Accounts Posts: 7,132 ✭✭✭x in the city


    hi dude

    thanks for your help, i got it sussed out now, but will be back to you again no doubt.....

    ciao!

    Eza


Advertisement