So I have two classes here:
PhotoComponent class:
(This class is to handle a specific image as a JComponent. When "flipped" I want to draw pen strokes instead of having an image. So I replace the image with a rectangle, attempting to draw pen strokes over it.)
public class PhotoComponent extends JComponent {
private Image pic;
private boolean flipped;
private int contentAreaWidth;
private int contentAreaHeight;
p
#Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
System.out.println("Draw: " + draw + ", Pic: " + pic);
if (draw && pic != null) {
super.paintComponent(g);
System.out.println("width using this: " + this.getWidth() + ", actual width of JPanel: " + contentAreaWidth);
System.out.println("height using this: " + this.getHeight() + ", actual height of JPanel: " + contentAreaHeight);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x,y,pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (drawingMode) {
g2.setPaint(Color.RED);
if (drawOval) {
penStrokes.put(ovalX, ovalY);
if (penStrokes != null) {
for (Integer xCoor : penStrokes.keySet()) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((xCoor - (brushSize / 2)), (penStrokes.get(xCoor) - (brushSize / 2)), brushSize, brushSize);
//System.out.println("SIZE OF HASHTABLE: " + penStrokes.size());
}
}
System.out.println("Filling an oval!" + ovalX + ", " + ovalY);
}
} else if (textMode) {
g2.setPaint(Color.YELLOW);
if (drawRect) {
rectDimensions.add(rectX);
rectDimensions.add(rectY);
rectDimensions.add(rectWidth);
rectDimensions.add(rectHeight);
for (int i = 0; i < rectDimensions.size(); i+=4) {
g2.fillRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
g2.drawRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
}
}
}
System.out.println("This is being called again!");
}
}
}
public void setRectangle(int x, int y, int width, int height) {
drawRect = true;
rectX = x;
rectY = y;
rectWidth = width;
rectHeight = height;
}
public void removeRectangle() {
drawRect = false;
}
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX =
NOTE THE DRAWLINE() METHOD ABOVE. I am drawing at the given points, repainting, and setting the old variables to be the current variables.
Main class:
private static PhotoComponent img;
private static JFrame frame;
private static JPanel contentArea;
//Mouse Coordinates for PenStrokes
private static int oldX, oldY;
//Mouse Coordinates for Sticky Notes
private static Point clickPoint;
public static void main (String[] args) {
frame = new JFrame("PhotoManip");
img = null;
contentArea = null;
oldX = 0;
oldY = 0;
setupMenubar(frame);
setupJFrame(frame);
}
private static void addPhotoComponent(File file) {
}
if (img.getTextMode()) {
img.removeRectangle();
clickPoint = null;
}
}
});
img.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (img.getDrawingMode()) {
if (withinRange(e.getX(), e.getY())) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
img.repaint();
}
}
if (img.getTextMode()) {
if (withinRange(e.getX(), e.getY())) {
Point dragPoint = e.getPoint();
h, height);
img.repaint();
}
}
}
});
if (img!=null) {
contentArea.add(img);
}
}
private static boolean withinRange(int x, int y) {
if (x > img.getX() && x < img.getX() + img.getWidth()) {
if (y > img.getY() && y < img.getY() + img.getHeight()) {
return true;
}
}
return false;
}
private static void flipImage() {
if (!img.isFlipped()) {
img.setFlipped(true);
} else if (img.isFlipped()) {
img.setFlipped(false);
}
}
drawLine() is called above in this main class, when a mousedrag occurs. Problem is that the strokes don't appear to show.
I know that the program is calling g2.fillOval() because I am printing out a verification statement afterwards.
Additionally, I have created print statements for when the mouse is pressed and dragged and they are getting the correct coordinates?
Why don't red strokes appear? I'm confused. Is it the way my code is structured?
The crux of your problem is that you are trying to draw something outside the paintComponent method, which is never supported. Whatever you draw will get overwritten by the next call of paintComponent, which will happen almost instantly. We can solve this by storing the co-ordinates of the oval and drawing it within paintComponent instead of trying to draw on a graphics object outside of the paintComponent method. See code below:
First we are going to add the following variables to your PhotoComponent class:
private boolean drawOval = false;
private int ovalX = 0;
private int ovalY = 0;
Then we will add methods for controlling them:
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX = currentX;
ovalY = currentY;
return toReturn;
}
public void removeOval() {
drawOval = false;
}
After that we can change the paintComponent method to have it draw the oval based on those variables:
#Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
super.paintComponent(g);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.RED);
}
//took the code you already used for drawing the oval and moved it here
if (drawOval) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((ovalX - (brushSize / 2)), (ovalY - (brushSize / 2)), brushSize, brushSize);
}
}
Finally change the addPhotoComponent method to update those variables instead of trying to draw the oval directly:
private static void addPhotoComponent(File file) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e2) {
System.out.println("ERROR: Couldn't get image.");
}
img = new PhotoComponent(image, contentArea);
img.revalidate();
img.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// your code here
System.out.println("You flipped the photo!!!");
flipImage();
img.repaint();
}
}
#Override
public void mousePressed(MouseEvent e) {
img.setOval(e.getX(), e.getY());
}
#Override
public void mouseReleased(MouseEvent e) {
img.removeOval();
}
});
img.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
}
});
if (img != null) {
contentArea.add(img);
}
}
Related
Is there any way to get only the initial press from the drag, because right now it keeps toggling between black and white when dragged over the same square. Also, I am trying to get from the command line the width and height of the rectangles that will make the grid, if the user screws up or nothing is input then they are set to 50 by default. I tried creating a method because I didn't really know how to put them in the main an then use it in the JPanel.
public class Clicky extends JFrame {
private static class Board extends JPanel {
public int BRICK_WIDTH = 50;
public int BRICK_HEIGHT = 50;
public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public double width = screenSize.getWidth();
public double height = screenSize.getHeight();
public double bWidth;
public double bHeight;
private int COLS = (int) (width / bWidth);
private int ROWS = (int) (height / bHeight);
private Color CO = Color.BLACK;
private boolean[][] isWhite = new boolean[COLS + 1][ROWS + 1];
public Board() {
System.out.println("WIdth:" + COLS + "Height:" + ROWS);
setBackground(Color.BLACK);
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
System.out.printf("X: %d Y: %d ", mx, my);
isWhite[(int) (mx / bWidth)][(int) (my
/ bHeight)] = !isWhite[(int) (mx / bWidth)][(int) (my / bHeight)];
repaint();
}
});
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
mx = e.getX();
my = e.getY();
isWhite[(int) (mx / bWidth)][(int) (my
/ bHeight)] = !isWhite[(int) (mx / bWidth)][(int) (my / bHeight)];
int gridx = e.getX();
int gridy = e.getY();
System.out.println(gridx);
}
#Override
public void mouseMoved(MouseEvent e) {
}
});
}
#Override
protected void paintComponent(Graphics g,String[] args ) {
super.paintComponent(g);
drawBricks(g);
getValues(args);
}
private double x;
private double y;
public void getValues(String[] args){
try
{
bWidth = Integer.valueOf(args[0]);
}
catch (IndexOutOfBoundsException | NumberFormatException ex)
{
// If the argument was bad then use the default.
bWidth = BRICK_WIDTH;
}
try
{
bHeight = Integer.valueOf(args[1]);
}
catch (IndexOutOfBoundsException | NumberFormatException ex)
{
// If the argument was bad then use the default.
bHeight = BRICK_HEIGHT;
}
}
private void drawBricks(Graphics g) {
Graphics2D brick = (Graphics2D) g.create();
x = 0;
y = 0;
for (int j = 0; j <= ROWS; j++) {
for (int a = 0; a <= COLS; a++) {
if (isWhite[a][j]) {
brick.setColor(Color.WHITE);
} else {
brick.setColor(Color.BLACK);
}
Rectangle2D.Double rect = new Rectangle2D.Double(x, y, bWidth, bHeight);
brick.fill(rect);
brick.setColor(Color.gray);
brick.draw(rect);
x += bWidth;
}
repaint();
x = 0;
y += bHeight;
}
}
public int mx = -100;
public int my = -100;
}
public Clicky() {
setDefaultCloseOperation(EXIT_ON_CLOSE); // mai bine cu exit on close
setSize(800, 820);
add(new Board());
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Clicky().setVisible(true);
}
});
}
}
There isn't an event you can listen for that will tell you when the drag event starts. You can write one yourself. You'll need to some kind of stateful variable that can be toggled on the first receipt of a mouse dragged event then used as a guard for subsequent mouse dragged events. You can reset the stateful variable on a mouse release event. See MouseEvent and MouseEventListener.
As for your other question - I'm not really sure what you're asking. Are you wondering how to get user-supplied data from the command line or trying to figure out how to make use of them once they are supplied?
For the former it's dead simple with command line arguments. For the latter, all you have to do is write a setter method that validates the input and overrides the default if caller-supplied values are valid.
I have a program where I am able to move images around on the screen by clicking and dragging. The only problem is, when I click an image and start to drag it around on my panel, the image first jumps to the position of the mouse cursor. How do I prevent this, so that the image is simply moved no matter where I click on the image? I can only get the mouse cursor to either jump to under the mouse arrow when starting to drag, or over the mouse arrow when starting to drag, or set to the middle of the image to drag.
I am moving an Image object of the Image class in another class with the following function:
moveImage(selected,x,y);
where x and y are coordinates from the mouseDragged method.
This is my image class:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
class Image {
private int x;
private int y;
private BufferedImage image;
private StringBuffer filepath;
private boolean isTurned;
public Image(int x, int y, String filepath) throws IOException {
this.filepath = new StringBuffer(filepath);
this.x = x;
this.y = y;
this.image = ImageIO.read(new File(String.valueOf(filepath)));
}
public void draw(Graphics g){
g.drawImage(image, x, y, null);
}
public void undraw (Graphics g, Color c ){
g.setColor(c);
g.fillRect(x,y, image.getWidth(), image.getHeight());
}
public boolean containsXY (int x, int y){
if ( (this.x <= x ) && (x <= (this.x+this.image.getWidth())) && (this.y <= y ) && (y <= (this.y+this.image.getHeight())) ){
return true;
}
return false;
}
public void move (Graphics g, int x, int y) {
System.out.println("int x = " + x + " int y = " + y);
System.out.println("this x = " + this.x + " this y = " + this.y);
System.out.println("width: " + this.image.getWidth() + " height: " + this.image.getHeight());
undraw(g, Color.WHITE);
/* this.x = x - ((x+image.getWidth())-x);
this.y = y -((y+image.getHeight())-y);
this.x = x - (x-this.x);
this.y = y - (y-this.y);
this.x = x - (this.x+image.getWidth()-x);
this.y = y - (this.y+image.getHeight()-y);
this.x = this.x + (x-image.getWidth());
this.y = this.y + (y-image.getHeight());
this.x = x - (image.getWidth());
this.y = y - (image.getHeight());*/
this.x = x - (image.getWidth()/2);
this.y = y - (image.getHeight()/2);
draw(g);
}
public void turn(Graphics g) throws IOException {
if (isTurned) {
filepath.replace(filepath.length()-9, filepath.length(), ".gif" );
undraw(g, Color.WHITE);
image = ImageIO.read(new File(String.valueOf(filepath)));
draw(g);
isTurned = false;
}
else {
filepath.replace(filepath.length()-4, filepath.length(), " back.gif" );
undraw(g, Color.WHITE);
image = ImageIO.read(new File(String.valueOf(filepath)));
draw(g);
isTurned = true;
}
}
}
As you can see, inside the move method, I have tried alot of different things on manipulation the incoming mouse event dragged coordinates. Right now in the code I am using
this.x = x - (image.getWidth()/2);
this.y = y - (image.getHeight()/2);
which makes the mouse cursor lock on the middle of the image when I am dragging it around which is the best I can currently do. So when I want to move an image and click in the corner of the image, it "pops" to the middle of the image and locks there while dragging the image around. But I do not want this. I want the mouse cursor to just stay on the location on the image where I clicked when I started the drag, not redraw the image somewhere else and then start the drag.
This is the class from which I call the Image object, I am calling it inside this class:s method moveImage:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
class PaintSurface extends JLabel implements MouseListener, MouseMotionListener {
private int x, y;
private JButton browse;
private Collection<Image> images = new ArrayList<Image>();
private final JFileChooser fc = new JFileChooser();
private Image selected;
public PaintSurface(JButton b){
browse = b;
addMouseListener(this);
addMouseMotionListener(this);
browse.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int x = fc.showOpenDialog(browse);
if (x == JFileChooser.APPROVE_OPTION){
try {
buttonPressed(fc);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if (x ==JFileChooser.CANCEL_OPTION){
System.out.println("No file selected.");
}
}
});
}
public void paintComponent (Graphics g){
super.paintComponent(g);
for (Image i: images){
i.draw(g);
}
}
public void addImage(Image i){
images.add(i);
Graphics g = getGraphics();
i.draw(g);
}
public void buttonPressed(JFileChooser fc) throws IOException {
File selectedFile = fc.getSelectedFile();
String filepath = String.valueOf(selectedFile.getAbsolutePath());
Image i = new Image(x, y, filepath );
selected = i;
addImage(i);
repaint();
}
public Image findImage(int x, int y){
Image[] imageArray = images.toArray(new Image[images.size()]);
for (int i = imageArray.length - 1; i >= 0; i--){
if (imageArray[i].containsXY(x, y)){
return imageArray[i];
}
}
return null;
}
public void moveImage (Image i, int x, int y) { //
i.move(getGraphics(), x, y);
}
public boolean removeImage(Image i){
Graphics g = getGraphics();
i.undraw(g, Color.WHITE);
return images.remove(i);
}
#Override
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
System.out.println("mouseclick");
selected = findImage(x,y);
if (selected != null) {
Graphics g = getGraphics();
try {
selected.turn(g);
repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
selected = null;
}
}
#Override
public void mousePressed(MouseEvent e) {
Image i = findImage(e.getX(), e.getY());
if (i == null) {
System.out.println("null mousepress");
return;
}
else {
System.out.println("not null mousepress");
if (i == selected){
return;
}
else {
removeImage(i);
addImage(i);
selected = i;
}
}
}
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (selected != null) {
moveImage(selected,x,y);
repaint();
}
}
#Override
public void mouseReleased(MouseEvent e) {
selected = null;
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
}
}
Can anyone with experience in this help? I am pulling my hair with this since about a week now...
You need to track the change from where you clicked instead of moving to the new cursor position. In mousePressed, store the original press location:
// lastX, lastY are object variables
lastX = e.getX();
lastY = e.getY();
In mouseDragged, calculate the delta
public void mouseDragged(MouseEvent e) {
int x = i.getX() + e.getX() - lastX;
int y = i.getY() + e.getY() - lastY;
if (selected != null) {
moveImage(selected,x,y);
repaint();
}
lastX = e.getX();
lastY = e.getY();
}
You can store two variables for x and y offset in your PaintSurface class and initilize them in the findImage() method like this:
if (imageArray[i].containsXY(x, y)){
xOffset = x - imageArray[i].getX();
xOffset = y - imageArray[i].getY();
return imageArray[i];
}
Then you change your move() method in Image to
move(Graphics g, int x, int y, int xOffset, int yOffset) {
undraw(g, Color.WHITE);
this.x = x - xOffset;
this.y = y - yOffset;
draw(g);
}
How to drag free-form lines (paths) with AffineTransform?
I'm making a Paint-like application in Java and one of the requirements is being able to drag drawn free-form shapes to a new location on a JPanel used for drawing. I've been trying to get AffineTransform to do this for a day now and what it does at the moment is while the required line(which is stored as a Path2D) is selected and dragged it does move. However, once there is no more line selected, the line goes back to its original location. Also, when I select it again, it is immediately shown in that new location (if that makes any sense); this is probably to the coordinate system being translated, but I'm not sure...
Any help will be highly appreciated! Perhaps there is an simpler way of doing this.
PS I also noticed that when moving any of the drawn lines, except for the one drawn last, all of the lines drawn prior to the line, which is being moved, are moved together with it.
Here's the code:
public class DrawPanel extends JPanel {
public double translateX=0;
public double translateY=0;
public int lastOffsetX;
public int lastOffsetY;
class Line {
public Point start;
public Point end;
public Color color;
public Path2D path;
}
ArrayList<Line> lines = new ArrayList<Line>();
ArrayList<Path2D> paths = new ArrayList<Path2D>();
boolean moveMode = false;
Path2D selectedLine;
int xDistance;
int yDistance;
public DrawPanel() {
setBackground(Color.WHITE);
setFocusable(true);
requestFocusInWindow();
this.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
//System.out.println(resizeMode);
if (moveMode) {
if (selectedLine!=null) {
int newX = e.getX() - lastOffsetX;
int newY = e.getY() - lastOffsetY;
lastOffsetX += newX;
lastOffsetY += newY;
translateX += newX;
translateY += newY;
repaint();
}
} else {
Path2D p = paths.get(paths.size() - 1);
p.lineTo(e.getX(), e.getY());
repaint();
}
}
#Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
if (resizeMode) {
selectedLine = null;
for (Path2D l : paths) {
if (l.contains(e.getPoint())) {
selectedLine = l;
}
}
repaint();
}
}
});
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (!moveMode) {
Line l = new Line(e.getPoint());
l.path = new Path2D.Double();
l.path.moveTo(e.getX(), e.getY());
lines.add(l);
paths.add(l.path);
} else {
lastOffsetX = e.getX();
lastOffsetY = e.getY();
}
}
}
#Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
if (!resizeMode) {
if (selectedLine == null) {
Line l = lines.get(lines.size() - 1);
l.end = e.getPoint();
l.path.lineTo(e.getX(), e.getY());
Path2D p = paths.get(paths.size() - 1);
p.lineTo(e.getX(), e.getY());
repaint();
}
} else {
for (int j=0; j<paths.size();j++) {
if (selectedLine!=null && selectedLine.equals(paths.get(j))) {
paths.set(j, selectedLine);
}
}
repaint();
}
}
});
}
private void setKeyBindings() {
ActionMap actionMap = getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition );
String ctrl = "VK_CONTROL";
String ctrl_rel = "control_released";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, KeyEvent.CTRL_DOWN_MASK, false), ctrl);
inputMap.put(KeyStroke.getKeyStroke(("released CONTROL")), ctrl_rel);
actionMap.put(ctrl, new KeyAction(ctrl));
actionMap.put(ctrl_rel, new KeyAction(ctrl_rel));
}
private class KeyAction extends AbstractAction {
public KeyAction(String actionCommand) {
putValue(ACTION_COMMAND_KEY, actionCommand);
}
#Override
public void actionPerformed(ActionEvent actionEvt) {
if(actionEvt.getActionCommand() == "VK_CONTROL") {
moveMode = true;
}
else if(actionEvt.getActionCommand() == "control_released") {
moveMode = false;
repaint();
}
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(10.0f));
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
if (moveMode) {
for (int j=0; j<paths.size();j++) {
Path2D path = paths.get(j);
if (selectedLine!=null && selectedLine.equals(path)) {
AffineTransform at = new AffineTransform();
at.translate(translateX, translateY);
g2d.transform(at);
g2d.setColor(Color.RED);
g2d.draw(path);
g2d.setColor(Color.BLACK);
continue;
}
g2d.draw(path);
g2d.setColor(Color.BLACK);
}
} else {
for (int i =0; i < paths.size();i++) {
Path2D path = paths.get(i);
g2d.draw(path); // outline
}
}
}
Edit to include resolution: So in the end what I did is I saved all of the path's coordinates (which I got from PathIterator) to and ArrayList, created a new empty path, added the previous path's coordinates from the ArrayList to the new path (via moveTo, lineTo) and appended the new path to the ArrayList of all drawn paths.
As you suspect, your AffineTransform alters the graphics context's coordinate system for all subsequent drawing.
In the example cited here, each shape is an instance of the class Node. Each Node includes a selected attribute, which allows the shape to be selected independently. The value determines the effect of updatePosition() when called from mouseDragged(). The implementation of updatePosition() simply updates each selected node's coordinates, but you can also use the createTransformedShape() of AffineTransform.
I make a game in java and I can't seem to call the paintComponent() method in my game. here's the code :
public class TwinMoonTowers extends JPanel implements ActionListener{
private class TMTMenu extends MouseAdapter{
private Icon start, shop, quit;
private Icon main_bg;
public TMTMenu (){
System.out.println("starting menu...");
start = new Icon(TMTConstant.START_SOURCE,
TMTConstant.START_COORD_X, TMTConstant.START_COORD_Y);
shop = new Icon (TMTConstant.SHOP_SOURCE,
TMTConstant.SHOP_COORD_X, TMTConstant.SHOP_COORD_Y);
quit = new Icon (TMTConstant.EXIT_SOURCE,
TMTConstant.EXIT_COORD_X, TMTConstant.EXIT_COORD_Y);
main_bg = new Icon (TMTConstant.MAIN_MENU_BG, 0, 0);
}
#Override
public void mousePressed(MouseEvent me){
int x = me.getX();
int y = me.getY();
System.out.println("coord: "+x+","+y);
if (me.getButton() == MouseEvent.BUTTON1){
if (x > start.x && x < start.x + start.getIcon().getWidth(null)
&& y > start.y && y < start.y + start.getIcon().getHeight(null)){
curState = TMTConstant.MODE_STATE;
}
else if (x > quit.x && x < quit.x + quit.getIcon().getWidth(null)
&& y > quit.y && y < quit.y + quit.getIcon().getHeight(null)){
curState = TMTConstant.EXIT_STATE;
}
else if (x > shop.x && x < shop.x + shop.getIcon().getWidth(null)
&& y > shop.y && y < shop.y + shop.getIcon().getHeight(null)){
curState = TMTConstant.SHOP_STATE;
}
}
}
}
private class TMTModeSelector extends MouseAdapter{
private Icon TwinTower, TripleTower, back;
public TMTModeSelector(){
}
#Override
public void mousePressed(MouseEvent e){
}
}
private String testDebug;
private int curState;
private TMTMenu menu;
public TwinMoonTowers (){
menu = new TMTMenu();
curState = TMTConstant.MAIN_MENU_STATE;
System.out.println("creating panel..");
this.setVisible(true);
this.setDoubleBuffered(true);
this.setSize(TMTConstant.WINDOW_WIDTH, TMTConstant.WINDOW_HEIGHT);
this.addMouseListener(menu);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
this.repaint();
}
#Override
public void paintComponent (Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
System.out.println("preparing canvas..");
switch(curState){
case TMTConstant.MAIN_MENU_STATE :
g2d.drawImage(menu.main_bg.getIcon(), menu.main_bg.x, menu.main_bg.y, this);
g2d.drawImage(menu.start.getIcon(), menu.start.x, menu.start.y, null);
g2d.drawImage(menu.quit.getIcon(), menu.quit.x, menu.quit.y, null);
g2d.drawImage(menu.shop.getIcon(), menu.shop.x, menu.shop.y, null);
break;
case TMTConstant.GAME_STATE :
break;
case TMTConstant.SHOP_STATE :
break;
case TMTConstant.MODE_STATE :
break;
}
System.out.print("render image...");
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
}
while class Icon is this:
public class Icon{
private Image pic;
public int x, y;
public Icon (String path, int x, int y){
ImageIcon ii = new ImageIcon (path);
pic = ii.getImage();
this.x = x;
this.y = y;
}
public Image getIcon() {return pic;}
}
the basics here is to draw a menu containing 3 buttons (start, shop, exit)
but, the paintComponent method is not called (the system.out.println not showing)
Toolkit.getDefaultToolkit().sync();
g.dispose();
These lines are unnecessary (might even be wrong), remove them.
The problem is most likely not in the code you posted. Where do you add the panel to a top level window (frame/applet)? What layout do you use for its content pane? My guess is the size is (0,0) so the panel won't be painted.
I'm trying to create a custom extension of BasicSliderUI. I'm just trying to make the thumb a circle (note I'm in the Windows L&F). I've created a very simple implementation that just calls g.drawOval, but whenever I drag it, it leaves a "trail" behind. Any ideas why this is?
thanks,
Jeff
You need to call repaint on the whole thing, you cant just draw the oval on top of it. Swing will by default only repaint what needs to be repainted, which usually isn't the whole control. When are you drawing the circle?
If you want to get rid of "trail" when you drag you should write your custom TrackListener and control trumb position related to mouse move.
Look at my implementation:
public class LightSliderUI extends BasicSliderUI{
private final Color rangeColor = Color.BLUE;
private final BasicStroke stroke = new BasicStroke(2f);
private transient boolean upperDragging;
public LightSliderUI(JSlider b) {
super(b);
}
public static ComponentUI createUI(JComponent c) {
return new LightSliderUI((JSlider)c);
}
#Override
protected void calculateThumbSize() {
super.calculateThumbSize();
thumbRect.setSize(thumbRect.width, thumbRect.height);
}
/** Creates a listener to handle track events in the specified slider.*/
#Override
protected TrackListener createTrackListener(JSlider slider) {
return new RangeTrackListener();
}
#Override
protected void calculateThumbLocation() {
// Call superclass method for lower thumb location.
super.calculateThumbLocation();
// Adjust upper value to snap to ticks if necessary.
if (slider.getSnapToTicks()) {
int upperValue = slider.getValue() + slider.getExtent();
int snappedValue = upperValue;
int majorTickSpacing = slider.getMajorTickSpacing();
int minorTickSpacing = slider.getMinorTickSpacing();
int tickSpacing = 0;
if (minorTickSpacing > 0) {
tickSpacing = minorTickSpacing;
} else if (majorTickSpacing > 0) {
tickSpacing = majorTickSpacing;
}
if (tickSpacing != 0) {
// If it's not on a tick, change the value
if ((upperValue - slider.getMinimum()) % tickSpacing != 0) {
float temp = (float)(upperValue - slider.getMinimum()) / (float)tickSpacing;
int whichTick = Math.round(temp);
snappedValue = slider.getMinimum() + (whichTick * tickSpacing);
}
if (snappedValue != upperValue) {
slider.setExtent(snappedValue - slider.getValue());
}
}
}
// Calculate upper thumb location. The thumb is centered over its
// value on the track.
if (slider.getOrientation() == JSlider.HORIZONTAL) {
int upperPosition = xPositionForValue(slider.getValue() + slider.getExtent());
thumbRect.x = upperPosition - (thumbRect.width / 2);
thumbRect.y = trackRect.y;
} else {
int upperPosition = yPositionForValue(slider.getValue() + slider.getExtent());
thumbRect.x = trackRect.x;
thumbRect.y = upperPosition - (thumbRect.height / 2);
}
slider.repaint();
}
/** Returns the size of a thumb.
* Parent method not use size from LaF
* #return size of trumb */
#Override
protected Dimension getThumbSize() {
return Dimensions.getSliderThumbSize();
}
private Shape createThumbShape(int width, int height) {
Ellipse2D shape = new Ellipse2D.Double(0, 0, width, height);
return shape;
}
#Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Stroke old = g2d.getStroke();
g2d.setStroke(stroke);
g2d.setPaint(Colors.TEXT_STEEL);
Color oldColor = Colors.TEXT_STEEL;
Rectangle trackBounds = trackRect;
if (slider.getOrientation() == SwingConstants.HORIZONTAL) {
g2d.drawLine(trackRect.x, trackRect.y + trackRect.height / 2,
trackRect.x + trackRect.width, trackRect.y + trackRect.height / 2);
int lowerX = thumbRect.width / 2;
int upperX = thumbRect.x + (thumbRect.width / 2);
int cy = (trackBounds.height / 2) - 2;
g2d.translate(trackBounds.x, trackBounds.y + cy);
g2d.setColor(rangeColor);
g2d.drawLine(lowerX - trackBounds.x, 2, upperX - trackBounds.x, 2);
g2d.translate(-trackBounds.x, -(trackBounds.y + cy));
g2d.setColor(oldColor);
}
g2d.setStroke(old);
}
/** Overrides superclass method to do nothing. Thumb painting is handled
* within the <code>paint()</code> method.*/
#Override
public void paintThumb(Graphics g) {
Rectangle knobBounds = thumbRect;
int w = knobBounds.width;
int h = knobBounds.height;
Graphics2D g2d = (Graphics2D) g.create();
Shape thumbShape = createThumbShape(w - 1, h - 1);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.translate(knobBounds.x, knobBounds.y);
g2d.setColor(Color.WHITE);
g2d.fill(thumbShape);
g2d.setColor(Colors.BIOLIN_BLUE_TINT);
g2d.draw(thumbShape);
g2d.dispose();
}
/** Listener to handle model change events. This calculates the thumb
* locations and repaints the slider if the value change is not caused by dragging a thumb.*/
public class ChangeHandler implements ChangeListener {
#Override
public void stateChanged(ChangeEvent arg0) {
calculateThumbLocation();
slider.repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JSlider slider = new JSlider(0, 100);
slider.setPaintTicks(true);
slider.setUI(new LightSliderUI(slider));
frame.add(slider);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
/** Listener to handle mouse movements in the slider track.*/
public class RangeTrackListener extends TrackListener {
#Override
public void mouseClicked(MouseEvent e) {
if (!slider.isEnabled()) {
return;
}
currentMouseX -= thumbRect.width / 2; // Because we want the mouse location correspond to middle of the "thumb", not left side of it.
moveUpperThumb();
}
public void mousePressed(MouseEvent e) {
if (!slider.isEnabled()) {
return;
}
currentMouseX = e.getX();
currentMouseY = e.getY();
if (slider.isRequestFocusEnabled()) {
slider.requestFocus();
}
boolean upperPressed = false;
if (slider.getMinimum() == slider.getValue()) {
if (thumbRect.contains(currentMouseX, currentMouseY)) {
upperPressed = true;
}
} else {
if (thumbRect.contains(currentMouseX, currentMouseY)) {
upperPressed = true;
}
}
if (upperPressed) {
switch (slider.getOrientation()) {
case JSlider.VERTICAL:
offset = currentMouseY - thumbRect.y;
break;
case JSlider.HORIZONTAL:
offset = currentMouseX - thumbRect.x;
break;
}
//upperThumbSelected = true;
upperDragging = true;
return;
}
upperDragging = false;
}
#Override
public void mouseReleased(MouseEvent e) {
upperDragging = false;
slider.setValueIsAdjusting(false);
super.mouseReleased(e);
}
#Override
public void mouseDragged(MouseEvent e) {
if (!slider.isEnabled()) {
return;
}
currentMouseX = e.getX();
currentMouseY = e.getY();
if (upperDragging) {
slider.setValueIsAdjusting(true);
moveUpperThumb();
}
}
#Override
public boolean shouldScroll(int direction) {
return false;
}
/** Moves the location of the upper thumb, and sets its corresponding value in the slider.*/
public void moveUpperThumb() {
int thumbMiddle = 0;
switch (slider.getOrientation()) {
case JSlider.HORIZONTAL:
int halfThumbWidth = thumbRect.width / 2;
int thumbLeft = currentMouseX - offset;
int trackLeft = trackRect.x;
int trackRight = trackRect.x + (trackRect.width - 1);
int hMax = xPositionForValue(slider.getMaximum() -
slider.getExtent());
if (drawInverted()) {
trackLeft = hMax;
}
else {
trackRight = hMax;
}
thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
setThumbLocation(thumbLeft, thumbRect.y);//setThumbLocation
thumbMiddle = thumbLeft + halfThumbWidth;
slider.setValue(valueForXPosition(thumbMiddle));
break;
default:
return;
}
}
}
}