I create a project.When I press "New Box" I want to create a blue box.Then when I press 'Line' I want to draw a line and connect the lines and the box. I created box's codes and this is working but line's codes is not working. Actually line's codes is working another window/folder but not here working. Maybe I made a mistake when write a codes.
This Window and main codes..
public class Cerceve extends JFrame implements ActionListener {
JPanel MAINPANEL, LEFTPANEL, RIGHTPANEL;
JButton btnBox, btnClear;
DragListener drag = new DragListener();
JButton btnLine;
public Cerceve() {
// ÇERÇEVE OLUŞTUR
super("ÇİZGE PROGRAMI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 700);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
// ÜZERİNE BİLEŞENLERİ EKLE
setLayout(new BorderLayout());
MAINPANEL = new JPanel();
MAINPANEL.setBackground(Color.WHITE);
this.add(MAINPANEL, BorderLayout.CENTER);
MAINPANEL.setLayout(new BorderLayout());
LEFTPANEL = new JPanel();
LEFTPANEL.setBackground(Color.GRAY);
MAINPANEL.add(LEFTPANEL, BorderLayout.WEST);
RIGHTPANEL = new JPanel();
RIGHTPANEL.setBackground(Color.WHITE);
MAINPANEL.add(RIGHTPANEL, BorderLayout.CENTER);
btnBox = new JButton("New Box");
LEFTPANEL.add(btnBox);
btnClear = new JButton("Clear");
LEFTPANEL.add(btnClear);
btnLine = new JButton("Line");
LEFTPANEL.add(btnLine);
btnBox.addActionListener(this);
btnClear.addActionListener(this);
btnLine.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnBox) {
Box box = new Box();
RIGHTPANEL.add(box);
RIGHTPANEL.repaint();
box.addMouseMotionListener(drag);
box.addMouseListener(drag);
}
else if (e.getSource() == btnClear) {
RIGHTPANEL.removeAll();
RIGHTPANEL.repaint();
}
else if (e.getSource() == btnLine) {
Line line = new Line();
RIGHTPANEL.repaint();
RIGHTPANEL.add(line);
line.addMouseListener(line.mouseHandler);
line.addMouseMotionListener(line.mouseMotionListener);
}
}
}
THİS İS BOX PACKAGE
public class Box extends JPanel {
public Box() {
setLayout(null);
setBackground(Color.BLUE);
setSize(50, 50);
setLocation(0, 0);
}
}
THİS İS DRAG LİSTENER PACKAGE
class DragListener extends MouseInputAdapter {
Point location;
MouseEvent mouseInfo;
#Override
public void mousePressed(MouseEvent me) {
mouseInfo = me;
}
#Override
public void mouseDragged(MouseEvent me) {
if (SwingUtilities.isLeftMouseButton(me) == true) {
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - mouseInfo.getX() + me.getX();
int y = location.y - mouseInfo.getY() + me.getY();
component.setLocation(x, y);
}
}
}
THİS İS LİNE PACKAGE
public class Line extends JFrame {
private int xBegin = 0;
private int xEnd = 0;
private int yBegin = 0;
private int yEnd = 0;
public MouseListener mouseHandler = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
xBegin = e.getX();
yBegin = e.getY();
repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
repaint();
}
};
public MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
repaint();
}
};
public void paint(Graphics g) {
super.paint(g);
g.drawLine(xBegin, yBegin, xEnd, yEnd);
}
}
Related
This code is kind of a mess. Im trying to prevent the mouse from going outside the red. MouseMotionListener is not working. Ive added The Listener, implemented it. When I try to use e.getX()Nothing shows up! Here is the code: Any help is good!
Class MazeMouse
public class MazeMouse extends JPanel implements ActionListener, MouseMotionListener {
JButton d,k;
JLabel f;
int button = 0;
MazeMouse(){
addMouseMotionListener(this);
setBackground(Color.black);
f= new JLabel(" ");
k = new JButton("Press To Finish");
d = new JButton("Press To Begin");
add(d);
add(f);
add(k);
k.addActionListener(this);
d.addActionListener(this);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(button == 1) {
g2.setColor(Color.red);
//start Rect
g2.fillRect(150,0,200,50);
//below start
g2.fillRect(150, 50, 50, 200);
//finish Rect
g2.fillRect(450, 0, 200, 50);
//below finish
g2.fillRect(600,50,50,200);
//really tiny line
g2.fillRect(150, 250, 500, 1);
}
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==d)
{
button++;
d.setEnabled(false);
repaint();
}
if(e.getSource()==k) {
setBackground(Color.green);
}
}
#Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent e) {
e.getX();
if(button ==1) {
if(e.getX() < 150 && e.getX() > 350 ) {
System.out.println("Game Over");
}
}
}
}
And MM
public class MM {
JFrame f;
MazeMouse p;
int button = 0;
public MM(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MazeMouse();
c.add(p);
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[ ]){
MM t = new MM();
}
}
Thanks
Could you please help me to know how to
create a ColorImage object by using the default constructor of the ColorImage class?
create a Canvas object by calling the Canvas constructor with arguments. The first and second arguments of the constructor are the width and the height of the ColorImage. The width and height of a ColorImage have obtain by using the getWidth() and getHeight() methods in the ColorImage class.
public class MyPaint {
public static void main(String[] args) {
new PaintFrame("JavaPainter",100,300);
}
}
class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
private Vector<Point> curve;
private Vector<Vector<Point>> curves;
private Point ptFrom = new Point();
private Point ptTo = new Point();
MyCanvas() {
curve = new Vector<Point>();
curves = new Vector<Vector<Point>>();
this.setPreferredSize(new Dimension(1, 1));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
for (Vector<Point> points : curves) {
Point pt0 = points.get(0);
for (int i = 1; i < points.size(); ++i) {
Point pt = points.get(i);
g.drawLine(pt0.x, pt0.y, pt.x, pt.y);
pt0 = pt;
}
}
}
#Override
public void mousePressed(MouseEvent e) {
ptFrom.x = e.getX();
ptFrom.y = e.getY();
curve.add((Point) ptFrom.clone());
}
#Override
public void mouseReleased(MouseEvent e) {
ptTo.x = e.getX();
ptTo.y = e.getY();
curve.add((Point) ptTo.clone());
curves.add(new Vector<Point>(curve));
curve.clear();
}
#Override
public void mouseDragged(MouseEvent e) {
ptTo.x = e.getX();
ptTo.y = e.getY();
curve.add((Point) ptTo.clone());
Graphics g = getGraphics();
g.setColor(Color.RED);
g.drawLine(ptFrom.x, ptFrom.y, ptTo.x, ptTo.y);
ptFrom.x = ptTo.x;
ptFrom.y = ptTo.y;
}
#Override
public void mouseEntered(MouseEvent e) {
// do nothing
}
#Override
public void mouseExited(MouseEvent e) {
// do nothing
}
#Override
public void mouseClicked(MouseEvent e) {
// do nothing
}
#Override
public void mouseMoved(MouseEvent e) {
// do nothing
}
}
class PaintFrame extends JFrame {
private MyCanvas canvas = new MyCanvas();
PaintFrame(String title) {
super(title);
Container cp = getContentPane();
cp.add(canvas);
setSize(300, 200);
setVisible(true);
}
PaintFrame(String title,Integer length,Integer width) {
super(title);
Container cp = getContentPane();
cp.add(canvas);
setSize(width, length);
setVisible(true);
}
}
From other place http://blog.csdn.net/fduan/article/details/8062556
I have a problem and can't solve it.
I want change the mouse cursor when I drag the JPanel and resize it, but when I press the JPanel and drag it, the mouse cursor will restore to the default cursor.
this is my code:
public boolean drag = false;
public Point dragLocation = new Point();
private JLabel test = new JLabel("Release");
private JLabel eGetPoint = new JLabel();
private JLabel dragLocationPoint = new JLabel();
public JLabel showSize = new JLabel();
private Cursor e_w_Cursor = new Cursor(Cursor.W_RESIZE_CURSOR);
private Cursor d_Cursor = new Cursor(Cursor.DEFAULT_CURSOR);
public drawPanel(){
setBounds(0,0,500,500);
setBackground(Color.WHITE);
showSize.setText(getWidth()+","+getHeight());
add(showSize);
add(test);
add(eGetPoint);
add(dragLocationPoint);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
test.setText("Drag");
dragLocation = e.getPoint();
dragLocationPoint.setText((int)(dragLocation.getX())+","+(int)(dragLocation.getY()));
drag = true;
setCursor(e_w_Cursor);
}
public void mouseReleased(MouseEvent e) {
test.setText("Release");
drag = false;
setCursor(d_Cursor);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (drag) {
eGetPoint.setText(e.getX()+","+e.getY());
showSize.setText(getWidth()+","+getHeight());
if (getWidth()-10 < dragLocation.getX() && dragLocation.getX() <= getWidth()) {
dragLocation = e.getPoint();
setSize(e.getX(), getHeight());
setCursor(e_w_Cursor);
}
if (getWidth() == e.getX()) {
setCursor(e_w_Cursor);
}
}
}
public void mouseMoved(MouseEvent e) {
if(getWidth()-10 < e.getX() && e.getX() <= getWidth()) {
setCursor(e_w_Cursor);
} else {
setCursor(d_Cursor);
}
}
});
Thank you.
I'm just trying to open an image in one panel and click a button on another panel to make that image disappear.
The following is just a very shorten version of the original code and addresses the specific problem.
public class NahualProductionTest {
public static void main(String[] args) {
NahualImagesPanel nahualPanel1 = new NahualImagesPanel();
NahualSettingsPanel nahualPanel2 = new NahualSettingsPanel();
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1,2));
frame.add(nahualPanel1);
frame.add(nahualPanel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,400);
frame.setLocation(200,200);
frame.setVisible(true);
}
}
public class NahualSettingsPanel extends JPanel {
private JButton button;
private NahualImagesPanel images;
public NahualSettingsPanel(){
images = new NahualImagesPanel();
button = new JButton("Add");
add(button);
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if (e.getSource()==button)
images.setVoid();
}
}
);
}
}
public class NahualImagesPanel extends JPanel{
private BufferedImage tempImage;
private int width, height, x, y;
private double wrate = 0;
private double hrate = 0;
private JButton load;
private PicPanel pics;
public NahualImagesPanel(){
setLayout(new BorderLayout());
pics = new PicPanel();
pics.setBackground(Color.DARK_GRAY);
JPanel commands = new JPanel();
commands.setLayout(new GridLayout(1,1));
load = new JButton("Add Image");
commands.add(load);
add(pics, BorderLayout.CENTER);
add(commands, BorderLayout.SOUTH);
load.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e1){
if (e1.getSource()==load){
try{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.showOpenDialog(null);
tempImage = ImageIO.read(chooser.getSelectedFile());
rescale(tempImage);
repaint();
} catch (IOException exception){
exception.printStackTrace();
System.exit(1);
} catch (IllegalArgumentException iae){}
}
}
}
);
}
public void setVoid(){
tempImage = null; //tempImage is set to null but after repaint() the image is still there
pics.revalidate();
pics.repaint();
}
public void rescale(Image img){
width = tempImage.getWidth();
height = tempImage.getHeight();
if (width>270 || height>250){
if (width-270>height-250){
wrate = (double) (width-270)/width;
width = 270;
height = height - (int) (wrate*height);
x = 10;
y = 15;
}
if (width-270<height-250) {
hrate = (double) (height-250)/height;
height = 250;
width = width - (int) (hrate*width);
x = (300-width)/2;
y = 15;
}
}
}
public class PicPanel extends JPanel{
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println(tempImage);
g.drawImage(tempImage, x, y, width, height, null);
//System.out.println(tempImage);
}
}
}
I have a JPanel that has 2+ JLables on it, I would like to be able to grab a label then move it to a different location on the JPanel. How can I do that? The only things I can find on this are moving a label from component "A" to component "B", nothing about moving it around on a Panel.
Start playing with this:
public class ComponentDragger extends MouseAdapter {
private Component target;
/**
* {#inheritDoc}
*/
#Override
public void mousePressed(MouseEvent e) {
Container container = (Container) e.getComponent();
for (Component c : container.getComponents()) {
if (c.getBounds().contains(e.getPoint())) {
target = c;
break;
}
}
}
/**
* {#inheritDoc}
*/
#Override
public void mouseDragged(MouseEvent e) {
if (target != null) {
target.setBounds(e.getX(), e.getY(), target.getWidth(), target.getHeight());
e.getComponent().repaint();
}
}
/**
* {#inheritDoc}
*/
#Override
public void mouseReleased(MouseEvent e) {
target = null;
}
public static void main(String[] args) {
JLabel label = new JLabel("Drag Me");
JPanel panel = new JPanel();
panel.add(label);
ComponentDragger dragger = new ComponentDragger();
panel.addMouseListener(dragger);
panel.addMouseMotionListener(dragger);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024, 768);
f.add(panel);
f.setVisible(true);
panel.setLayout(null);
f.setState(Frame.MAXIMIZED_BOTH);
}
}
Here's another example of this where the MouseListener and MouseMotionListener are on the JLabels themselves. For this to work, it needs to know the mouse's location on the screen vs it's initial location on screen when the mouse was initially pressed.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MovingLabels {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static void createAndShowGui() {
Random random = new Random();
final JPanel panel = new JPanel();
Color[] colors = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.cyan};
panel.setPreferredSize(new Dimension(PREF_W, PREF_H)); // sorry kleopatra
panel.setLayout(null);
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
for (int i = 0; i < colors.length; i++) {
Color c = colors[i];
JLabel label = new JLabel("Label " + (i + 1));
Border outsideBorder = new LineBorder(Color.black);
int eb = 10;
Border insideBorder = new EmptyBorder(eb, eb, eb, eb);
label.setBorder(BorderFactory.createCompoundBorder(outsideBorder , insideBorder));
label.setSize(label.getPreferredSize());
label.setBackground(c);
label.setOpaque(true);
int x = random.nextInt(PREF_W - 200) + 100;
int y = random.nextInt(PREF_H - 200) + 100;
label.setLocation(x, y);
label.addMouseListener(myMouseAdapter);
label.addMouseMotionListener(myMouseAdapter);
panel.add(label);
}
JFrame frame = new JFrame("MovingLabels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyMouseAdapter extends MouseAdapter {
private Point initialLoc;
private Point initialLocOnScreen;
#Override
public void mousePressed(MouseEvent e) {
Component comp = (Component)e.getSource();
initialLoc = comp.getLocation();
initialLocOnScreen = e.getLocationOnScreen();
}
#Override
public void mouseReleased(MouseEvent e) {
Component comp = (Component)e.getSource();
Point locOnScreen = e.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
}
#Override
public void mouseDragged(MouseEvent e) {
Component comp = (Component)e.getSource();
Point locOnScreen = e.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
}
}
You are probably getting it for the label itself. Try observing the coordinates of the panel. It should work
Here is what I wanted:
public class LayerItem extends JLabel{
protected int lblYPt = 0;
public LayerItem(JPanel layers){
this.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent evt){
lblMousePressed(evt);
}
});
this.addMouseMotionListener(new MouseAdapter(){
#Override
public void mouseDragged(MouseEvent evt){
lblMouseDragged(evt);
}
});
}
public void lblMousePressed(MouseEvent evt){
lblYPt = evt.getY();
}
public void lblMouseDragged(MouseEvent evt){
Component parent = evt.getComponent().getParent();
Point mouse = parent.getMousePosition();
try{
if(mouse.y - lblYPt >= 30){
this.setBounds(0, mouse.y - lblYPt, 198, 50);
}
}catch(Exception e){
}
}
}