Hello everyone!
I am trying to get the hold on Jframes and I can so far create a simple window:
package javvv; import java.awt.*; import java.awt.event.*; import javax.swing.*; //package javvv; public class Javvv { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(640, 480)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } //Ignore this for now. public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; int x = 0; int y = 0; } }
And that works fine.
But I want to be able to draw for example a rectangle on it so I tried to create a class for that which takes the inputs of x, y and the sides.
NOTE: I took the code for creating a rectangle from a websire./* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Square_class; import javax.swing.JFrame; public class Square { private int x; private int y; private int side1; private int side2; /**The variables for the rectangle */ public Square(int x, int y, int side) { this.x = x; this.y = y; this.side1 = side1; this.side2 = side2; } /* Draw the rectangle in the window*/ public void draw(JFrame frame){ frame.g2.draw(new Rectangle2D.Double(x, y, side1, side2)); } }
But I dont know what to do from now on.
I want to be able to just write, after I have called the Square class:
Square(the x-position,the y-position, the side1, the side2)
My code might be a bit messy... But I hope you understand what I need help with.
thank you in advance!