Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ.

καλησπερα, εχω φτιαξει ενα JFrame με ενα canvas και θελω να ζωγραφισω ν τυχαιους κυκλους με ενα πατημα του ποντικιου. Πως μπορω να το κανω αυτό?

 

Παραθέτω τον κώδικα

>import javax.swing.JLabel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class Canvas extends JLabel implements MouseListener{
	private int x = -1;
	private int y = -1;
	private int rad = 50;
	
	private boolean circle = true;
	
	public boolean isCircle() {
		return circle;
	}

	public void setCircle(boolean circle) {
		this.circle = circle;
	}

	public Canvas() {
		super();
		setPreferredSize(new Dimension(800,600));
		this.addMouseListener(this);
		
	}
	
	public void paint(Graphics g){
		if (x < 0) reset();
		if (circle) {
			for (int i = 0; i < 5; i++) {
				x = (int) (Math.random()*400);
				y = (int) (Math.random()*400);
				rad = (int) (Math.random()*50);
				g.drawOval(x-rad, y-rad, 2*rad, 2*rad);
			}
			
		}
		else g.drawRect(x-rad, y-rad, 2*rad, 2*rad);
		
	}
	
	public void reset(){
		x = getWidth()/2;
		y = getHeight()/2;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		x = arg0.getX();
		y = arg0.getY();
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

 

Εdit: Επισης πως μπορω να εχω τους κυκλους μεσα στον καμβα?

Δημοσ.

Προσθεσα μερικα στοιχεια

>import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class Canvas extends JLabel implements MouseListener{
	private int x = -1;
	private int y = -1;
	private int rad = 50;
	
	private boolean circle = true;
	private boolean square = false;
	
	public boolean isCircle() {
		return circle;
	}

	public void setCircle(boolean circle) {
		this.circle = circle;
	}
	
	public void setSquare(boolean square) {
		this.square  = square;
	}

	public Canvas() {
		super();
		setPreferredSize(new Dimension(800,600));
		this.addMouseListener(this);
		
	}
	
	public void paint(Graphics g){
		// Draw shapes from 1-5
		int n = (int) Math.ceil(Math.random()*5) ;
		if (x < 0) reset();
		if (circle) {
			for (int i = 0; i < n; i++) {
				// random in canvas
				x = (int) (Math.random()*400);
				y = (int) (Math.random()*400);
				rad = (int) (Math.random()*100);
				// random colour
	            Color randomHue = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
	            g.setColor(randomHue);
	            // full colour inside
	            g.fillOval(x-rad, y-rad, 2*rad, 2*rad);
				//g.drawOval(x-rad, y-rad, 2*rad, 2*rad);
			}
			
		}
		if (square) {
			for (int i = 0; i < n; i++) {
				// random in canvas
				x = (int) (Math.random()*400);
				y = (int) (Math.random()*400);
				rad = (int) (Math.random()*100);
				// random colour
	            Color randomHue = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
	            g.setColor(randomHue);
	            // full colour inside
	            g.fillRect(x-rad, y-rad, 2*rad, 2*rad);
	            //g.drawRect(x-rad, y-rad, 2*rad, 2*rad);
			}
		}
		else {
			for (int i = 0; i < n; i++) {
				// random in canvas
				int x1 = (int) (Math.random()*400);
				int y1 = (int) (Math.random()*400);
				int x2 = (int) (Math.random()*400);
				int y2 = (int) (Math.random()*400);
				// random colour
	            Color randomHue = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
	            g.setColor(randomHue);
	            // full colour inside
	            g.drawLine(x1,y1,x2,y2);
			}
		}
	}
	
	public void reset(){
		x = getWidth()/2;
		y = getHeight()/2;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		x = arg0.getX();
		y = arg0.getY();
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

 

Εβαλα στον canvas να ζωγαφιζει κυκλος τετραγωνα και γραμμες, ομως οταν επιλεγω τους κυκλους εχω ενα combobox(παραθετω τον κωδικα παρακατω) τοτε στην επλογη circle βλεπω κυκλους και γραμμες. Γιατι?

 

 

>import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyFrame extends JFrame implements ActionListener {

Canvas canvas = new Canvas();
public MyFrame() throws HeadlessException {
	// set title
	super("This is my Frame");
	// set to visible
	setVisible(true);
	this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	// Combobox options
	String [] options = {"circle","square","line"};
	// Create combobox
	JComboBox combo = new JComboBox(options);
	// size of combobox
	//combo.setPreferredSize(new Dimension(800,150));
	// auto-item
	combo.setSelectedItem("circle");
	// location of the combobox SOUTH-NORTH
	getContentPane().add(combo,BorderLayout.NORTH);
	// mouse click
	combo.addActionListener(this);
	
	
	// Canvas addition
	getContentPane().add(canvas, BorderLayout.CENTER);
	// Start the thread
	new myThread(canvas).start();
	
	pack();
}
@Override
public void actionPerformed(ActionEvent ev) {
	JComboBox com = (JComboBox) ev.getSource();
	if (com.getSelectedItem().equals("circle"))
		canvas.setCircle(true);
	else canvas.setCircle(false);
	if (com.getSelectedItem().equals("square"))
		canvas.setSquare(true);
	else canvas.setSquare(false);
	repaint();
}

}

Δημοσ.

έχεις γράψει λάθος το if γι'αυτό βλέπεις και κύκλους και γραμμές

 

π.χ.

 

>
.........
g.fillRect(x-rad, y-rad, 2*rad, 2*rad);
                           //g.drawRect(x-rad, y-rad, 2*rad, 2*rad);
                               }
                       }
                       if (!circle && !square){//άλλαξε το else που έχεις στον κώδικα σου με αυτό το if
                               for (int i = 0; i < n; i++) {
                                       // random in canvas
...........................

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...