Hello everyone!
I've recently tried to get swing frames working but the obstacle now is that I want to be able to move a rectangle on the screen.
My first step was creating a rectangle which got drawn at the start on mouse x and y position:
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.*; import java.awt.MouseInfo; public class MainWindowtestagain { public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI(){ System.out.println("Created GUI on EDT? "+ SwingUtilities.isEventDispatchThread()); JFrame f = new JFrame("Swing Paint Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MyPanel()); f.pack(); f.setVisible(true); } } class MyPanel extends JPanel { public MyPanel() { setBorder(BorderFactory.createLineBorder(Color.blue)); } public Dimension getPreferredSize() { return new Dimension(640,480); } //// //// public void paintComponent(Graphics g) { super.paintComponent(g); int rectx=77; int recty=66; // Draw Text //g.drawString("("+xpos+","+ypos+")",xpos,ypos); PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int x = (int) b.getX(); int y = (int) b.getY(); g.setColor(Color.black); g.drawString("This is my custom Panel!",10,20); g.setColor(Color.red); //g.drawRect(x/*x1*/, y/*y1*/, 32/*x2*/, 32 /*x2*/); g.drawLine(x, y-32, x+32, y-32); g.drawLine(x, y, x, y-32); g.drawLine(x+32, y, x+32, y-32); g.drawLine(x, y, x+32, y); g.setColor(Color.cyan); g.drawLine(140, 90, 86, 349); g.drawString(x+" "+y,32,16); } } }
but then I wanted it to happen every step so I tried to create a loop:
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.*; import java.awt.MouseInfo; public class MainWindowtestagain { public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI(){ System.out.println("Created GUI on EDT? "+ SwingUtilities.isEventDispatchThread()); JFrame f = new JFrame("Swing Paint Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MyPanel()); f.pack(); f.setVisible(true); } } class MyPanel extends JPanel { public MyPanel() { setBorder(BorderFactory.createLineBorder(Color.blue)); } public Dimension getPreferredSize() { return new Dimension(640,480); } //// //// public void paintComponent(Graphics g) { super.paintComponent(g); int rectx=77; int recty=66; // Draw Text //g.drawString("("+xpos+","+ypos+")",xpos,ypos); int ii = 0; while (ii < 234823) { PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int x = (int) b.getX(); int y = (int) b.getY(); g.setColor(Color.black); g.drawString("This is my custom Panel!",10,20); g.setColor(Color.red); //g.drawRect(x/*x1*/, y/*y1*/, 32/*x2*/, 32 /*x2*/); g.drawLine(x, y-32, x+32, y-32); g.drawLine(x, y, x, y-32); g.drawLine(x+32, y, x+32, y-32); g.drawLine(x, y, x+32, y); g.setColor(Color.cyan); g.drawLine(140, 90, 86, 349); g.drawString(x+" "+y,32,16); ii+=1; } } }
but the program refused to show the images until i closed it, must be some sort of lag.
What should I do?