473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with moving shapes

so far i got this...
but the shapes that i created i need to be able to click and dragg and move around with the mouse..help!!!!

and also some where here i need to draw and equal sided triangle


IN PANEL

import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.sql.Array;
import javax.swing.*;

public class Drawing extends JPanel
implements MouseListener, MouseMotionListener {
int size = 60;
int x = 0;
int y = 0;
boolean dragging = false;

public Drawing() {
addMouseListener(this);
addMouseMotionListener(this);
}
/** Creates new form Drawing */
@Override public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(new Color(125, 167, 116));
g2d.fillOval(10, 15, size, size);

g2d.setColor(new Color(42, 179, 231));
g2d.fillRect(130, 15, 90, size);

g2d.setColor(new Color(70, 67, 123));
g2d.fillRect(250, 15, 90, size);


if (dragging) {
g2d.setColor(new Color(255, 255, 255));
g2d.drawString("Dragging",x,y);
}

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>

public void mouseClicked(MouseEvent e) {
size = size + 5;
this.repaint();
}

public void mousePressed(MouseEvent e) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.M OVE_CURSOR));

}

public void mouseReleased(MouseEvent e) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.D EFAULT_CURSOR));
dragging = false;
this.repaint();
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseDragged(MouseEvent e) {
dragging = true;
x = e.getX();
y = e.getY();
this.repaint();
}

public void mouseMoved(MouseEvent e) {

}




// Variables declaration - do not modify
// End of variables declaration

}
IN FRAME

import java.awt.*;
import javax.swing.JFrame;
/**
*
* @author User
*/
public class Ass4Main {


public static void main(String[] args){
Drawing draw = new Drawing();
JFrame frame = new JFrame("Drawing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add(draw);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}


}
Jun 12 '08 #1
1 3960
BigDaddyLH
1,216 Expert 1GB
A common way to be able to drag images, shapes, etc... around is to
1. Subclass JComponent to turn your image, shape, etc... into a component. You can even override the contains method to have a non-rectangular component.
2. Put the components in a JLayeredPane
Jun 12 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Carl Gilbert | last post by:
Hi I have a math kinda problem where I'm trying to split some lines when two or more lines connect two shapes. The reason I am doing this is to make it clear that there are multiple lines...
5
by: Michael Russell | last post by:
Hi all, Using C#, I've created a simple wrapper class for using Excel. I have Office Pro 2003 installed on my devel machine. The wrapper class works great, reading and writing to/from Excel. ...
1
by: PreciousK | last post by:
I have a project to complete and Im not too comfortable with modifying Shapes. I have to drag a shape to some other location. By clicking the left side of the mouse button. The shape changes colour...
5
by: tex1ntux | last post by:
Can someone offer some insight to help correct this piece of code? TwoDShape *shapes; shapes = &Triangle("right", 8.0, 12.0); shapes = &Rectangle(10); shapes = &Rectangle(10,...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
12
by: StephQ | last post by:
I face the following problem. I wrote a poor's man plotting function: computePlot. This function append some x-values belonging to step and the correseponding f( x ) (for a given const member...
1
by: javatech007 | last post by:
The question is below and it is the only one I cant for a project due tomarrow. Question. Write a class called shapes that accepts two variables into its constructor called a and b. Along with...
12
by: =?Utf-8?B?TWFyaw==?= | last post by:
I modified a C# application from Bob Powell that moves graphics primitives around on the screen into a Vb.NET 2005 application. I want to modify this application so along with moving regular...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.